From 331da87bd53bc1fb79063d142764c75df9f32170 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 14 Mar 2018 12:19:54 -0400 Subject: tests --- frontend/__tests__/CreateItem.test.js | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 frontend/__tests__/CreateItem.test.js (limited to 'frontend/__tests__/CreateItem.test.js') diff --git a/frontend/__tests__/CreateItem.test.js b/frontend/__tests__/CreateItem.test.js new file mode 100644 index 0000000..0994d9c --- /dev/null +++ b/frontend/__tests__/CreateItem.test.js @@ -0,0 +1,85 @@ +import React from 'react'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { CreateItem } from '../components/CreateItem'; + +// Wait func +const wait = amount => new Promise(resolve => setTimeout(resolve, amount)); + +const dogImage = 'https://dog.com/dog.jpg'; + +// Mock fetch to resolve data +global.fetch = jest.fn().mockResolvedValue({ + json: () => ({ + secure_url: dogImage, + eager: [{ secure_url: dogImage }], + }), +}); + +const fakeItem = { + title: 'Testing', + description: 'This is a really nice item', + image: dogImage, + largeImage: dogImage, + price: 50000, +}; + +describe('', () => { + it('renders the form out', () => { + const createItemMutation = jest.fn(); + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('uploads a file when changed', async () => { + const createItemMutation = jest.fn(); + const wrapper = shallow(); + + const input = wrapper.find('input[type="file"]'); + // internally this does some setState() calls + input.simulate('change', { currentTarget: { files: ['fakedog.jpg'] } }); + await wait(0); + expect(wrapper.state('image')).toEqual(dogImage); + expect(wrapper.state('largeImage')).toEqual(dogImage); + }); + + it('handles state updating', async () => { + const createItemMutation = jest.fn(); + const wrapper = shallow(); + + wrapper.find('#title').simulate('change', { target: { value: 'Testing', name: 'title' } }); + wrapper.find('#price').simulate('change', { + target: { + value: 50000, + name: 'price', + type: 'number', + }, + }); + wrapper.find('#description').simulate('change', { + target: { + value: 'This is a really nice item', + name: 'description', + }, + }); + expect(wrapper.state().item).toMatchObject({ + title: 'Testing', + description: 'This is a really nice item', + image: '', + largeImage: '', + price: 50000, + }); + }); + + it('creates an item with all the inputs', () => { + const createItemMutation = jest.fn(() => Promise.resolve({})); + const wrapper = shallow(); + wrapper.setState({ item: fakeItem }); + const form = wrapper.find('Form'); + // console.log(form.debug()); + form.simulate('submit', { + preventDefault() {}, + }); + expect(createItemMutation).toHaveBeenCalled(); + expect(createItemMutation).toHaveBeenCalledWith({ variables: fakeItem }); + }); +}); -- cgit v1.3