summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__/EditUser.test.js
blob: 08dd6f6091eaa774c29c75f6ec9cb50317550a2e (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from 'react';
import { shallow } from 'enzyme';
import toJSON from 'enzyme-to-json';
import { EditUser } from '../components/EditUser';

const wait = amount => new Promise(resolve => setTimeout(resolve, amount));

const currentUser = {
  me: {
    name: 'Wes Bos',
  },
  refetch() {},
};

describe('<EditUser/>', () => {
  it('renders', () => {
    const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={() => {}} />);
    expect(toJSON(wrapper)).toMatchSnapshot();
  });

  it('asks you to log in when you arent logged in', () => {
    const wrapper = shallow(<EditUser currentUser={{}} updateUser={() => {}} />);
    expect(toJSON(wrapper)).toMatchSnapshot();
  });

  it('displays changes', () => {
    const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={() => {}} />);
    const nameInput = wrapper.find('[name="name"]');
    nameInput.simulate('change', { target: { name: 'name', value: 'Scott' } });
    const diff = wrapper.find('pre[data-test="change"]');
    expect(diff.text()).toBe('{"name":"Scott"}');
  });

  it('calls update user when form is submitted', () => {
    const updateUserSpy = jest.fn();
    const wrapper = shallow(<EditUser currentUser={currentUser} updateUser={updateUserSpy} />);
    const nameInput = wrapper.find('[name="name"]');
    nameInput.simulate('change', { target: { name: 'name', value: 'Scott' } });
    wrapper.simulate('submit', { preventDefault() {} });
    expect(updateUserSpy).toHaveBeenCalledWith({ variables: { name: 'Scott' } });
  });

  it('refetches the current user after an update', async () => {
    const currentUserWithSpy = {
      me: { name: 'wes' },
      refetch: jest.fn(),
    };
    const wrapper = shallow(<EditUser currentUser={currentUserWithSpy} updateUser={() => {}} />);
    wrapper.simulate('submit', { preventDefault() {} });
    await wait(0);
    expect(currentUserWithSpy.refetch).toHaveBeenCalled();
  });
});