summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__/AddToCart.test.js
blob: 9b6a3042bddba5440ba141a9ff46fe65f50c163c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from 'react';
import { mount } from 'enzyme';
import toJSON from 'enzyme-to-json';
import AddToCart from '../components/AddToCart';
import mountOptions from './mockMang';

describe('<AddtoCart/>', () => {
  it('renders and matches snapshot', () => {
    const wrapper = mount(<AddToCart id="abc123" />, mountOptions);
    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('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');
  });
});