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__/AddToCart.test.js | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 finished-application/frontend/__tests__/AddToCart.test.js (limited to 'finished-application/frontend/__tests__/AddToCart.test.js') diff --git a/finished-application/frontend/__tests__/AddToCart.test.js b/finished-application/frontend/__tests__/AddToCart.test.js new file mode 100644 index 0000000..fe15f3f --- /dev/null +++ b/finished-application/frontend/__tests__/AddToCart.test.js @@ -0,0 +1,92 @@ +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 AddToCart, { ADD_TO_CART_MUTATION } from '../components/AddToCart'; +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: [], + }, + }, + }, + }, + { + request: { query: ADD_TO_CART_MUTATION, variables: { id: 'abc123' } }, + result: { + data: { + addToCart: { + ...fakeCartItem(), + quantity: 1, + }, + }, + }, + }, +]; + +describe('', () => { + it('renders and matches snapshot', async () => { + const wrapper = mount( + + + + ); + await wait(); + wrapper.update(); + + expect(toJSON(wrapper.find('button'))).toMatchSnapshot(); + }); + + 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(); + wrapper.update(); + + 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', 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