|
| 1 | +import React from 'react'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import configureStore from 'redux-mock-store'; |
| 4 | +import { fireEvent } from '@storybook/testing-library'; |
| 5 | +import { reduxRender, screen, act, waitFor } from '../../../test-utils'; |
| 6 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 7 | +import NewPasswordForm from './NewPasswordForm'; |
| 8 | + |
| 9 | +const mockStore = configureStore([thunk]); |
| 10 | +const store = mockStore(initialTestState); |
| 11 | + |
| 12 | +const mockResetPasswordToken = 'mockResetToken'; |
| 13 | +const subject = () => { |
| 14 | + reduxRender(<NewPasswordForm resetPasswordToken={mockResetPasswordToken} />, { |
| 15 | + store |
| 16 | + }); |
| 17 | +}; |
| 18 | +const mockDispatch = jest.fn(); |
| 19 | + |
| 20 | +jest.mock('react-redux', () => ({ |
| 21 | + ...jest.requireActual('react-redux'), |
| 22 | + useDispatch: () => mockDispatch |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../../../utils/reduxFormUtils', () => ({ |
| 26 | + validateNewPassword: jest.fn() |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('../actions', () => ({ |
| 30 | + updatePassword: jest.fn().mockReturnValue( |
| 31 | + new Promise((resolve) => { |
| 32 | + resolve(); |
| 33 | + }) |
| 34 | + ) |
| 35 | +})); |
| 36 | + |
| 37 | +describe('<NewPasswordForm/>', () => { |
| 38 | + beforeEach(() => { |
| 39 | + mockDispatch.mockClear(); |
| 40 | + jest.clearAllMocks(); |
| 41 | + }); |
| 42 | + test('renders form fields correctly', () => { |
| 43 | + subject(); |
| 44 | + |
| 45 | + const passwordInputElements = screen.getAllByLabelText(/Password/i); |
| 46 | + expect(passwordInputElements).toHaveLength(2); |
| 47 | + |
| 48 | + const passwordInputElement = passwordInputElements[0]; |
| 49 | + expect(passwordInputElement).toBeInTheDocument(); |
| 50 | + |
| 51 | + const confirmPasswordInputElement = passwordInputElements[1]; |
| 52 | + expect(confirmPasswordInputElement).toBeInTheDocument(); |
| 53 | + |
| 54 | + const submitElemement = screen.getByRole('button', { |
| 55 | + name: /set new password/i |
| 56 | + }); |
| 57 | + expect(submitElemement).toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('submits form with valid data', async () => { |
| 61 | + subject(); |
| 62 | + const passwordInputElements = screen.getAllByLabelText(/Password/i); |
| 63 | + |
| 64 | + const passwordInputElement = passwordInputElements[0]; |
| 65 | + fireEvent.change(passwordInputElement, { |
| 66 | + target: { value: 'password123' } |
| 67 | + }); |
| 68 | + |
| 69 | + const confirmPasswordInputElement = passwordInputElements[1]; |
| 70 | + fireEvent.change(confirmPasswordInputElement, { |
| 71 | + target: { value: 'password123' } |
| 72 | + }); |
| 73 | + |
| 74 | + const submitElemement = screen.getByRole('button', { |
| 75 | + name: /set new password/i |
| 76 | + }); |
| 77 | + |
| 78 | + await act(async () => { |
| 79 | + fireEvent.click(submitElemement); |
| 80 | + }); |
| 81 | + |
| 82 | + await waitFor(() => expect(mockDispatch).toHaveBeenCalled()); |
| 83 | + }); |
| 84 | +}); |
0 commit comments