-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue-#2923 added test for the NewPasswordForm.
- Loading branch information
1 parent
37727bf
commit 61577b9
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
client/modules/User/components/NewPasswordForm.unit.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React from 'react'; | ||
import thunk from 'redux-thunk'; | ||
import configureStore from 'redux-mock-store'; | ||
import { fireEvent } from '@storybook/testing-library'; | ||
import { reduxRender, screen, act, waitFor } from '../../../test-utils'; | ||
import { initialTestState } from '../../../testData/testReduxStore'; | ||
import NewPasswordForm from './NewPasswordForm'; | ||
|
||
const mockStore = configureStore([thunk]); | ||
const store = mockStore(initialTestState); | ||
|
||
const mockResetPasswordToken = 'mockResetToken'; | ||
const subject = () => { | ||
reduxRender(<NewPasswordForm resetPasswordToken={mockResetPasswordToken} />, { | ||
store | ||
}); | ||
}; | ||
const mockDispatch = jest.fn(); | ||
|
||
jest.mock('react-redux', () => ({ | ||
...jest.requireActual('react-redux'), | ||
useDispatch: () => mockDispatch | ||
})); | ||
|
||
jest.mock('../../../utils/reduxFormUtils', () => ({ | ||
validateNewPassword: jest.fn() | ||
})); | ||
|
||
jest.mock('../actions', () => ({ | ||
updatePassword: jest.fn().mockReturnValue( | ||
new Promise((resolve) => { | ||
resolve(); | ||
}) | ||
) | ||
})); | ||
|
||
describe('<NewPasswordForm/>', () => { | ||
beforeEach(() => { | ||
mockDispatch.mockClear(); | ||
jest.clearAllMocks(); | ||
}); | ||
test('renders form fields correctly', () => { | ||
subject(); | ||
|
||
const passwordInputElements = screen.getAllByLabelText(/Password/i); | ||
expect(passwordInputElements).toHaveLength(2); | ||
|
||
const passwordInputElement = passwordInputElements[0]; | ||
expect(passwordInputElement).toBeInTheDocument(); | ||
|
||
const confirmPasswordInputElement = passwordInputElements[1]; | ||
expect(confirmPasswordInputElement).toBeInTheDocument(); | ||
|
||
const submitElemement = screen.getByRole('button', { | ||
name: /set new password/i | ||
}); | ||
expect(submitElemement).toBeInTheDocument(); | ||
}); | ||
|
||
test('submits form with valid data', async () => { | ||
subject(); | ||
const passwordInputElements = screen.getAllByLabelText(/Password/i); | ||
|
||
const passwordInputElement = passwordInputElements[0]; | ||
fireEvent.change(passwordInputElement, { | ||
target: { value: 'password123' } | ||
}); | ||
|
||
const confirmPasswordInputElement = passwordInputElements[1]; | ||
fireEvent.change(confirmPasswordInputElement, { | ||
target: { value: 'password123' } | ||
}); | ||
|
||
const submitElemement = screen.getByRole('button', { | ||
name: /set new password/i | ||
}); | ||
|
||
await act(async () => { | ||
fireEvent.click(submitElemement); | ||
}); | ||
|
||
await waitFor(() => expect(mockDispatch).toHaveBeenCalled()); | ||
}); | ||
}); |