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 +++++++++++++++++++++ frontend/__tests__/RemoveFromCart.test.js | 18 ++++++++ frontend/__tests__/ResetRequest.test.js | 33 +++++++++++++ frontend/__tests__/SingleItem.test.js | 23 +++++++++ .../__tests__/__snapshots__/CartList.test.js.snap | 33 ------------- .../__tests__/__snapshots__/EditUser.test.js.snap | 44 ++++++++++++++++++ .../__snapshots__/RemoveFromCart.test.js.snap | 10 ++++ .../__snapshots__/ResetRequest.test.js.snap | 54 ++++++++++++++++++++++ .../__snapshots__/SingleItem.test.js.snap | 17 +++++++ 9 files changed, 252 insertions(+), 33 deletions(-) create mode 100644 frontend/__tests__/EditUser.test.js create mode 100644 frontend/__tests__/RemoveFromCart.test.js create mode 100644 frontend/__tests__/ResetRequest.test.js create mode 100644 frontend/__tests__/SingleItem.test.js delete mode 100644 frontend/__tests__/__snapshots__/CartList.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/EditUser.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/ResetRequest.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/SingleItem.test.js.snap (limited to 'frontend/__tests__') 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(); + }); +}); diff --git a/frontend/__tests__/RemoveFromCart.test.js b/frontend/__tests__/RemoveFromCart.test.js new file mode 100644 index 0000000..da03f0c --- /dev/null +++ b/frontend/__tests__/RemoveFromCart.test.js @@ -0,0 +1,18 @@ +import React from 'react'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { RemoveFromCart } from '../components/RemoveFromCart'; + +describe('', () => { + it('renders and matches snapshot', () => { + const wrapper = shallow( {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it("runs a function when it's clicked", () => { + const removeSpy = jest.fn(); + const wrapper = shallow(); + wrapper.simulate('click'); + expect(removeSpy).toHaveBeenCalledWith({ variables: { id: 'abc123' } }); + }); +}); diff --git a/frontend/__tests__/ResetRequest.test.js b/frontend/__tests__/ResetRequest.test.js new file mode 100644 index 0000000..bd8a934 --- /dev/null +++ b/frontend/__tests__/ResetRequest.test.js @@ -0,0 +1,33 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { ResetRequest } from '../components/ResetRequest'; + +const wait = amount => new Promise(resolve => setTimeout(resolve, amount)); + +describe('', () => { + it('renders and matches snapshot', () => { + const wrapper = shallow( {}} />); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('handles the reset request', async () => { + const resetSpy = jest.fn(() => Promise.resolve({})); + const wrapper = shallow(); + const input = wrapper.find('input[name="email"]'); + input.simulate('change', { target: { name: 'email', value: 'wesbos@gmail.com' } }); + wrapper.simulate('submit', { preventDefault() {} }); + expect(resetSpy).toHaveBeenCalledWith({ variables: { email: 'wesbos@gmail.com' } }); + }); + + it('displays errors', async () => { + const resetSpy = jest.fn(() => Promise.resolve({ errors: [{ message: 'Error!!!' }] })); + const wrapper = shallow(); + const input = wrapper.find('input[name="email"]'); + input.simulate('change', { target: { name: 'email', value: 'wesbos@gmail.com' } }); + wrapper.simulate('submit', { preventDefault() {} }); + await wait(0); + wrapper.update(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); +}); diff --git a/frontend/__tests__/SingleItem.test.js b/frontend/__tests__/SingleItem.test.js new file mode 100644 index 0000000..e39431d --- /dev/null +++ b/frontend/__tests__/SingleItem.test.js @@ -0,0 +1,23 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { SingleItem } from '../components/SingleItem'; + +const wait = amount => new Promise(resolve => setTimeout(resolve, amount)); + +const findItem = { + items: [ + { + largeImage: 'dog.jpg', + title: 'Sneakers', + description: 'A cool set of shoes', + }, + ], +}; + +describe('', () => { + it('renders', () => { + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); +}); diff --git a/frontend/__tests__/__snapshots__/CartList.test.js.snap b/frontend/__tests__/__snapshots__/CartList.test.js.snap deleted file mode 100644 index 1f3cca5..0000000 --- a/frontend/__tests__/__snapshots__/CartList.test.js.snap +++ /dev/null @@ -1,33 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[` renders and matches snapshot 1`] = ` - - Fake Item -
-

- Fake Item -

-

- $1,000 - — - - 20 - × - $50 - each - -

-
- -
-`; diff --git a/frontend/__tests__/__snapshots__/EditUser.test.js.snap b/frontend/__tests__/__snapshots__/EditUser.test.js.snap new file mode 100644 index 0000000..de5df25 --- /dev/null +++ b/frontend/__tests__/__snapshots__/EditUser.test.js.snap @@ -0,0 +1,44 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` asks you to log in when you arent logged in 1`] = ` +

+ You must be logged in +

+`; + +exports[` renders 1`] = ` +
+ + + + me: + +
+    "Wes Bos"
+  
+ + Change: + +
+    {}
+  
+
+`; diff --git a/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap b/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap new file mode 100644 index 0000000..b25bcc9 --- /dev/null +++ b/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders and matches snapshot 1`] = ` + + × + +`; diff --git a/frontend/__tests__/__snapshots__/ResetRequest.test.js.snap b/frontend/__tests__/__snapshots__/ResetRequest.test.js.snap new file mode 100644 index 0000000..ef7e9f9 --- /dev/null +++ b/frontend/__tests__/__snapshots__/ResetRequest.test.js.snap @@ -0,0 +1,54 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` displays errors 1`] = ` +
+

+ Error!!! +

+ + +
+`; + +exports[` renders and matches snapshot 1`] = ` +
+ + +
+`; diff --git a/frontend/__tests__/__snapshots__/SingleItem.test.js.snap b/frontend/__tests__/__snapshots__/SingleItem.test.js.snap new file mode 100644 index 0000000..173fb95 --- /dev/null +++ b/frontend/__tests__/__snapshots__/SingleItem.test.js.snap @@ -0,0 +1,17 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders 1`] = ` +
+ Sneakers +

+ Viewing + Sneakers +

+

+ A cool set of shoes +

+
+`; -- cgit v1.3