summaryrefslogtreecommitdiff
path: root/web-ui/src/backup_account/backup_email/backup_email.spec.js
blob: b23aadaa7872e0c7394cadf68cd7580891d8e1e2 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { shallow } from 'enzyme';
import expect from 'expect';
import React from 'react';
import fetchMock from 'fetch-mock';
import { BackupEmail } from 'src/backup_account/backup_email/backup_email';
import browser from 'helpers/browser';

describe('BackupEmail', () => {
  let backupEmail;
  let mockOnSubmit;
  let mockTranslations;
  let backupEmailInstance;

  beforeEach(() => {
    mockOnSubmit = expect.createSpy();

    mockTranslations = key => key;
    backupEmail = shallow(<BackupEmail t={mockTranslations} onSubmit={mockOnSubmit} />);
  });

  it('renders backup email title', () => {
    expect(backupEmail.find('h1').text()).toEqual('backup-account.backup-email.title');
  });

  it('renders backup account email input field', () => {
    expect(backupEmail.find('InputField').props().name).toEqual('email');
  });

  it('renders backup account submit button', () => {
    expect(backupEmail.find('SubmitButton').props().buttonText).toEqual('backup-account.backup-email.button');
  });

  describe('Email validation', () => {
    beforeEach(() => {
      backupEmailInstance = backupEmail.instance();
    });

    it('verify initial state', () => {
      expect(backupEmailInstance.state.error).toEqual('');
      expect(backupEmail.find('SubmitButton').props().disabled).toBe(true);
    });

    context('with invalid email', () => {
      beforeEach(() => {
        backupEmailInstance.validateEmail({ target: { value: 'test' } });
      });

      it('sets error in state', () => {
        expect(backupEmailInstance.state.error).toEqual('backup-account.backup-email.error.invalid-email');
      });

      it('disables submit button', () => {
        expect(backupEmail.find('SubmitButton').props().disabled).toBe(true);
      });
    });

    context('with valid email', () => {
      beforeEach(() => {
        backupEmailInstance.validateEmail({ target: { value: 'test@test.com' } });
      });

      it('does not set error in state', () => {
        expect(backupEmailInstance.state.error).toEqual('');
      });

      it('submit button is enabled', () => {
        expect(backupEmail.find('SubmitButton').props().disabled).toBe(false);
      });
    });

    context('with empty email', () => {
      beforeEach(() => {
        backupEmailInstance.validateEmail({ target: { value: '' } });
      });

      it('not set error in state', () => {
        expect(backupEmailInstance.state.error).toEqual('');
      });

      it('disables submit button', () => {
        expect(backupEmail.find('SubmitButton').props().disabled).toBe(true);
      });
    });
  });

  describe('Email changing handler', () => {
    beforeEach(() => {
      backupEmailInstance = backupEmail.instance();
    });

    it('sets user backup email in the state', () => {
      backupEmailInstance.handleChange({ target: { value: 'test@test.com' } });
      expect(backupEmailInstance.state.backupEmail).toEqual('test@test.com');
    });
  });

  describe('Submit', () => {
    let preventDefaultSpy;

    beforeEach(() => {
      preventDefaultSpy = expect.createSpy();
    });

    context('on success', () => {
      beforeEach((done) => {
        mockOnSubmit = expect.createSpy().andCall(() => done());
        expect.spyOn(browser, 'getCookie').andReturn('abc123');

        fetchMock.post('/backup-account', 204);
        backupEmail = shallow(<BackupEmail t={mockTranslations} onSubmit={mockOnSubmit} />);

        backupEmail.find('InputField').simulate('change', { target: { value: 'test@test.com' } });
        backupEmail.find('form').simulate('submit', { preventDefault: preventDefaultSpy });
      });

      it('posts backup email', () => {
        expect(fetchMock.called('/backup-account')).toBe(true, 'Backup account POST was not called');
      });

      it('sends csrftoken as content', () => {
        expect(fetchMock.lastOptions('/backup-account').body).toContain('"csrftoken":["abc123"]');
      });

      it('sends user email as content', () => {
        expect(fetchMock.lastOptions('/backup-account').body).toContain('"backupEmail":"test@test.com"');
      });

      it('sends content-type header', () => {
        expect(fetchMock.lastOptions('/backup-account').headers['Content-Type']).toEqual('application/json');
      });

      it('sends same origin headers', () => {
        expect(fetchMock.lastOptions('/backup-account').credentials).toEqual('same-origin');
      });

      it('prevents default call to refresh page', () => {
        expect(preventDefaultSpy).toHaveBeenCalled();
      });

      it('calls onSubmit from props with success', () => {
        expect(mockOnSubmit).toHaveBeenCalledWith('success');
      });
    });

    context('on error', () => {
      beforeEach((done) => {
        mockOnSubmit = expect.createSpy().andCall(() => done());

        fetchMock.post('/backup-account', 500);
        backupEmail = shallow(<BackupEmail t={mockTranslations} onSubmit={mockOnSubmit} />);
        backupEmail.find('form').simulate('submit', { preventDefault: preventDefaultSpy });
      });

      it('calls onSubmit from props with error', () => {
        expect(mockOnSubmit).toHaveBeenCalledWith('error');
      });
    });
  });

  afterEach(() => {
    fetchMock.restore();
  });
});