summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__/AddToCart.test.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-05-04 11:41:28 -0400
committerWes Bos <wesbos@gmail.com>2018-05-04 11:41:28 -0400
commit952c37536aeb69fb9edac15a9d260890d4b1c50c (patch)
treed82526e12d437eaa94c16927c09cc0cf81e5b9a4 /frontend/__tests__/AddToCart.test.js
parent06003b7a5f66f61bde6bb2fc9443c8cf3bc8bd9b (diff)
Add and remove mutations
Diffstat (limited to 'frontend/__tests__/AddToCart.test.js')
-rw-r--r--frontend/__tests__/AddToCart.test.js86
1 files changed, 70 insertions, 16 deletions
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('<AddtoCart/>', () => {
it('renders and matches snapshot', () => {
- const wrapper = mount(<AddToCart id="abc123" />, mountOptions);
+ const wrapper = mount(
+ <MockedProvider>
+ <AddToCart id="abc123" />
+ </MockedProvider>
+ );
expect(toJSON(wrapper.find('button'))).toMatchSnapshot();
});
- it('adds an item to the cart', () => {
- const wrapper = mount(<AddToCart id="abc123" />, 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(<RemoveFromCart id="abc123" />);
+ const wrapper = mount(
+ <MockedProvider mocks={mocks}>
+ <ApolloConsumer>
+ {client => {
+ apolloClient = client;
+ return <AddToCart id="abc123" />;
+ }}
+ </ApolloConsumer>
+ </MockedProvider>
+ );
+ 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(<AddToCart id="abc123" />, 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(
+ <MockedProvider mocks={mocks}>
+ <AddToCart id="abc123" />
+ </MockedProvider>
+ );
+ expect(wrapper.text()).toContain('🛒 Add To Cart');
+ wrapper.find('button').simulate('click');
+ expect(wrapper.text()).toContain('🛒 Adding To Cart');
});
});