Skip to content

Commit

Permalink
Added test for ForgotPassword.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
aarishshahmohsin committed Nov 29, 2023
1 parent 655b6b3 commit ea896dd
Showing 1 changed file with 166 additions and 15 deletions.
181 changes: 166 additions & 15 deletions src/screens/ForgotPassword/ForgotPassword.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,75 @@ 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: '[email protected]',
},
},
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: '[email protected]',
},
},
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<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}

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');
});
Expand Down Expand Up @@ -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: '[email protected]',
};

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ForgotPassword />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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: '[email protected]',
Expand Down Expand Up @@ -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: '[email protected]',
};
// localStorage.setItem('otpToken', '');
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<ForgotPassword />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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: '[email protected]',
};
render(
<MockedProvider addTypename={false} link={notWorkingLink}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<ForgotPassword />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);
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: '[email protected]',
};
render(
<MockedProvider addTypename={false} link={talawaApiUnavailableLink}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<ToastContainer />
<ForgotPassword />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>
);

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',
Expand Down

0 comments on commit ea896dd

Please sign in to comment.