Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for ForgotPassword.tsx #1100

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 160 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,91 @@ 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 () => {
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
Loading