From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- .../frontend/__tests__/RemoveFromCart.test.js | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 finished-application/frontend/__tests__/RemoveFromCart.test.js (limited to 'finished-application/frontend/__tests__/RemoveFromCart.test.js') diff --git a/finished-application/frontend/__tests__/RemoveFromCart.test.js b/finished-application/frontend/__tests__/RemoveFromCart.test.js new file mode 100644 index 0000000..34229a5 --- /dev/null +++ b/finished-application/frontend/__tests__/RemoveFromCart.test.js @@ -0,0 +1,78 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import toJSON from 'enzyme-to-json'; +import { ApolloConsumer } from 'react-apollo'; +import { MockedProvider } from 'react-apollo/test-utils'; +import wait from 'waait'; +import RemoveFromCart, { REMOVE_FROM_CART_MUTATION } from '../components/RemoveFromCart'; +import { fakeCartItem, fakeUser } from '../lib/testUtils'; +import { CURRENT_USER_QUERY } from '../components/User'; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [fakeCartItem({ id: 'abc123' })], + orders: [], + }, + }, + }, + }, + { + request: { + // This is the mutation + query: REMOVE_FROM_CART_MUTATION, + variables: { id: 'abc123' }, + }, + result: { + data: { + removeFromCart: { + __typename: 'CartItem', + id: 'abc123', + }, + }, + }, + }, +]; + +describe('', () => { + it('renders and matches snapshot', () => { + const wrapper = mount( + + + + ); + expect(toJSON(wrapper.find('button'))).toMatchSnapshot(); + }); + + it('removes an item from the cart', async () => { + // 1. Grab a copy of the current client + let apolloClient; + // const wrapper = mountWithApollo(); + const wrapper = mount( + + + {client => { + apolloClient = client; + return ; + }} + + + ); + // Fire Off that first mock + await apolloClient.query({ query: CURRENT_USER_QUERY }); + // check if it worked + const { me } = apolloClient.readQuery({ query: CURRENT_USER_QUERY }); + expect(me.cart).toHaveLength(1); + expect(me.cart[0].item.price).toBe(5000); + + // simulate a click, which will fire REMOVE_FROM_CART mock + wrapper.find('button').simulate('click'); + await wait(); + const { me: me2 } = apolloClient.readQuery({ query: CURRENT_USER_QUERY }); + expect(me2.cart).toHaveLength(0); + }); +}); -- cgit v1.3