From 217cfed064989816fbffcb1e285134c8e03ef5c7 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Tue, 20 Mar 2018 14:13:18 -0400 Subject: tests --- frontend/__tests__/EditUser.test.js | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 frontend/__tests__/EditUser.test.js (limited to 'frontend/__tests__/EditUser.test.js') 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('', () => { + it('renders', () => { + const wrapper = shallow( {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('asks you to log in when you arent logged in', () => { + const wrapper = shallow( {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('displays changes', () => { + const wrapper = shallow( {}} />); + 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(); + 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( {}} />); + wrapper.simulate('submit', { preventDefault() {} }); + await wait(0); + expect(currentUserWithSpy.refetch).toHaveBeenCalled(); + }); +}); -- cgit v1.3