Skip to content

Commit

Permalink
test: SecuredRouteForUser component 100% Test Coverage and fixed unco…
Browse files Browse the repository at this point in the history
…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
duplixx authored Nov 11, 2023
1 parent ada33be commit b6c2d15
Showing 1 changed file with 54 additions and 0 deletions.
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('/');
});
});
});

0 comments on commit b6c2d15

Please sign in to comment.