|
| 1 | +import { mount } from 'enzyme'; |
| 2 | +import wait from 'waait'; |
| 3 | +import toJSON from 'enzyme-to-json'; |
| 4 | +import Router from 'next/router'; |
| 5 | +import { MockedProvider } from 'react-apollo/test-utils'; |
| 6 | +import CreateItem, { CREATE_ITEM_MUTATION } from '../components/CreateItem'; |
| 7 | +import { fakeItem } from '../lib/testUtils'; |
| 8 | + |
| 9 | +const dogImage = 'https://dog.com/dog.jpg'; |
| 10 | + |
| 11 | +// mock the global fetch API |
| 12 | +global.fetch = jest.fn().mockResolvedValue({ |
| 13 | + json: () => ({ |
| 14 | + secure_url: dogImage, |
| 15 | + eager: [{ secure_url: dogImage }], |
| 16 | + }), |
| 17 | +}); |
| 18 | + |
| 19 | +describe('<CreateItem/>', () => { |
| 20 | + it('renders and matches snapshot', async () => { |
| 21 | + const wrapper = mount( |
| 22 | + <MockedProvider> |
| 23 | + <CreateItem /> |
| 24 | + </MockedProvider> |
| 25 | + ); |
| 26 | + const form = wrapper.find('form[data-test="form"]'); |
| 27 | + expect(toJSON(form)).toMatchSnapshot(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('uploads a file when changed', async () => { |
| 31 | + const wrapper = mount( |
| 32 | + <MockedProvider> |
| 33 | + <CreateItem /> |
| 34 | + </MockedProvider> |
| 35 | + ); |
| 36 | + const input = wrapper.find('input[type="file"]'); |
| 37 | + input.simulate('change', { target: { files: ['fakedog.jpg'] } }); |
| 38 | + await wait(); |
| 39 | + const component = wrapper.find('CreateItem').instance(); |
| 40 | + expect(component.state.image).toEqual(dogImage); |
| 41 | + expect(component.state.largeImage).toEqual(dogImage); |
| 42 | + expect(global.fetch).toHaveBeenCalled(); |
| 43 | + global.fetch.mockReset(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles state updating', async () => { |
| 47 | + const wrapper = mount( |
| 48 | + <MockedProvider> |
| 49 | + <CreateItem /> |
| 50 | + </MockedProvider> |
| 51 | + ); |
| 52 | + wrapper.find('#title').simulate('change', { target: { value: 'Testing', name: 'title' } }); |
| 53 | + wrapper |
| 54 | + .find('#price') |
| 55 | + .simulate('change', { target: { value: 50000, name: 'price', type: 'number' } }); |
| 56 | + wrapper |
| 57 | + .find('#description') |
| 58 | + .simulate('change', { target: { value: 'This is a really nice item', name: 'description' } }); |
| 59 | + |
| 60 | + expect(wrapper.find('CreateItem').instance().state).toMatchObject({ |
| 61 | + title: 'Testing', |
| 62 | + price: 50000, |
| 63 | + description: 'This is a really nice item', |
| 64 | + }); |
| 65 | + }); |
| 66 | + it('creates an item when the form is submitted', async () => { |
| 67 | + const item = fakeItem(); |
| 68 | + const mocks = [ |
| 69 | + { |
| 70 | + request: { |
| 71 | + query: CREATE_ITEM_MUTATION, |
| 72 | + variables: { |
| 73 | + title: item.title, |
| 74 | + description: item.description, |
| 75 | + image: '', |
| 76 | + largeImage: '', |
| 77 | + price: item.price, |
| 78 | + }, |
| 79 | + }, |
| 80 | + result: { |
| 81 | + data: { |
| 82 | + createItem: { |
| 83 | + ...fakeItem, |
| 84 | + id: 'abc123', |
| 85 | + __typename: 'Item', |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ]; |
| 91 | + |
| 92 | + const wrapper = mount( |
| 93 | + <MockedProvider mocks={mocks}> |
| 94 | + <CreateItem /> |
| 95 | + </MockedProvider> |
| 96 | + ); |
| 97 | + // simulate someone filling out the form |
| 98 | + wrapper.find('#title').simulate('change', { target: { value: item.title, name: 'title' } }); |
| 99 | + wrapper |
| 100 | + .find('#price') |
| 101 | + .simulate('change', { target: { value: item.price, name: 'price', type: 'number' } }); |
| 102 | + wrapper |
| 103 | + .find('#description') |
| 104 | + .simulate('change', { target: { value: item.description, name: 'description' } }); |
| 105 | + // mock the router |
| 106 | + Router.router = { push: jest.fn() }; |
| 107 | + wrapper.find('form').simulate('submit'); |
| 108 | + await wait(50); |
| 109 | + expect(Router.router.push).toHaveBeenCalled(); |
| 110 | + expect(Router.router.push).toHaveBeenCalledWith({ pathname: '/item', query: { id: 'abc123' } }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments