summaryrefslogtreecommitdiffstats
path: root/frontend/__tests__/Signup.test.js
blob: f73c01c8c550f8fba0aefe2ef57739b4034df164 (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
54
import React from 'react';
import { mount } from 'enzyme';
import toJSON from 'enzyme-to-json';
import wait from 'waait';
import Signup from '../components/Signup';
import mountOptions from './mockMang';

function type(wrapper, name, value) {
  wrapper.find(`input[name="${name}"]`).simulate('change', {
    target: {
      name,
      value,
    },
  });
}

describe('<Signup/>', () => {
  beforeAll(() => {
    // mock localStorage
    global.localStorage = {
      setItem: jest.fn(),
    };
  });

  it('renders and matches snapshot', () => {
    const wrapper = mount(<Signup />, mountOptions);
    expect(toJSON(wrapper.find('form'))).toMatchSnapshot();
  });

  it('calls mutation with correct data', async () => {
    const wrapper = mount(<Signup />, mountOptions);
    const mutation = wrapper.find('Mutation').instance();
    mutation.client.mutate = jest.fn().mockResolvedValue({
      data: { signup: { token: 'omg123' } },
    });

    const expected = {
      name: 'Randy',
      email: 'Randy@randymail.net',
      password: 'pupsrock',
    };

    type(wrapper, 'name', expected.name);
    type(wrapper, 'email', expected.email);
    type(wrapper, 'password', expected.password);

    wrapper.simulate('submit');

    expect(mutation.client.mutate).toHaveBeenCalled();
    expect(mutation.client.mutate).toHaveBeenCalledWithVariables(expected);
    await wait();
    expect(localStorage.setItem).toHaveBeenCalledWith('token', 'omg123');
  });
});