Skip to content

Commit

Permalink
Issue-#2923 added test for the NewPasswordForm.
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravsingh94 committed Feb 8, 2024
1 parent 37727bf commit 61577b9
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions client/modules/User/components/NewPasswordForm.unit.test.jsx
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());
});
});

0 comments on commit 61577b9

Please sign in to comment.