generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
784f8ac
commit 3d5ddcf
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
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,75 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { MemoryRouter, Routes, Route } from 'react-router-dom'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import ProtectedRoute from '../../routes/ProtectedRoute'; | ||
import { useGetAuth } from '../../contexts/AuthProvider'; | ||
import { Loading } from "@carbon/react"; | ||
|
||
vi.mock('../../contexts/AuthProvider', () => ({ | ||
useGetAuth: vi.fn(), | ||
})); | ||
|
||
vi.mock('@carbon/react', () => ({ | ||
Loading: vi.fn(() => <div>Loading...</div>), | ||
})); | ||
|
||
describe('ProtectedRoute', () => { | ||
it('should render loading component when isLoading is true', () => { | ||
(useGetAuth as vi.Mock).mockReturnValue({ isLoading: true }); | ||
|
||
const { getByText } = render( | ||
<MemoryRouter> | ||
<ProtectedRoute /> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(getByText('Loading...')).toBeDefined(); | ||
}); | ||
|
||
it('should redirect to login when requireAuth is true and user is not logged in', () => { | ||
(useGetAuth as vi.Mock).mockReturnValue({ isLoading: false, isLoggedIn: false }); | ||
|
||
const { container } = render( | ||
<MemoryRouter initialEntries={['/protected']}> | ||
<Routes> | ||
<Route path="/login" element={<div>Login Page</div>} /> | ||
<Route path="/protected" element={<ProtectedRoute requireAuth redirectTo="/login" />} /> | ||
</Routes> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(container.innerHTML).toContain('Login Page'); | ||
}); | ||
|
||
it('should redirect to unauthorized when requiredRoles are not met', () => { | ||
(useGetAuth as vi.Mock).mockReturnValue({ isLoading: false, isLoggedIn: true, userRoles: ['user'] }); | ||
|
||
const { container } = render( | ||
<MemoryRouter initialEntries={['/protected']}> | ||
<Routes> | ||
<Route path="/unauthorized" element={<div>Unauthorized Page</div>} /> | ||
<Route path="/protected" element={<ProtectedRoute requiredRoles={['admin']} />} /> | ||
</Routes> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(container.innerHTML).toContain('Unauthorized Page'); | ||
}); | ||
|
||
it('should render child routes when all checks pass', () => { | ||
(useGetAuth as vi.Mock).mockReturnValue({ isLoading: false, isLoggedIn: true, userRoles: ['admin'] }); | ||
|
||
const { container } = render( | ||
<MemoryRouter initialEntries={['/protected']}> | ||
<Routes> | ||
<Route path="/protected" element={<ProtectedRoute requiredRoles={['admin']} />}> | ||
<Route path="" element={<div>Protected Content</div>} /> | ||
</Route> | ||
</Routes> | ||
</MemoryRouter> | ||
); | ||
|
||
expect(container.innerHTML).toContain('Protected Content'); | ||
}); | ||
}); |