-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve UX and workflow, write tests
- Loading branch information
mrkvon
committed
Dec 2, 2018
1 parent
30d669f
commit 0aed9f3
Showing
7 changed files
with
279 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
/** | ||
* @TODO make these elements nicer | ||
*/ | ||
|
||
export function Self() { | ||
return (<div>Self</div>); | ||
} | ||
|
||
export function Loading() { | ||
return (<div>Loading</div>); | ||
} | ||
|
||
export function Duplicate() { | ||
return (<div>Duplicate</div>); | ||
} | ||
|
||
export function Submitted({ isReported, isPublic }) { | ||
return ( | ||
<div> | ||
<div>Submitted</div> | ||
{(isReported) ? <div>Reported</div> : null} | ||
<div>{(isPublic) ? '' : 'not '}public</div> | ||
</div> | ||
); | ||
} | ||
|
||
Submitted.propTypes = { | ||
isReported: PropTypes.boolean, | ||
isPublic: PropTypes.boolean | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
'use strict'; | ||
|
||
import ReferencesNew from '../../client/components/ReferencesNew.component'; | ||
import { Self, Loading, Duplicate, Submitted } from '../../client/components/Info'; | ||
import Interaction from '../../client/components/Interaction'; | ||
import Navigation from '../../client/components/Navigation'; | ||
import Enzyme from 'enzyme'; | ||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
import sinon from 'sinon'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import jasmineEnzyme from 'jasmine-enzyme'; | ||
import * as api from '../../client/components/references.api'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
/** | ||
* This is a first React test suite with enzyme. | ||
* The enzyme configuration can be moved elsewhere (before all tests ever). | ||
*/ | ||
|
||
(function () { | ||
|
||
describe('<ReferencesNew />', () => { | ||
beforeEach(() => { | ||
jasmineEnzyme(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should not be possible to leave a reference to self', () => { | ||
const me = { | ||
_id: '123456', | ||
username: 'username' | ||
}; | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={me} userTo={me} />); | ||
expect(wrapper.find(Self)).toExist(); | ||
}); | ||
|
||
it('check whether the reference exists at the beginning', () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
const stub = sinon.stub(api, 'read'); | ||
stub.withArgs({ userFrom: userFrom._id, userTo: userTo._id }).returns(new Promise(() => {})); | ||
|
||
expect(stub.callCount).toBe(0); | ||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
expect(stub.callCount).toBe(1); | ||
expect(wrapper.find(Loading)).toExist(); | ||
}); | ||
|
||
it('can not leave a second reference', async () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
const stub = sinon.stub(api, 'read'); | ||
stub.withArgs({ userFrom: userFrom._id, userTo: userTo._id }).resolves([{ | ||
userFrom, userTo, public: false | ||
}]); | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
|
||
expect(wrapper.find(Duplicate)).not.toExist(); | ||
await null; | ||
expect(wrapper.find(Duplicate)).toExist(); | ||
expect(wrapper.find(Interaction)).not.toExist(); | ||
}); | ||
|
||
it('can leave a reference (reference form is available)', async () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
const stub = sinon.stub(api, 'read'); | ||
stub.withArgs({ userFrom: userFrom._id, userTo: userTo._id }).resolves([]); | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
expect(wrapper.find(Interaction)).not.toExist(); | ||
await null; | ||
expect(wrapper.find(Interaction)).toExist(); | ||
}); | ||
|
||
it('submit a reference', async () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
const spyCreate = sinon.spy(api, 'create'); | ||
const stubRead = sinon.stub(api, 'read'); | ||
stubRead.withArgs({ userFrom: userFrom._id, userTo: userTo._id }).resolves([]); | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
|
||
await null; | ||
|
||
expect(spyCreate.callCount).toBe(0); | ||
wrapper.setState({ | ||
reference: { | ||
interactions: { | ||
met: false, | ||
hostedMe: true, | ||
hostedThem: false | ||
}, | ||
recommend: 'yes' | ||
} | ||
}); | ||
|
||
const nav = wrapper.find(Navigation); | ||
nav.props().onSubmit(); | ||
|
||
expect(spyCreate.callCount).toBe(1); | ||
|
||
expect(spyCreate.calledOnceWith({ | ||
interactions: { | ||
met: false, | ||
hostedMe: true, | ||
hostedThem: false | ||
}, | ||
recommend: 'yes', | ||
userTo: userTo._id | ||
})).toBe(true); | ||
}); | ||
|
||
it('submit a report when recommend is no and user wants to send a report', async () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
const spyReport = sinon.spy(api, 'report'); | ||
const stubRead = sinon.stub(api, 'read'); | ||
const stubCreate = sinon.stub(api, 'create'); | ||
stubRead.withArgs({ userFrom: userFrom._id, userTo: userTo._id }).resolves([]); | ||
stubCreate.resolves(); | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
|
||
await null; | ||
|
||
expect(spyReport.callCount).toBe(0); | ||
wrapper.setState({ | ||
reference: { | ||
interactions: { | ||
met: false, | ||
hostedMe: true, | ||
hostedThem: false | ||
}, | ||
recommend: 'unknown' | ||
}, | ||
report: true, | ||
reportMessage: 'asdf' | ||
}); | ||
|
||
const nav = wrapper.find(Navigation); | ||
|
||
nav.props().onSubmit(); | ||
|
||
await null; | ||
|
||
expect(spyReport.callCount).toBe(0); | ||
|
||
wrapper.setState({ | ||
reference: { | ||
interactions: { | ||
met: true, | ||
hostedMe: true, | ||
hostedThem: true | ||
}, | ||
recommend: 'no' | ||
} | ||
}); | ||
|
||
nav.props().onSubmit(); | ||
|
||
await null; | ||
|
||
expect(spyReport.callCount).toBe(1); | ||
expect(spyReport.calledOnceWith(userTo, 'asdf')).toBe(true); | ||
}); | ||
|
||
it('give the information that the reference was submitted', async () => { | ||
const userFrom = { _id: '111111', username: 'userfrom' }; | ||
const userTo = { _id: '222222', username: 'userto' }; | ||
|
||
const wrapper = shallow(<ReferencesNew userFrom={userFrom} userTo={userTo} />); | ||
|
||
wrapper.setState({ | ||
isLoading: false, | ||
isSubmitted: true | ||
}); | ||
|
||
wrapper.update(); | ||
|
||
expect(wrapper.find(Submitted)).toExist(); | ||
}); | ||
}); | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters