From 8a5825d52271d712ec7c8348447d433067e13ef2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 13 Sep 2018 14:18:56 -0400 Subject: DONE --- .../65/frontend/__tests__/AddToCart.test.js | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 stepped-solutions/65/frontend/__tests__/AddToCart.test.js (limited to 'stepped-solutions/65/frontend/__tests__/AddToCart.test.js') diff --git a/stepped-solutions/65/frontend/__tests__/AddToCart.test.js b/stepped-solutions/65/frontend/__tests__/AddToCart.test.js new file mode 100755 index 0000000..cfc2e3e --- /dev/null +++ b/stepped-solutions/65/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'); + }); +}); -- cgit v1.3