Skip to content

Commit

Permalink
fixed redirect issue when already logged in#1646
Browse files Browse the repository at this point in the history
  • Loading branch information
kanhaiya04 committed Mar 5, 2024
1 parent e186ed2 commit d6b1543
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
54 changes: 53 additions & 1 deletion src/screens/LoginPage/LoginPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import { act, render, screen, fireEvent } from '@testing-library/react';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { BrowserRouter, MemoryRouter } from 'react-router-dom';
import userEvent from '@testing-library/user-event';
import { I18nextProvider } from 'react-i18next';
import 'jest-localstorage-mock';
Expand All @@ -17,6 +17,7 @@ import {
import { store } from 'state/store';
import i18nForTest from 'utils/i18nForTest';
import { BACKEND_URL } from 'Constant/constant';
import useLocalStorage from 'utils/useLocalstorage';

const MOCKS = [
{
Expand Down Expand Up @@ -79,6 +80,7 @@ const MOCKS = [
];

const link = new StaticMockLink(MOCKS, true);
const mockHistoryPush = jest.fn();

async function wait(ms = 100): Promise<void> {
await act(() => {
Expand All @@ -96,6 +98,13 @@ jest.mock('react-toastify', () => ({
},
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: () => ({
push: mockHistoryPush,
}),
}));

jest.mock('Constant/constant.ts', () => ({
...jest.requireActual('Constant/constant.ts'),
REACT_APP_USE_RECAPTCHA: 'yes',
Expand Down Expand Up @@ -763,3 +772,46 @@ describe('Testing Login Page Screen', () => {
}
});
});

describe('Testing redirect if already logged in', () => {
test('Logged in as USER', async () => {
const { setItem } = useLocalStorage();
setItem('IsLoggedIn', 'TRUE');
setItem('UserType', 'USER');
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<MemoryRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
</I18nextProvider>
</Provider>
</MemoryRouter>
</BrowserRouter>
</MockedProvider>,
);
await wait();
expect(mockHistoryPush).toHaveBeenCalledWith('/user/organizations');
});
test('Logged as in Admin or SuperAdmin', async () => {
const { setItem } = useLocalStorage();
setItem('IsLoggedIn', 'TRUE');
setItem('UserType', 'ADMIN');
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<MemoryRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<LoginPage />
</I18nextProvider>
</Provider>
</MemoryRouter>
</BrowserRouter>
</MockedProvider>,
);
await wait();
expect(mockHistoryPush).toHaveBeenCalledWith('/orglist');
});
});
4 changes: 3 additions & 1 deletion src/screens/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ const loginPage = (): JSX.Element => {
useEffect(() => {
const isLoggedIn = getItem('IsLoggedIn');
if (isLoggedIn == 'TRUE') {
history.push(role === 'admin' ? '/orglist' : '/user/organizations');
history.push(
getItem('UserType') === 'USER' ? '/user/organizations' : '/orglist',
);
}
setComponentLoader(false);
}, []);
Expand Down

0 comments on commit d6b1543

Please sign in to comment.