From 8a5825d52271d712ec7c8348447d433067e13ef2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 13 Sep 2018 14:18:56 -0400 Subject: DONE --- .../FINISHED/frontend/__tests__/AddToCart.test.js | 97 +++++++++++++++++ .../FINISHED/frontend/__tests__/Cart.test.js | 39 +++++++ .../FINISHED/frontend/__tests__/CartCount.test.js | 21 ++++ .../FINISHED/frontend/__tests__/CreateItem.test.js | 112 +++++++++++++++++++ .../FINISHED/frontend/__tests__/Item.test.js | 39 +++++++ .../FINISHED/frontend/__tests__/Nav.test.js | 76 +++++++++++++ .../FINISHED/frontend/__tests__/Order.test.js | 27 +++++ .../FINISHED/frontend/__tests__/Pagination.test.js | 89 ++++++++++++++++ .../frontend/__tests__/PleaseSignIn.test.js | 51 +++++++++ .../frontend/__tests__/RemoveFromCart.test.js | 67 ++++++++++++ .../frontend/__tests__/RequestReset.test.js | 46 ++++++++ .../FINISHED/frontend/__tests__/Signup.test.js | 80 ++++++++++++++ .../FINISHED/frontend/__tests__/SingleItem.test.js | 57 ++++++++++ .../frontend/__tests__/TakeMyMoney.test.js | 98 +++++++++++++++++ .../__tests__/__snapshots__/AddToCart.test.js.snap | 11 ++ .../__tests__/__snapshots__/Cart.test.js.snap | 32 ++++++ .../__tests__/__snapshots__/CartCount.test.js.snap | 79 ++++++++++++++ .../__snapshots__/CreateItem.test.js.snap | 77 ++++++++++++++ .../__tests__/__snapshots__/Item.test.js.snap | 58 ++++++++++ .../__tests__/__snapshots__/Nav.test.js.snap | 37 +++++++ .../__tests__/__snapshots__/Order.test.js.snap | 118 +++++++++++++++++++++ .../__snapshots__/Pagination.test.js.snap | 67 ++++++++++++ .../__snapshots__/RemoveFromCart.test.js.snap | 12 +++ .../__snapshots__/RequestReset.test.js.snap | 39 +++++++ .../__tests__/__snapshots__/Signup.test.js.snap | 62 +++++++++++ .../__snapshots__/SingleItem.test.js.snap | 32 ++++++ .../__snapshots__/TakeMyMoney.test.js.snap | 67 ++++++++++++ .../frontend/__tests__/formatMoney.test.js | 23 ++++ .../FINISHED/frontend/__tests__/mocking.test.js | 36 +++++++ .../FINISHED/frontend/__tests__/sample.test.js | 19 ++++ 30 files changed, 1668 insertions(+) create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/AddToCart.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Cart.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/CartCount.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/CreateItem.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Item.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Nav.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Order.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Pagination.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/PleaseSignIn.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/RemoveFromCart.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/RequestReset.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/Signup.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/SingleItem.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/TakeMyMoney.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/AddToCart.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Cart.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/CartCount.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/CreateItem.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Item.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Nav.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Order.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Pagination.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/RequestReset.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/Signup.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/SingleItem.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/__snapshots__/TakeMyMoney.test.js.snap create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/formatMoney.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/mocking.test.js create mode 100644 stepped-solutions/FINISHED/frontend/__tests__/sample.test.js (limited to 'stepped-solutions/FINISHED/frontend/__tests__') diff --git a/stepped-solutions/FINISHED/frontend/__tests__/AddToCart.test.js b/stepped-solutions/FINISHED/frontend/__tests__/AddToCart.test.js new file mode 100644 index 0000000..cfc2e3e --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/AddToCart.test.js @@ -0,0 +1,97 @@ +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { ApolloConsumer } from 'react-apollo'; +import AddToCart, { ADD_TO_CART_MUTATION } from '../components/AddToCart'; +import { CURRENT_USER_QUERY } from '../components/User'; +import { fakeUser, fakeCartItem } from '../lib/testUtils'; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [], + }, + }, + }, + }, + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem()], + }, + }, + }, + }, + { + request: { query: ADD_TO_CART_MUTATION, variables: { id: 'abc123' } }, + result: { + data: { + addToCart: { + ...fakeCartItem(), + quantity: 1, + }, + }, + }, + }, +]; + +describe('', () => { + it('renders and matches the snap shot', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + expect(toJSON(wrapper.find('button'))).toMatchSnapshot(); + }); + + it('adds an item to cart when clicked', async () => { + let apolloClient; + const wrapper = mount( + + + {client => { + apolloClient = client; + return ; + }} + + + ); + await wait(); + wrapper.update(); + const { data: { me } } = await apolloClient.query({ query: CURRENT_USER_QUERY }); + // console.log(me); + expect(me.cart).toHaveLength(0); + // add an item to the cart + wrapper.find('button').simulate('click'); + await wait(); + // check if the item is in the cart + const { data: { me: me2 } } = await apolloClient.query({ query: CURRENT_USER_QUERY }); + expect(me2.cart).toHaveLength(1); + expect(me2.cart[0].id).toBe('omg123'); + expect(me2.cart[0].quantity).toBe(3); + }); + + it('changes from add to adding when clicked', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + expect(wrapper.text()).toContain('Add To Cart'); + wrapper.find('button').simulate('click'); + expect(wrapper.text()).toContain('Adding To Cart'); + }); +}); diff --git a/stepped-solutions/FINISHED/frontend/__tests__/Cart.test.js b/stepped-solutions/FINISHED/frontend/__tests__/Cart.test.js new file mode 100644 index 0000000..b8afc88 --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/Cart.test.js @@ -0,0 +1,39 @@ +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import { MockedProvider } from 'react-apollo/test-utils'; +import Cart, { LOCAL_STATE_QUERY } from '../components/Cart'; +import { CURRENT_USER_QUERY } from '../components/User'; +import { fakeUser, fakeCartItem } from '../lib/testUtils'; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem()], + }, + }, + }, + }, + { + request: { query: LOCAL_STATE_QUERY }, + result: { data: { cartOpen: true } }, + }, +]; + +describe('', () => { + it('renders and matches snappy', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + expect(toJSON(wrapper.find('header'))).toMatchSnapshot(); + expect(wrapper.find('CartItem')).toHaveLength(1); + }); +}); diff --git a/stepped-solutions/FINISHED/frontend/__tests__/CartCount.test.js b/stepped-solutions/FINISHED/frontend/__tests__/CartCount.test.js new file mode 100644 index 0000000..8ace3da --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/CartCount.test.js @@ -0,0 +1,21 @@ +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import CartCount from '../components/CartCount'; + +describe('', () => { + it('renders', () => { + shallow(); + }); + + it('matches the snapshot', () => { + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + + it('updates via props', () => { + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + wrapper.setProps({ count: 10 }); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); +}); diff --git a/stepped-solutions/FINISHED/frontend/__tests__/CreateItem.test.js b/stepped-solutions/FINISHED/frontend/__tests__/CreateItem.test.js new file mode 100644 index 0000000..6e88d94 --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/CreateItem.test.js @@ -0,0 +1,112 @@ +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import Router from 'next/router'; +import { MockedProvider } from 'react-apollo/test-utils'; +import CreateItem, { CREATE_ITEM_MUTATION } from '../components/CreateItem'; +import { fakeItem } from '../lib/testUtils'; + +const dogImage = 'https://dog.com/dog.jpg'; + +// mock the global fetch API +global.fetch = jest.fn().mockResolvedValue({ + json: () => ({ + secure_url: dogImage, + eager: [{ secure_url: dogImage }], + }), +}); + +describe('', () => { + it('renders and matches snapshot', async () => { + const wrapper = mount( + + + + ); + const form = wrapper.find('form[data-test="form"]'); + expect(toJSON(form)).toMatchSnapshot(); + }); + + it('uploads a file when changed', async () => { + const wrapper = mount( + + + + ); + const input = wrapper.find('input[type="file"]'); + input.simulate('change', { target: { files: ['fakedog.jpg'] } }); + await wait(); + const component = wrapper.find('CreateItem').instance(); + expect(component.state.image).toEqual(dogImage); + expect(component.state.largeImage).toEqual(dogImage); + expect(global.fetch).toHaveBeenCalled(); + global.fetch.mockReset(); + }); + + it('handles state updating', async () => { + const wrapper = mount( + + + + ); + 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.find('CreateItem').instance().state).toMatchObject({ + title: 'Testing', + price: 50000, + description: 'This is a really nice item', + }); + }); + it('creates an item when the form is submitted', async () => { + const item = fakeItem(); + const mocks = [ + { + request: { + query: CREATE_ITEM_MUTATION, + variables: { + title: item.title, + description: item.description, + image: '', + largeImage: '', + price: item.price, + }, + }, + result: { + data: { + createItem: { + ...fakeItem, + id: 'abc123', + __typename: 'Item', + }, + }, + }, + }, + ]; + + const wrapper = mount( + + + + ); + // simulate someone filling out the form + wrapper.find('#title').simulate('change', { target: { value: item.title, name: 'title' } }); + wrapper + .find('#price') + .simulate('change', { target: { value: item.price, name: 'price', type: 'number' } }); + wrapper + .find('#description') + .simulate('change', { target: { value: item.description, name: 'description' } }); + // mock the router + Router.router = { push: jest.fn() }; + wrapper.find('form').simulate('submit'); + await wait(50); + expect(Router.router.push).toHaveBeenCalled(); + expect(Router.router.push).toHaveBeenCalledWith({ pathname: '/item', query: { id: 'abc123' } }); + }); +}); diff --git a/stepped-solutions/FINISHED/frontend/__tests__/Item.test.js b/stepped-solutions/FINISHED/frontend/__tests__/Item.test.js new file mode 100644 index 0000000..692b913 --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/Item.test.js @@ -0,0 +1,39 @@ +import ItemComponent from '../components/Item'; +import { shallow, mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; + +const fakeItem = { + id: 'ABC123', + title: 'A Cool Item', + price: 4000, + description: 'This item is really cool!', + image: 'dog.jpg', + largeImage: 'largedog.jpg', +}; + +describe('', () => { + it('renders and matches the snapshot', () => { + const wrapper = shallow(); + expect(toJSON(wrapper)).toMatchSnapshot(); + }); + // it('renders the image properly', () => { + // const wrapper = shallow(); + // const img = wrapper.find('img'); + // expect(img.props().src).toBe(fakeItem.image); + // expect(img.props().alt).toBe(fakeItem.title); + // }); + // it('renders the pricetag and title', () => { + // const wrapper = shallow(); + // const PriceTag = wrapper.find('PriceTag'); + // expect(PriceTag.children().text()).toBe('$50'); + // expect(wrapper.find('Title a').text()).toBe(fakeItem.title); + // }); + // it('renders out the buttons properly', () => { + // const wrapper = shallow(); + // const buttonList = wrapper.find('.buttonList'); + // expect(buttonList.children()).toHaveLength(3); + // expect(buttonList.find('Link')).toHaveLength(1); + // expect(buttonList.find('AddToCart').exists()).toBe(true); + // expect(buttonList.find('DeleteItem').exists()).toBe(true); + // }); +}); diff --git a/stepped-solutions/FINISHED/frontend/__tests__/Nav.test.js b/stepped-solutions/FINISHED/frontend/__tests__/Nav.test.js new file mode 100644 index 0000000..d30cb39 --- /dev/null +++ b/stepped-solutions/FINISHED/frontend/__tests__/Nav.test.js @@ -0,0 +1,76 @@ +import { mount } from 'enzyme'; +import wait from 'waait'; +import toJSON from 'enzyme-to-json'; +import Nav from '../components/Nav'; +import { CURRENT_USER_QUERY } from '../components/User'; +import { MockedProvider } from 'react-apollo/test-utils'; +import { fakeUser, fakeCartItem } from '../lib/testUtils'; + +const notSignedInMocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { data: { me: null } }, + }, +]; + +const signedInMocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { data: { me: fakeUser() } }, + }, +]; + +const signedInMocksWithCartItems = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem(), fakeCartItem(), fakeCartItem()], + }, + }, + }, + }, +]; + +describe('