diff --git a/src/screens/LoginPage/LoginPage.test.tsx b/src/screens/LoginPage/LoginPage.test.tsx index 09873042ad..21bbd50ed9 100644 --- a/src/screens/LoginPage/LoginPage.test.tsx +++ b/src/screens/LoginPage/LoginPage.test.tsx @@ -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'; @@ -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 = [ { @@ -79,6 +80,7 @@ const MOCKS = [ ]; const link = new StaticMockLink(MOCKS, true); +const mockHistoryPush = jest.fn(); async function wait(ms = 100): Promise { await act(() => { @@ -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', @@ -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( + + + + + + + + + + + , + ); + 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( + + + + + + + + + + + , + ); + await wait(); + expect(mockHistoryPush).toHaveBeenCalledWith('/orglist'); + }); +}); diff --git a/src/screens/LoginPage/LoginPage.tsx b/src/screens/LoginPage/LoginPage.tsx index c4d2b60d51..391689572f 100644 --- a/src/screens/LoginPage/LoginPage.tsx +++ b/src/screens/LoginPage/LoginPage.tsx @@ -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); }, []);