-
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
273887f
commit 05366e9
Showing
3 changed files
with
70 additions
and
9 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
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,59 @@ | ||
import { render, waitFor } from '@testing-library/react'; | ||
import * as ReactRouter from 'react-router'; | ||
import { History, createMemoryHistory } from 'history'; | ||
import { routes } from '../../../types/generic/routes'; | ||
import RoleGuard from './RoleGuard'; | ||
import useRole from '../../../helpers/hooks/useRole'; | ||
import { REPOSITORY_ROLE } from '../../../types/generic/authRole'; | ||
|
||
jest.mock('../../../helpers/hooks/useRole'); | ||
const mockedUseRole = useRole as jest.Mock; | ||
|
||
const guardPage = routes.LLOYD_GEORGE; | ||
describe('RoleGuard', () => { | ||
beforeEach(() => { | ||
process.env.REACT_APP_ENVIRONMENT = 'jest'; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('navigates user to unauthorised when role is not accepted', async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
|
||
mockedUseRole.mockReturnValue(REPOSITORY_ROLE.PCSE); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(routes.UNAUTHORISED); | ||
}); | ||
}); | ||
|
||
it('navigates user to correct page when role is accepted', async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: [guardPage], | ||
initialIndex: 0, | ||
}); | ||
|
||
mockedUseRole.mockReturnValue(REPOSITORY_ROLE.GP_ADMIN); | ||
expect(history.location.pathname).toBe(guardPage); | ||
renderAuthGuard(history); | ||
|
||
await waitFor(async () => { | ||
expect(history.location.pathname).toBe(guardPage); | ||
}); | ||
}); | ||
}); | ||
|
||
const renderAuthGuard = (history: History) => { | ||
return render( | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<RoleGuard> | ||
<div /> | ||
</RoleGuard> | ||
</ReactRouter.Router>, | ||
); | ||
}; |
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