summaryrefslogtreecommitdiffstats
path: root/finished-application/frontend/__tests__/AddToCart.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'finished-application/frontend/__tests__/AddToCart.test.js')
-rw-r--r--finished-application/frontend/__tests__/AddToCart.test.js45
1 files changed, 25 insertions, 20 deletions
diff --git a/finished-application/frontend/__tests__/AddToCart.test.js b/finished-application/frontend/__tests__/AddToCart.test.js
index fe15f3f..cfc2e3e 100644
--- a/finished-application/frontend/__tests__/AddToCart.test.js
+++ b/finished-application/frontend/__tests__/AddToCart.test.js
@@ -1,12 +1,11 @@
-import React from 'react';
import { mount } from 'enzyme';
+import wait from 'waait';
import toJSON from 'enzyme-to-json';
-import { ApolloConsumer } from 'react-apollo';
import { MockedProvider } from 'react-apollo/test-utils';
-import wait from 'waait';
+import { ApolloConsumer } from 'react-apollo';
import AddToCart, { ADD_TO_CART_MUTATION } from '../components/AddToCart';
-import { fakeCartItem, fakeUser } from '../lib/testUtils';
import { CURRENT_USER_QUERY } from '../components/User';
+import { fakeUser, fakeCartItem } from '../lib/testUtils';
const mocks = [
{
@@ -21,6 +20,17 @@ const mocks = [
},
},
{
+ request: { query: CURRENT_USER_QUERY },
+ result: {
+ data: {
+ me: {
+ ...fakeUser(),
+ cart: [fakeCartItem()],
+ },
+ },
+ },
+ },
+ {
request: { query: ADD_TO_CART_MUTATION, variables: { id: 'abc123' } },
result: {
data: {
@@ -33,8 +43,8 @@ const mocks = [
},
];
-describe('<AddtoCart/>', () => {
- it('renders and matches snapshot', async () => {
+describe('<AddToCart/>', () => {
+ it('renders and matches the snap shot', async () => {
const wrapper = mount(
<MockedProvider mocks={mocks}>
<AddToCart id="abc123" />
@@ -42,14 +52,11 @@ describe('<AddtoCart/>', () => {
);
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
+ it('adds an item to cart when clicked', async () => {
let apolloClient;
- // const wrapper = mountWithApollo(<RemoveFromCart id="abc123" />);
const wrapper = mount(
<MockedProvider mocks={mocks}>
<ApolloConsumer>
@@ -62,22 +69,20 @@ describe('<AddtoCart/>', () => {
);
await wait();
wrapper.update();
-
- const { me } = apolloClient.readQuery({ query: CURRENT_USER_QUERY });
+ const { data: { me } } = await apolloClient.query({ query: CURRENT_USER_QUERY });
+ // console.log(me);
expect(me.cart).toHaveLength(0);
-
- // add to cart
+ // add an item to the 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 });
-
+ const { data: { me: me2 } } = await apolloClient.query({ query: CURRENT_USER_QUERY });
expect(me2.cart).toHaveLength(1);
expect(me2.cart[0].id).toBe('omg123');
- expect(me2.cart[0].quantity).toBe(1);
+ expect(me2.cart[0].quantity).toBe(3);
});
- it('changes to adding when clicked', async () => {
+ it('changes from add to adding when clicked', async () => {
const wrapper = mount(
<MockedProvider mocks={mocks}>
<AddToCart id="abc123" />
@@ -85,8 +90,8 @@ describe('<AddtoCart/>', () => {
);
await wait();
wrapper.update();
- expect(wrapper.text()).toContain('🛒 Add To Cart');
+ expect(wrapper.text()).toContain('Add To Cart');
wrapper.find('button').simulate('click');
- expect(wrapper.text()).toContain('🛒 Adding To Cart');
+ expect(wrapper.text()).toContain('Adding To Cart');
});
});