Skip to content

Commit

Permalink
Forgot Password Screen Tests done!
Browse files Browse the repository at this point in the history
  • Loading branch information
rishav-jha-mech committed Oct 27, 2023
1 parent dde8409 commit c831bb2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 21 deletions.
50 changes: 41 additions & 9 deletions src/screens/ForgotPassword/ForgotPassword.test.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { act, render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import userEvent from '@testing-library/user-event';
import 'jest-localstorage-mock';
import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';

import ForgotPassword from './ForgotPassword';
import { store } from 'state/store';
import {
FORGOT_PASSWORD_MUTATION,
GENERATE_OTP_MUTATION,
} from 'GraphQl/Mutations/mutations';
import i18nForTest from 'utils/i18nForTest';
import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import i18nForTest from 'utils/i18nForTest';
import ForgotPassword from './ForgotPassword';

const MOCKS = [
{
Expand Down Expand Up @@ -57,7 +57,9 @@ async function wait(ms = 100): Promise<void> {
});
});
}

beforeEach(() => {
localStorage.setItem('IsLoggedIn', 'FALSE');
});
afterEach(() => {
localStorage.clear();
});
Expand All @@ -81,7 +83,9 @@ describe('Testing Forgot Password screen', () => {
await wait();

expect(screen.getByText(/Forgot Password/i)).toBeInTheDocument();
expect(screen.getByText(/Back to Home/i)).toBeInTheDocument();
expect(screen.getByText(/Registered Email/i)).toBeInTheDocument();
expect(screen.getByText(/Get Otp/i)).toBeInTheDocument();
expect(screen.getByText(/Back to Login/i)).toBeInTheDocument();
expect(window.location).toBeAt('/orglist');
});

Expand Down Expand Up @@ -128,13 +132,15 @@ describe('Testing Forgot Password screen', () => {
);

userEvent.click(screen.getByText('Get OTP'));
await wait();
});

test('Testing forgot password functionality', async () => {
const formData = {
userOtp: '12345',
newPassword: 'johnDoe',
confirmNewPassword: 'johnDoe',
email: '[email protected]',
};

render(
Expand All @@ -151,18 +157,28 @@ describe('Testing Forgot Password screen', () => {

await wait();

userEvent.type(
screen.getByPlaceholderText(/Registered email/i),
formData.email
);

userEvent.click(screen.getByText('Get OTP'));
await wait();

userEvent.type(screen.getByPlaceholderText('e.g. 12345'), formData.userOtp);
userEvent.type(screen.getByTestId('newPassword'), formData.newPassword);
userEvent.type(
screen.getByTestId('confirmNewPassword'),
formData.confirmNewPassword
);

localStorage.setItem('otpToken', 'lorem ipsum');
userEvent.click(screen.getByText('Change Password'));
await wait();
});

test('Testing forgot password functionality, when new password and confirm password is not same', async () => {
const formData = {
email: '[email protected]',
userOtp: '12345',
newPassword: 'johnDoe',
confirmNewPassword: 'doeJohn',
Expand All @@ -182,6 +198,14 @@ describe('Testing Forgot Password screen', () => {

await wait();

userEvent.type(
screen.getByPlaceholderText(/Registered email/i),
formData.email
);

userEvent.click(screen.getByText('Get OTP'));
await wait();

userEvent.type(screen.getByPlaceholderText('e.g. 12345'), formData.userOtp);
userEvent.type(screen.getByTestId('newPassword'), formData.newPassword);
userEvent.type(
Expand All @@ -197,6 +221,7 @@ describe('Testing Forgot Password screen', () => {
userOtp: '12345',
newPassword: 'johnDoe',
confirmNewPassword: 'johnDoe',
email: '[email protected]',
};

localStorage.setItem('otpToken', '');
Expand All @@ -215,13 +240,20 @@ describe('Testing Forgot Password screen', () => {

await wait();

userEvent.type(
screen.getByPlaceholderText(/Registered email/i),
formData.email
);

userEvent.click(screen.getByText('Get OTP'));
await wait();

userEvent.type(screen.getByPlaceholderText('e.g. 12345'), formData.userOtp);
userEvent.type(screen.getByTestId('newPassword'), formData.newPassword);
userEvent.type(
screen.getByTestId('confirmNewPassword'),
formData.confirmNewPassword
);

userEvent.click(screen.getByText('Change Password'));
});
});
26 changes: 14 additions & 12 deletions src/screens/ForgotPassword/ForgotPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const ForgotPassword = (): JSX.Element => {

const [showEnterEmail, setShowEnterEmail] = useState(true);

const [componentLoader, setComponentLoader] = useState(true);
const [registeredEmail, setregisteredEmail] = useState('');

const [forgotPassFormData, setForgotPassFormData] = useState({
Expand All @@ -38,22 +37,24 @@ const ForgotPassword = (): JSX.Element => {
confirmNewPassword: '',
});

const [otp, { loading: otpLoading }] = useMutation(GENERATE_OTP_MUTATION);
const [otp, { loading: otpLoading, error }] = useMutation(
GENERATE_OTP_MUTATION
);
const [forgotPassword, { loading: forgotPasswordLoading }] = useMutation(
FORGOT_PASSWORD_MUTATION
);

const isLoggedIn = localStorage.getItem('IsLoggedIn');
useEffect(() => {
const isLoggedIn = localStorage.getItem('IsLoggedIn');
if (isLoggedIn == 'TRUE') {
window.location.replace('/orglist');
}
setComponentLoader(false);
return () => {
localStorage.removeItem('otpToken');
};
}, []);

const getOTP = async (e: ChangeEvent<HTMLFormElement>): Promise<void> => {
e.preventDefault();
setShowEnterEmail(false);

try {
const { data } = await otp({
Expand All @@ -62,14 +63,12 @@ const ForgotPassword = (): JSX.Element => {
},
});

/* istanbul ignore next */
if (data) {
localStorage.setItem('otpToken', data.otp.otpToken);
toast.success(t('OTPsent'));
setShowEnterEmail(false);
}
} catch (error: any) {
/* istanbul ignore next */
if (error.message === 'User not found') {
toast.warn(t('emailNotRegistered'));
} else if (error.message === 'Failed to fetch') {
Expand Down Expand Up @@ -117,16 +116,15 @@ const ForgotPassword = (): JSX.Element => {
});
}
} catch (error: any) {
/* istanbul ignore next */
setShowEnterEmail(true);
/* istanbul ignore next */
errorHandler(t, error);
}
};

if (componentLoader || otpLoading || forgotPasswordLoading) {
if (otpLoading || forgotPasswordLoading) {
return <Loader />;
}

return (
<>
<div className={styles.pageWrapper}>
Expand Down Expand Up @@ -158,7 +156,11 @@ const ForgotPassword = (): JSX.Element => {
}
/>
</div>
<Button type="submit" className="mt-4 w-100">
<Button
type="submit"
className="mt-4 w-100"
data-testid="getOtpBtn"
>
{t('getOtp')}
</Button>
</Form>
Expand Down

0 comments on commit c831bb2

Please sign in to comment.