From 952c37536aeb69fb9edac15a9d260890d4b1c50c Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 4 May 2018 11:41:28 -0400 Subject: Add and remove mutations --- frontend/__tests__/AddToCart.test.js | 86 +++++++++++++++++++++++++------ frontend/__tests__/RemoveFromCart.test.js | 10 +--- 2 files changed, 71 insertions(+), 25 deletions(-) (limited to 'frontend') diff --git a/frontend/__tests__/AddToCart.test.js b/frontend/__tests__/AddToCart.test.js index 5d205f1..9faa883 100644 --- a/frontend/__tests__/AddToCart.test.js +++ b/frontend/__tests__/AddToCart.test.js @@ -2,30 +2,84 @@ import React from 'react'; import { mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; import AddToCart from '../components/AddToCart'; -import mountOptions from '../lib/testUtils'; +import { ApolloConsumer } from 'react-apollo'; +import { MockedProvider } from 'react-apollo/test-utils'; +import wait from 'waait'; +import { fakeCartItem, fakeUser } from '../lib/testUtils'; +import { ADD_TO_CART_MUTATION, CURRENT_USER_QUERY } from '../queries/queries'; + +const mocks = [ + { + request: { query: CURRENT_USER_QUERY }, + result: { + data: { + me: { + ...fakeUser(), + cart: [], + }, + }, + }, + }, + { + request: { query: ADD_TO_CART_MUTATION, variables: { id: 'abc123' } }, + result: { + data: { + addToCart: { + ...fakeCartItem(), + quantity: 1, + }, + }, + }, + }, +]; describe('', () => { it('renders and matches snapshot', () => { - const wrapper = mount(, mountOptions); + const wrapper = mount( + + + + ); expect(toJSON(wrapper.find('button'))).toMatchSnapshot(); }); - it('adds an item to the cart', () => { - const wrapper = mount(, mountOptions); - const mutation = wrapper.find('Mutation').instance(); - mutation.client.mutate = jest.fn().mockResolvedValue(null); - wrapper.simulate('click'); - // check that it was called - expect(mutation.client.mutate).toHaveBeenCalled(); - // check that it was called with the correct ID - expect(mutation.client.mutate).toHaveBeenCalledWithVariables({ id: 'abc123' }); + it('adds an item to the cart when clicked', async () => { + // 1. Grab a copy of the current client + let apolloClient; + // const wrapper = mountWithApollo(); + const wrapper = mount( + + + {client => { + apolloClient = client; + return ; + }} + + + ); + await wait(); + const { me } = apolloClient.readQuery({ query: CURRENT_USER_QUERY }); + expect(me.cart).toHaveLength(0); + + // add to cart + wrapper.find('button').simulate('click'); + await wait(); + // check if the item is in the cart + const { me: me2 } = apolloClient.readQuery({ query: CURRENT_USER_QUERY }); + + expect(me2.cart).toHaveLength(1); + expect(me2.cart[0].id).toBe('omg123'); + expect(me2.cart[0].quantity).toBe(1); }); it('changes to adding when clicked', () => { - const wrapper = mount(, mountOptions); - const button = wrapper.find('button'); - expect(button.text()).toContain('Add To Cart'); - button.simulate('click'); - expect(button.text()).toContain('Adding To Cart'); + const wrapper = mount( + + + + ); + expect(wrapper.text()).toContain('🛒 Add To Cart'); + wrapper.find('button').simulate('click'); + expect(wrapper.text()).toContain('🛒 Adding To Cart'); }); }); diff --git a/frontend/__tests__/RemoveFromCart.test.js b/frontend/__tests__/RemoveFromCart.test.js index acf5755..5439129 100644 --- a/frontend/__tests__/RemoveFromCart.test.js +++ b/frontend/__tests__/RemoveFromCart.test.js @@ -5,7 +5,7 @@ import { ApolloConsumer } from 'react-apollo'; import { MockedProvider } from 'react-apollo/test-utils'; import wait from 'waait'; import RemoveFromCart from '../components/RemoveFromCart'; -import { mountWithApollo, fakeCartItem, fakeUser } from '../lib/testUtils'; +import { fakeCartItem, fakeUser } from '../lib/testUtils'; import { CURRENT_USER_QUERY, REMOVE_FROM_CART_MUTATION } from '../queries/queries'; const mocks = [ @@ -36,14 +36,6 @@ const mocks = [ }, }, }, - // { - // request: { query: CURRENT_USER_QUERY }, - // result: { - // data: { - // me: { name: 'hi', cart: [], permissions: [], orders: [], id: 22, email: 'jame@apollo.com' }, - // }, - // }, - // }, ]; describe('', () => { -- cgit v1.3