summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-04-19 13:29:25 -0400
committerWes Bos <wesbos@gmail.com>2018-04-19 13:29:25 -0400
commite018f5482e550a49535f4df990d3bfc09128f5d8 (patch)
treed8058ac92f68a487c985553cff26650f040cdd11 /frontend/__tests__
parente91740e812080fafa058b13b63218eeda4449135 (diff)
Add to cart tests
Diffstat (limited to 'frontend/__tests__')
-rw-r--r--frontend/__tests__/AddToCart.test.js35
-rw-r--r--frontend/__tests__/__snapshots__/AddToCart.test.js.snap4
2 files changed, 22 insertions, 17 deletions
diff --git a/frontend/__tests__/AddToCart.test.js b/frontend/__tests__/AddToCart.test.js
index f049152..92ad0ad 100644
--- a/frontend/__tests__/AddToCart.test.js
+++ b/frontend/__tests__/AddToCart.test.js
@@ -1,28 +1,31 @@
import React from 'react';
import { shallow, mount } from 'enzyme';
import toJSON from 'enzyme-to-json';
-import { AddToCart } from '../components/AddToCart';
-
-const currentUser = {
- me: {},
- refetch() {},
-};
+import AddToCart from '../components/AddToCart';
+import mountOptions from './mockMang';
describe('<AddtoCart/>', () => {
it('renders and matches snapshot', () => {
- const wrapper = shallow(
- <AddToCart id="abc123" addToCart={() => {}} currentUser={currentUser} />
- );
- expect(toJSON(wrapper)).toMatchSnapshot();
+ const wrapper = mount(<AddToCart id="abc123" />, mountOptions);
+ expect(toJSON(wrapper.find('button'))).toMatchSnapshot();
});
it('adds an item to the cart', () => {
- const addToCart = jest.fn(() => Promise.resolve());
- const wrapper = shallow(
- <AddToCart id="abc123" addToCart={addToCart} currentUser={currentUser} />
- );
+ const wrapper = mount(<AddToCart id="abc123" />, mountOptions);
+ const mutation = wrapper.find('Mutation').instance();
+ mutation.client.mutate = jest.fn().mockResolvedValue(null);
wrapper.simulate('click');
- expect(addToCart).toHaveBeenCalled();
- expect(addToCart).toHaveBeenCalledWith({ variables: { id: 'abc123' } });
+ // 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('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');
});
});
diff --git a/frontend/__tests__/__snapshots__/AddToCart.test.js.snap b/frontend/__tests__/__snapshots__/AddToCart.test.js.snap
index 999161c..e3f8229 100644
--- a/frontend/__tests__/__snapshots__/AddToCart.test.js.snap
+++ b/frontend/__tests__/__snapshots__/AddToCart.test.js.snap
@@ -2,8 +2,10 @@
exports[`<AddtoCart/> renders and matches snapshot 1`] = `
<button
+ disabled={false}
onClick={[Function]}
>
- 🛒 Add To Cart
+ 🛒 Add
+ To Cart
</button>
`;