From 331da87bd53bc1fb79063d142764c75df9f32170 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 14 Mar 2018 12:19:54 -0400 Subject: tests --- frontend/.babelrc | 9 - frontend/__tests__/CartCount.test.js | 21 +++ frontend/__tests__/CreateItem.test.js | 85 +++++++++ frontend/__tests__/Item.test.js | 31 ++++ frontend/__tests__/Nav.test.js | 35 ++++ frontend/__tests__/Pagination.test.js | 46 +++++ .../__tests__/__snapshots__/CartCount.test.js.snap | 201 +++++++++++++++++++++ .../__snapshots__/CreateItem.test.js.snap | 58 ++++++ frontend/__tests__/__snapshots__/Item.test.js.snap | 60 ++++++ frontend/__tests__/__snapshots__/Nav.test.js.snap | 69 +++++++ .../__snapshots__/Pagination.test.js.snap | 67 +++++++ frontend/__tests__/formatMoney.test.js | 21 +++ frontend/components/AddToCart.js | 41 +---- frontend/components/CartCount.js | 1 + frontend/components/Count.js | 4 +- frontend/components/CreateItem.js | 70 +++---- frontend/components/Item.js | 5 +- frontend/components/Items.js | 3 +- frontend/components/Nav.js | 9 +- frontend/components/Pagination.js | 32 +++- frontend/components/Search.js | 10 +- frontend/components/SingleItem.js | 18 +- frontend/components/styles/Form.js | 3 + frontend/enhancers/enhancers.js | 2 +- frontend/lib/formatMoney.js | 12 +- frontend/package.json | 20 +- 26 files changed, 815 insertions(+), 118 deletions(-) delete mode 100644 frontend/.babelrc create mode 100644 frontend/__tests__/CartCount.test.js create mode 100644 frontend/__tests__/CreateItem.test.js create mode 100644 frontend/__tests__/Item.test.js create mode 100644 frontend/__tests__/Nav.test.js create mode 100644 frontend/__tests__/Pagination.test.js create mode 100644 frontend/__tests__/__snapshots__/CartCount.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/CreateItem.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/Item.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/Nav.test.js.snap create mode 100644 frontend/__tests__/__snapshots__/Pagination.test.js.snap create mode 100644 frontend/__tests__/formatMoney.test.js diff --git a/frontend/.babelrc b/frontend/.babelrc deleted file mode 100644 index d9c3f91..0000000 --- a/frontend/.babelrc +++ /dev/null @@ -1,9 +0,0 @@ -{ - "presets": ["next/babel"], - "plugins": [ - [ - "styled-components", - { "ssr": true, "displayName": true, "preprocess": false } - ] - ] -} diff --git a/frontend/__tests__/CartCount.test.js b/frontend/__tests__/CartCount.test.js new file mode 100644 index 0000000..f3c4978 --- /dev/null +++ b/frontend/__tests__/CartCount.test.js @@ -0,0 +1,21 @@ +import React from 'react'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { CartCount } from '../components/CartCount'; + +describe('', () => { + it('renders okay', () => { + shallow(); + }); + + it('matches snapshot', () => { + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + it('updates via props', () => { + const wrapper = mount(); + expect(toJSON(wrapper)).toMatchSnapshot(); + wrapper.setProps({ count: 10 }); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); +}); 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 }); + }); +}); diff --git a/frontend/__tests__/Item.test.js b/frontend/__tests__/Item.test.js new file mode 100644 index 0000000..2990b1f --- /dev/null +++ b/frontend/__tests__/Item.test.js @@ -0,0 +1,31 @@ +/* eslint-env jest */ + +import React from 'react'; +import Enzyme, { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { ItemComponent } from '../components/Item'; + +const fakeItem = { + id: 'ABC123', + title: 'A Cool Item', + price: 5000, + description: 'This item is really cool!', + image: 'dog.jpg', + largeImage: 'largedog.jpg', +}; + +describe('', () => { + it('Renders an item', () => { + const removeItem = jest.fn(); + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + it('handles button clicks', async () => { + const removeItem = jest.fn(); + global.confirm = jest.fn().mockReturnValue(true); + const wrapper = shallow(); + wrapper.find('button').simulate('click'); + expect(removeItem).toHaveBeenCalled(); + expect(global.confirm).toHaveBeenCalled(); + }); +}); diff --git a/frontend/__tests__/Nav.test.js b/frontend/__tests__/Nav.test.js new file mode 100644 index 0000000..49d16d6 --- /dev/null +++ b/frontend/__tests__/Nav.test.js @@ -0,0 +1,35 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { Nav } from '../components/Nav'; + +const currentUserLoggedOut = { + refetch() {}, +}; + +const currentUser = { + me: {}, + refetch() {}, +}; + +describe('', () => { + it('renders', () => { + shallow(