From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- .../frontend/__tests__/SingleItem.test.js | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 finished-application/frontend/__tests__/SingleItem.test.js (limited to 'finished-application/frontend/__tests__/SingleItem.test.js') diff --git a/finished-application/frontend/__tests__/SingleItem.test.js b/finished-application/frontend/__tests__/SingleItem.test.js new file mode 100644 index 0000000..aeb8554 --- /dev/null +++ b/finished-application/frontend/__tests__/SingleItem.test.js @@ -0,0 +1,67 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import SingleItem, { SINGLE_ITEM_QUERY } from '../components/SingleItem'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { fakeItem, fakeUser } from '../lib/testUtils'; +import { CURRENT_USER_QUERY } from '../components/User'; + +const data = { + items: [fakeItem()], +}; + +describe('', () => { + it('Renders with Proper Data', async () => { + const mocks = [ + { + request: { query: SINGLE_ITEM_QUERY, variables: { id: '123' } }, + result: { data }, + }, + { + request: { query: CURRENT_USER_QUERY }, + result: { data: { me: fakeUser() } }, + }, + ]; + const wrapper = mount( + + + + ); + // Check for loading state + expect(wrapper.text()).toContain('Loading...'); + + // wait for response and refresh + await wait(55); + wrapper.update(); + // Grab the piece we want + expect(toJSON(wrapper.find('h2'))).toMatchSnapshot(); + expect(toJSON(wrapper.find('img'))).toMatchSnapshot(); + expect(toJSON(wrapper.find('p'))).toMatchSnapshot(); + }); + + it('Errors with a not found Item', async () => { + const mocks = [ + { + request: { query: SINGLE_ITEM_QUERY, variables: { id: '123' } }, + delay: 50, + // we can return data and an error + result: { data, errors: [{ message: 'Item not found!' }] }, + // or just an error! + // error: new Error('offline!'), + }, + ]; + const wrapper = mount( + + + + ); + + // wait for response + await wait(55); + wrapper.update(); + + const Item = wrapper.find('[data-test="graphql-error"]'); + expect(toJSON(Item)).toMatchSnapshot(); + }); +}); -- cgit v1.3