summaryrefslogtreecommitdiff
path: root/web-ui/src/account_recovery/new_password_form/new_password_form.spec.js
blob: c29487a7eba3ccd1381558d5f3013674c426bc53 (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
import { shallow } from 'enzyme';
import expect from 'expect';
import React from 'react';
import fetchMock from 'fetch-mock';
import { NewPasswordForm } from './new_password_form';

describe('NewPasswordForm', () => {
  let newPasswordForm;
  let mockPrevious;
  let mockNext;
  let mockTranslations;

  beforeEach(() => {
    mockTranslations = key => key;
    mockPrevious = expect.createSpy();
    newPasswordForm = shallow(
      <NewPasswordForm t={mockTranslations} previous={mockPrevious} userCode='def234' />
    );
  });

  it('renders title for new password form', () => {
    expect(newPasswordForm.find('h1').text()).toEqual('account-recovery.new-password-form.title');
  });

  it('renders input for new password', () => {
    expect(newPasswordForm.find('InputField').at(0).props().type).toEqual('password');
    expect(newPasswordForm.find('InputField').at(0).props().label).toEqual('account-recovery.new-password-form.input-label1');
  });

  it('renders input to confirm new password', () => {
    expect(newPasswordForm.find('InputField').at(1).props().type).toEqual('password');
    expect(newPasswordForm.find('InputField').at(1).props().label).toEqual('account-recovery.new-password-form.input-label2');
  });

  it('renders submit button', () => {
    expect(newPasswordForm.find('SubmitButton').props().buttonText).toEqual('account-recovery.button-next');
  });

  it('returns to previous step on link click', () => {
    newPasswordForm.find('BackLink').simulate('click');
    expect(mockPrevious).toHaveBeenCalled();
  });

  describe('Submit', () => {
    beforeEach((done) => {
      mockNext = expect.createSpy().andCall(() => done());
      newPasswordForm = shallow(
        <NewPasswordForm t={mockTranslations} previous={mockPrevious} userCode='def234' next={mockNext} />
      );
      fetchMock.post('/account-recovery', 200);
      newPasswordForm.find('InputField[name="new-password"]').simulate('change', { target: { value: '123' } });
      newPasswordForm.find('InputField[name="confirm-password"]').simulate('change', { target: { value: '456' } });
      newPasswordForm.find('form').simulate('submit', { preventDefault: expect.createSpy() });
    });

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

    it('sends user code as content', () => {
      expect(fetchMock.lastOptions('/account-recovery').body).toContain('"userCode":"def234"');
    });

    it('sends password as content', () => {
      expect(fetchMock.lastOptions('/account-recovery').body).toContain('"password":"123"');
    });

    it('sends confirm password as content', () => {
      expect(fetchMock.lastOptions('/account-recovery').body).toContain('"confirmPassword":"456"');
    });
  });

  describe('Password validation', () => {
    let newPasswordFormInstance;

    beforeEach(() => {
      newPasswordFormInstance = newPasswordForm.instance();
    });

    it('verifies initial state', () => {
      expect(newPasswordFormInstance.state.errorPassword).toEqual('');
      expect(newPasswordFormInstance.state.errorConfirmPassword).toEqual('');
      expect(newPasswordForm.find('SubmitButton').props().disabled).toBe(true);
    });

    context('with valid fields', () => {
      beforeEach(() => {
        newPasswordFormInstance.validatePassword('12345678', '12345678');
      });

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

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

    context('with invalid password', () => {
      beforeEach(() => {
        newPasswordFormInstance.validatePassword('1234', '');
      });

      it('sets password error in state', () => {
        expect(newPasswordFormInstance.state.errorPassword).toEqual('account-recovery.new-password-form.error.invalid-password');
      });

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

    context('with invalid confirm password', () => {
      beforeEach(() => {
        newPasswordFormInstance.validatePassword('12345678', '1234');
      });

      it('sets confirm password error in state', () => {
        expect(newPasswordFormInstance.state.errorConfirmPassword).toEqual('account-recovery.new-password-form.error.invalid-confirm-password');
      });

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

    context('with empty fields', () => {
      it('does not set error in state if both empty', () => {
        newPasswordFormInstance.validatePassword('', '');
        expect(newPasswordFormInstance.state.errorPassword).toEqual('');
        expect(newPasswordFormInstance.state.errorConfirmPassword).toEqual('');
      });

      it('does not set confirm password error in state if empty', () => {
        newPasswordFormInstance.validatePassword('12345678', '');
        expect(newPasswordFormInstance.state.errorConfirmPassword).toEqual('');
      });

      it('sets confirm password error in state if not empty', () => {
        newPasswordFormInstance.validatePassword('', '12345678');
        expect(newPasswordFormInstance.state.errorConfirmPassword).toEqual('account-recovery.new-password-form.error.invalid-confirm-password');
      });

      it('disables submit button if empty confirm password', () => {
        newPasswordFormInstance.validatePassword('12345678', '');
        expect(newPasswordForm.find('SubmitButton').props().disabled).toBe(true);
      });

      it('disables submit button if empty password', () => {
        newPasswordFormInstance.validatePassword('', '12345678');
        expect(newPasswordForm.find('SubmitButton').props().disabled).toBe(true);
      });
    });

    it('calls next handler on success', () => {
      expect(mockNext).toHaveBeenCalled();
    });
  });
});