summaryrefslogtreecommitdiff
path: root/web-ui/src/backup_account/backup_email/backup_email.spec.js
blob: 48199738c45222abd7815463d0979e9fc5a64b7f (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
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;

  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', () => {
    let backupEmailInstance;

    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('Submit', () => {
    let preventDefaultSpy;

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

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

      fetchMock.post('/backup-account', 204);
      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 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 when success', () => {
      expect(mockOnSubmit).toHaveBeenCalledWith('success');
    });
  });
});