Skip to content

Commit

Permalink
added unit test case of forgot password (#15113)
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-vador authored Feb 9, 2024
1 parent 96de1f5 commit e4e96b2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const PageNotFound = withSuspenseFallback(

const ForgotPassword = withSuspenseFallback(
React.lazy(
() => import('../../pages/ForgotPassword/forgot-password.component')
() => import('../../pages/ForgotPassword/ForgotPassword.component')
)
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Collate.
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -46,7 +46,9 @@ const ForgotPassword = () => {
);

return (
<div className="h-full py-24 forgot-password-container ">
<div
className="h-full py-24 forgot-password-container "
data-testid="forgot-password-container">
<Card
bodyStyle={{ padding: '48px' }}
className="m-auto"
Expand Down Expand Up @@ -118,6 +120,7 @@ const ForgotPassword = () => {
<Button
ghost
className="w-full"
data-testid="go-back-button"
type="primary"
onClick={() => history.push(ROUTES.SIGNIN)}>
{t('message.go-back-to-login-page')}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2024 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { act, fireEvent, render } from '@testing-library/react';
import React from 'react';
import { useBasicAuth } from '../../components/Auth/AuthProviders/BasicAuthProvider';
import ForgotPassword from './ForgotPassword.component';

const mockPush = jest.fn();
const handleForgotPassword = jest.fn();

jest.mock('../../components/Auth/AuthProviders/BasicAuthProvider', () => {
return {
useBasicAuth: jest.fn().mockImplementation(() => ({
handleResetPassword: handleForgotPassword,
})),
};
});

jest.mock('react-router-dom', () => ({
useHistory: jest.fn().mockImplementation(() => ({
push: mockPush,
})),
}));

describe('ForgotPassword', () => {
it('renders correctly', () => {
const { getByTestId, getByText } = render(<ForgotPassword />);

expect(getByTestId('forgot-password-container')).toBeInTheDocument();
expect(
getByText('message.enter-your-registered-email')
).toBeInTheDocument();
});

it('calls handleForgotPassword with the correct email', async () => {
(useBasicAuth as jest.Mock).mockReturnValue({ handleForgotPassword });

const { getByLabelText, getByText } = render(<ForgotPassword />);
const emailInput = getByLabelText('label.email');
const submitButton = getByText('label.submit');
await act(async () => {
fireEvent.change(emailInput, { target: { value: 'test@example.com' } });
});
await act(async () => {
fireEvent.click(submitButton);
});

expect(handleForgotPassword).toHaveBeenCalledWith('test@example.com');
});

it('shows an error when email is not provided', async () => {
jest.useFakeTimers();
const { getByLabelText, getByText, findByText } = render(
<ForgotPassword />
);
const emailInput = getByLabelText('label.email');
const submitButton = getByText('label.submit');

await act(async () => {
fireEvent.change(emailInput, { target: { value: '' } });
fireEvent.click(submitButton);
});
jest.advanceTimersByTime(20);
const errorMessage = await findByText('label.field-invalid');

expect(errorMessage).toBeInTheDocument();
});

it('show alert', async () => {
const { getByLabelText, getByText, getByTestId } = render(
<ForgotPassword />
);
const emailInput = getByLabelText('label.email');
const submitButton = getByText('label.submit');
await act(async () => {
fireEvent.change(emailInput, { target: { value: 'test@example.com' } });
});
await act(async () => {
fireEvent.click(submitButton);
});

expect(handleForgotPassword).toHaveBeenCalledWith('test@example.com');
expect(getByTestId('success-screen-container')).toBeInTheDocument();
expect(getByTestId('success-icon')).toBeInTheDocument();
expect(getByTestId('success-line')).toBeInTheDocument();
});

it('show call push back to login', async () => {
const { getByTestId } = render(<ForgotPassword />);
const goBackButton = getByTestId('go-back-button');
await act(async () => {
fireEvent.click(goBackButton);
});

expect(mockPush).toHaveBeenCalled();
});
});

0 comments on commit e4e96b2

Please sign in to comment.