Skip to content

Commit

Permalink
Fix: Improve code coverage in src/screens/forgot password (#3109)
Browse files Browse the repository at this point in the history
* Refactor: Migrated src/screens/ForgotPassword/ForgotPassword.tsx from Jest to Vitest

* Fix: Improved Code Coverage in src/screens/ForgotPassword/ForgotPassword.tsx
  • Loading branch information
PratapRathi authored Jan 1, 2025
1 parent a60179f commit 9e43fa5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@ import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { act, render, screen, waitFor } from '@testing-library/react';
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 { toast, ToastContainer } from 'react-toastify';

import { GENERATE_OTP_MUTATION } from 'GraphQl/Mutations/mutations';
import { toast } from 'react-toastify';
import {
FORGOT_PASSWORD_MUTATION,
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';
import useLocalStorage from 'utils/useLocalstorage';
import { vi, beforeEach, afterEach, expect, it, describe } from 'vitest';

const { setItem, removeItem } = useLocalStorage();

jest.mock('react-toastify', () => ({
vi.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
success: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
},
}));

const MOCKS = [
{
request: {
query: FORGOT_PASSWORD_MUTATION,
variables: {
otpToken: 'lorem ipsum',
userOtp: '12345',
newPassword: 'johnDoe@12345',
},
},
result: {
data: {
forgotPassword: true,
},
},
},

{
request: {
query: GENERATE_OTP_MUTATION,
Expand Down Expand Up @@ -100,8 +117,8 @@ afterEach(() => {
});

describe('Testing Forgot Password screen', () => {
test('Component should be rendered properly', async () => {
window.location.assign('/orglist');
it('Component should be rendered properly', async () => {
window.history.pushState({}, 'Test page', '/orglist');

render(
<MockedProvider addTypename={false} link={link}>
Expand All @@ -121,10 +138,10 @@ describe('Testing Forgot Password screen', () => {
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');
expect(window.location.pathname).toBe('/orglist');
});

test('Testing, If user is already loggedIn', async () => {
it('Testing, If user is already loggedIn', async () => {
setItem('IsLoggedIn', 'TRUE');

render(
Expand All @@ -142,7 +159,7 @@ describe('Testing Forgot Password screen', () => {
await wait();
});

test('Testing get OTP functionality', async () => {
it('Testing get OTP functionality', async () => {
const formData = {
email: '[email protected]',
};
Expand Down Expand Up @@ -172,11 +189,11 @@ describe('Testing Forgot Password screen', () => {
});
});

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

Expand Down Expand Up @@ -213,7 +230,7 @@ describe('Testing Forgot Password screen', () => {
await wait();
});

test('Testing forgot password functionality, if the otp got deleted from the local storage', async () => {
it('Testing forgot password functionality, if the otp got deleted from the local storage', async () => {
const formData = {
userOtp: '12345',
newPassword: 'johnDoe',
Expand Down Expand Up @@ -254,7 +271,7 @@ describe('Testing Forgot Password screen', () => {
await wait();
});

test('Testing forgot password functionality, when new password and confirm password is not same', async () => {
it('Testing forgot password functionality, when new password and confirm password is not same', async () => {
const formData = {
email: '[email protected]',
userOtp: '12345',
Expand Down Expand Up @@ -294,7 +311,7 @@ describe('Testing Forgot Password screen', () => {
userEvent.click(screen.getByText('Change Password'));
});

test('Testing forgot password functionality, when the user is not found', async () => {
it('Testing forgot password functionality, when the user is not found', async () => {
const formData = {
email: '[email protected]',
};
Expand Down Expand Up @@ -324,7 +341,7 @@ describe('Testing Forgot Password screen', () => {
});
});

test('Testing forgot password functionality, when there is an error except unregistered email and api failure', async () => {
it('Testing forgot password functionality, when there is an error except unregistered email and api failure', async () => {
render(
<MockedProvider addTypename={false} link={notWorkingLink}>
<BrowserRouter>
Expand All @@ -342,7 +359,7 @@ describe('Testing Forgot Password screen', () => {
});
});

test('Testing forgot password functionality, when talawa api failed', async () => {
it('Testing forgot password functionality, when talawa api failed', async () => {
const formData = {
email: '[email protected]',
};
Expand Down Expand Up @@ -372,7 +389,7 @@ describe('Testing Forgot Password screen', () => {
});
});

test('Testing forgot password functionality, when otp token is not present', async () => {
it('Testing forgot password functionality, when otp token is not present', async () => {
const formData = {
userOtp: '12345',
newPassword: 'johnDoe',
Expand Down
3 changes: 1 addition & 2 deletions src/screens/ForgotPassword/ForgotPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const ForgotPassword = (): JSX.Element => {
},
});

/* istanbul ignore next */
/* istanbul ignore else -- @preserve */
if (data) {
toast.success(t('passwordChanges') as string);
setShowEnterEmail(true);
Expand All @@ -147,7 +147,6 @@ const ForgotPassword = (): JSX.Element => {
}
} catch (error: unknown) {
setShowEnterEmail(true);
/* istanbul ignore next */
errorHandler(t, error);
}
};
Expand Down

0 comments on commit 9e43fa5

Please sign in to comment.