From ea896dda2c102db214f4a6b23d4959b77e695512 Mon Sep 17 00:00:00 2001 From: Aarish Shah Mohsin Date: Wed, 29 Nov 2023 22:26:49 +0530 Subject: [PATCH] Added test for ForgotPassword.tsx --- .../ForgotPassword/ForgotPassword.test.tsx | 181 ++++++++++++++++-- 1 file changed, 166 insertions(+), 15 deletions(-) diff --git a/src/screens/ForgotPassword/ForgotPassword.test.tsx b/src/screens/ForgotPassword/ForgotPassword.test.tsx index 94db5b2839..f2cfe324cb 100644 --- a/src/screens/ForgotPassword/ForgotPassword.test.tsx +++ b/src/screens/ForgotPassword/ForgotPassword.test.tsx @@ -7,49 +7,62 @@ import 'jest-location-mock'; import { I18nextProvider } from 'react-i18next'; import { Provider } from 'react-redux'; import { BrowserRouter } from 'react-router-dom'; +import { ToastContainer } from 'react-toastify'; -import { - FORGOT_PASSWORD_MUTATION, - GENERATE_OTP_MUTATION, -} from 'GraphQl/Mutations/mutations'; +import { GENERATE_OTP_MUTATION } from 'GraphQl/Mutations/mutations'; import { store } from 'state/store'; import { StaticMockLink } from 'utils/StaticMockLink'; import i18nForTest from 'utils/i18nForTest'; import ForgotPassword from './ForgotPassword'; +import i18n from 'utils/i18nForTest'; const MOCKS = [ { request: { - query: FORGOT_PASSWORD_MUTATION, + query: GENERATE_OTP_MUTATION, variables: { - userOtp: '12345', - newPassword: 'johndoe', - otpToken: 'otpToken', + email: 'johndoe@gmail.com', }, }, result: { data: { - forgotPassword: true, + otp: { + otpToken: 'otpToken', + }, }, }, }, + { request: { query: GENERATE_OTP_MUTATION, variables: { - email: 'johndoe@gmail.com', + email: 'notexists@gmail.com', }, }, - result: { - data: { - otp: { - otpToken: 'otpToken', - }, + error: new Error('User not found'), + }, +]; + +const MOCKS_INTERNET_UNAVAILABLE = [ + { + request: { + query: GENERATE_OTP_MUTATION, + variables: { + email: 'johndoe@gmail.com', }, }, + error: new Error('Failed to fetch'), }, ]; + const link = new StaticMockLink(MOCKS, true); +const notWorkingLink = new StaticMockLink([], true); +const talawaApiUnavailableLink = new StaticMockLink( + MOCKS_INTERNET_UNAVAILABLE, + true +); + async function wait(ms = 100): Promise { await act(() => { return new Promise((resolve) => { @@ -57,6 +70,12 @@ async function wait(ms = 100): Promise { }); }); } + +const translations = JSON.parse( + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain + JSON.stringify(i18n.getDataByLanguage('en')?.translation.forgotPassword!) +); + beforeEach(() => { localStorage.setItem('IsLoggedIn', 'FALSE'); }); @@ -176,6 +195,47 @@ describe('Testing Forgot Password screen', () => { await wait(); }); + test('Testing forgot password functionality, if the otp got deleted from the local storage', async () => { + const formData = { + userOtp: '12345', + newPassword: 'johnDoe', + confirmNewPassword: 'johnDoe', + email: 'johndoe@gmail.com', + }; + + render( + + + + + + + + + + ); + + 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.removeItem('otpToken'); + 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: 'johndoe@gmail.com', @@ -216,6 +276,97 @@ describe('Testing Forgot Password screen', () => { userEvent.click(screen.getByText('Change Password')); }); + test('Testing forgot password functionality, when the user is not found', async () => { + const formData = { + email: 'notexists@gmail.com', + }; + // localStorage.setItem('otpToken', ''); + render( + + + + + + + + + + + ); + + await wait(); + + userEvent.type( + screen.getByPlaceholderText(/Registered email/i), + formData.email + ); + + userEvent.click(screen.getByText('Get OTP')); + await wait(); + + expect( + await screen.findByText(translations.emailNotRegistered) + ).toBeInTheDocument(); + }); + + test('Testing forgot password functionality, when there is an error except unregistered email and api failure', async () => { + const formData = { + userOtp: '12345', + newPassword: 'johnDoe', + confirmNewPassword: 'johnDoe', + email: 'johndoe@gmail.com', + }; + render( + + + + + + + + + + + ); + userEvent.click(screen.getByText('Get OTP')); + await wait(); + + expect( + await screen.findByText(translations.errorSendingMail) + ).toBeInTheDocument(); + }); + + test('Testing forgot password functionality, when talawa api failed', async () => { + const formData = { + email: 'johndoe@gmail.com', + }; + render( + + + + + + + + + + + ); + + await wait(); + + userEvent.type( + screen.getByPlaceholderText(/Registered email/i), + formData.email + ); + userEvent.click(screen.getByText('Get OTP')); + await wait(); + + expect( + await screen.findByText(translations.talawaApiUnavailable) + ).toBeInTheDocument(); + }); + test('Testing forgot password functionality, when otp token is not present', async () => { const formData = { userOtp: '12345',