diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-03-20 14:13:18 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-03-20 14:13:18 -0400 |
| commit | 217cfed064989816fbffcb1e285134c8e03ef5c7 (patch) | |
| tree | 0e9a3e43641ad839f5c141c32a3375d7d305022c /frontend/__tests__/EditUser.test.js | |
| parent | 43890f9940326702dac1769eb0ded8651db6bad6 (diff) | |
tests
Diffstat (limited to 'frontend/__tests__/EditUser.test.js')
| -rw-r--r-- | frontend/__tests__/EditUser.test.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/frontend/__tests__/EditUser.test.js b/frontend/__tests__/EditUser.test.js new file mode 100644 index 0000000..08dd6f6 --- /dev/null +++ b/frontend/__tests__/EditUser.test.js @@ -0,0 +1,53 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { EditUser } from '../components/EditUser'; + +const wait = amount => new Promise(resolve => setTimeout(resolve, amount)); + +const currentUser = { + me: { + name: 'Wes Bos', + }, + refetch() {}, +}; + +describe('<EditUser/>', () => { + it('renders', () => { + const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={() => {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('asks you to log in when you arent logged in', () => { + const wrapper = shallow(<EditUser currentUser={{}} updateUser={() => {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('displays changes', () => { + const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={() => {}} />); + const nameInput = wrapper.find('[name="name"]'); + nameInput.simulate('change', { target: { name: 'name', value: 'Scott' } }); + const diff = wrapper.find('pre[data-test="change"]'); + expect(diff.text()).toBe('{"name":"Scott"}'); + }); + + it('calls update user when form is submitted', () => { + const updateUserSpy = jest.fn(); + const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={updateUserSpy} />); + const nameInput = wrapper.find('[name="name"]'); + nameInput.simulate('change', { target: { name: 'name', value: 'Scott' } }); + wrapper.simulate('submit', { preventDefault() {} }); + expect(updateUserSpy).toHaveBeenCalledWith({ variables: { name: 'Scott' } }); + }); + + it('refetches the current user after an update', async () => { + const currentUserWithSpy = { + me: { name: 'wes' }, + refetch: jest.fn(), + }; + const wrapper = shallow(<EditUser currentUser={currentUserWithSpy} updateUser={() => {}} />); + wrapper.simulate('submit', { preventDefault() {} }); + await wait(0); + expect(currentUserWithSpy.refetch).toHaveBeenCalled(); + }); +}); |
