|
| 1 | +import { render, fireEvent } from '@testing-library/react'; |
| 2 | +import ChangePassword from '../ChangePassword.component'; |
| 3 | +import { getTestI18n } from '../../../../../tests/testHelpers'; |
| 4 | + |
| 5 | +const i18n = getTestI18n(); |
| 6 | + |
| 7 | +describe('ChangePassword container', () => { |
| 8 | + it('submitting form does not work when fields empty', () => { |
| 9 | + const onSave = jest.fn(); |
| 10 | + const { container } = render(<ChangePassword className="the-class" onSave={onSave} i18n={i18n} oneTimePassword="" throttle={false} />); |
| 11 | + |
| 12 | + const submitButton = container.querySelector('button[type=submit]') as HTMLButtonElement; |
| 13 | + fireEvent.click(submitButton); |
| 14 | + |
| 15 | + expect(onSave).not.toHaveBeenCalled(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('submitting form still does not works when fields non-empty but new passwords are not identical', () => { |
| 19 | + const onSave = jest.fn(); |
| 20 | + const { container } = render(<ChangePassword className="the-class" onSave={onSave} i18n={i18n} oneTimePassword="" throttle={false} />); |
| 21 | + |
| 22 | + const currentPasswordField = container.querySelector('input[name=currentPassword]') as HTMLInputElement; |
| 23 | + fireEvent.change(currentPasswordField, { |
| 24 | + target: { |
| 25 | + value: 'current-password' |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + const newPasswordField1 = container.querySelector('input[name=password]') as HTMLInputElement; |
| 30 | + fireEvent.change(newPasswordField1, { |
| 31 | + target: { |
| 32 | + value: 'password' |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + const newPasswordField2 = container.querySelector('input[name=password2]') as HTMLInputElement; |
| 37 | + fireEvent.change(newPasswordField2, { |
| 38 | + target: { |
| 39 | + value: 'passwordX' |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + const submitButton = container.querySelector('button[type=submit]') as HTMLButtonElement; |
| 44 | + fireEvent.click(submitButton); |
| 45 | + |
| 46 | + expect(onSave).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('submitting form works when fields non-empty and new passwords are identical', () => { |
| 50 | + const onSave = jest.fn(); |
| 51 | + const { container } = render(<ChangePassword className="the-class" onSave={onSave} i18n={i18n} oneTimePassword="" throttle={false} />); |
| 52 | + |
| 53 | + const currentPasswordField = container.querySelector('input[name=currentPassword]') as HTMLInputElement; |
| 54 | + fireEvent.change(currentPasswordField, { |
| 55 | + target: { |
| 56 | + value: 'current-password' |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + const newPasswordField1 = container.querySelector('input[name=password]') as HTMLInputElement; |
| 61 | + fireEvent.change(newPasswordField1, { |
| 62 | + target: { |
| 63 | + value: 'password' |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + const newPasswordField2 = container.querySelector('input[name=password2]') as HTMLInputElement; |
| 68 | + fireEvent.change(newPasswordField2, { |
| 69 | + target: { |
| 70 | + value: 'password' |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + const submitButton = container.querySelector('button[type=submit]') as HTMLButtonElement; |
| 75 | + fireEvent.click(submitButton); |
| 76 | + |
| 77 | + expect(onSave).toHaveBeenCalled(); |
| 78 | + }); |
| 79 | +}); |
0 commit comments