-
-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: SecuredRouteForUser component 100% Test Coverage and fixed unco…
…vered lines (#1048) * SecuredRouteForUser test case added * Fixed Linting Errors * Update SecuredRouteForUser.test.tsx * Update SecuredRouteForUser.test.tsx * linting fix * Update SecuredRouteForUser.test.tsx * Update SecuredRouteForUser.test.tsx
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/components/UserPortal/SecuredRouteForUser/SecuredRouteForUser.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import { MemoryRouter, Route } from 'react-router-dom'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import SecuredRouteForUser from './SecuredRouteForUser'; | ||
|
||
describe('SecuredRouteForUser', () => { | ||
test('renders the route when the user is logged in', () => { | ||
// Set the 'IsLoggedIn' value to 'TRUE' in localStorage to simulate a logged-in user | ||
localStorage.setItem('IsLoggedIn', 'TRUE'); | ||
|
||
render( | ||
<MemoryRouter initialEntries={['/user/organizations']}> | ||
<Route | ||
path="/user/organizations" | ||
render={() => ( | ||
<SecuredRouteForUser | ||
path="/user/organizations" | ||
component={() => ( | ||
<div data-testid="organizations-content"> | ||
Organizations Component | ||
</div> | ||
)} | ||
/> | ||
)} | ||
/> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(screen.getByTestId('organizations-content')).toBeInTheDocument(); | ||
}); | ||
|
||
test('redirects to /user when the user is not logged in', async () => { | ||
// Set the user as not logged in in local storage | ||
localStorage.setItem('IsLoggedIn', 'FALSE'); | ||
|
||
render( | ||
<MemoryRouter initialEntries={['/secured']}> | ||
<Route | ||
path="/secured" | ||
exact | ||
render={() => ( | ||
<SecuredRouteForUser> | ||
<div data-testid="secured-content">Secured Content</div> | ||
</SecuredRouteForUser> | ||
)} | ||
/> | ||
</MemoryRouter> | ||
); | ||
|
||
await waitFor(() => { | ||
expect(window.location.pathname).toBe('/'); | ||
}); | ||
}); | ||
}); |