From 13d263b7c088690b77d964f84fac454922b57ef8 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 20 Apr 2018 12:38:03 -0400 Subject: RemoveFromCart tests --- frontend/__tests__/RemoveFromCart.test.js | 19 +++++++++++-------- .../__snapshots__/RemoveFromCart.test.js.snap | 5 +++-- frontend/__tests__/mockMang.js | 9 +++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'frontend/__tests__') diff --git a/frontend/__tests__/RemoveFromCart.test.js b/frontend/__tests__/RemoveFromCart.test.js index 0b1de74..9cdde5e 100644 --- a/frontend/__tests__/RemoveFromCart.test.js +++ b/frontend/__tests__/RemoveFromCart.test.js @@ -1,19 +1,22 @@ import React from 'react'; import { shallow, mount } from 'enzyme'; import toJSON from 'enzyme-to-json'; +import { ApolloProvider } from 'react-apollo'; import RemoveFromCart from '../components/RemoveFromCart'; -import mountOptions from './mockMang'; +import mountOptions, { mocked, mountWithApollo } from './mockMang'; describe('', () => { it('renders and matches snapshot', () => { - const wrapper = shallow( { }} />); - expect(toJSON(wrapper)).toMatchSnapshot(); + const wrapper = mountWithApollo(); + expect(toJSON(wrapper.find('button'))).toMatchSnapshot(); }); - it("runs a function when it's clicked", () => { - const removeSpy = jest.fn(); - const wrapper = shallow(); - wrapper.simulate('click'); - expect(removeSpy).toHaveBeenCalledWith({ variables: { id: 'abc123' } }); + it('runs the mutation with correct variables', () => { + const wrapper = mountWithApollo(); + const mutation = wrapper.find('Mutation').instance(); + mutation.client.mutate = jest.fn().mockResolvedValue({ removeFromCart: { id: 'abc123' } }); + wrapper.find('button').simulate('click'); + expect(mutation.client.mutate).toHaveBeenCalled(); + expect(mutation.client.mutate).toHaveBeenCalledWithVariables({ id: 'abc123' }); }); }); diff --git a/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap b/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap index b25bcc9..f982011 100644 --- a/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap +++ b/frontend/__tests__/__snapshots__/RemoveFromCart.test.js.snap @@ -1,10 +1,11 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[` renders and matches snapshot 1`] = ` - × - + `; diff --git a/frontend/__tests__/mockMang.js b/frontend/__tests__/mockMang.js index 3dba703..6922601 100644 --- a/frontend/__tests__/mockMang.js +++ b/frontend/__tests__/mockMang.js @@ -3,6 +3,8 @@ import { makeExecutableSchema } from 'graphql-tools'; import MockClient from 'graphql-mock'; import casual from 'casual'; import PropTypes from 'prop-types'; +import { ApolloProvider } from 'react-apollo'; +import { mount } from 'enzyme'; // seed it so we get consistent results casual.seed(777); @@ -61,4 +63,11 @@ const mountOptions = { }, }; +const mountWithApollo = (Component, props) => { + const wrapper = mount({Component}); + return wrapper.children(); +}; + export default mountOptions; + +export { mocked, mountWithApollo }; -- cgit v1.3