-
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
7905f72
commit 3cc4665
Showing
7 changed files
with
176 additions
and
133 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 |
---|---|---|
@@ -1,40 +1,99 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import NavLinks from './NavLinks'; | ||
import SessionProvider, { Session } from '../../../providers/sessionProvider/SessionProvider'; | ||
import { buildUserAuth } from '../../../helpers/test/testBuilders'; | ||
import * as ReactRouter from 'react-router'; | ||
import { createMemoryHistory } from 'history'; | ||
import { act } from 'react-dom/test-utils'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { routes } from '../../../types/generic/routes'; | ||
|
||
const mockedUseNavigate = jest.fn(); | ||
jest.mock('react-router', () => ({ | ||
useNavigate: () => mockedUseNavigate, | ||
})); | ||
|
||
describe('NavLinks', () => { | ||
const oldWindowLocation = window.location; | ||
|
||
beforeEach(() => { | ||
sessionStorage.setItem('UserSession', ''); | ||
process.env.REACT_APP_ENVIRONMENT = 'jest'; | ||
}); | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
window.location = oldWindowLocation; | ||
}); | ||
|
||
it('renders a navlink that returns to app home', () => { | ||
renderNavWithRouter(); | ||
describe('Rendering', () => { | ||
it('renders a navlink for app home when user logged in', () => { | ||
const isLoggedIn = true; | ||
renderNav(isLoggedIn); | ||
|
||
expect(screen.getByRole('link', { name: 'Home' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders a navlink for app logout when user logged in', () => { | ||
const isLoggedIn = true; | ||
renderNav(isLoggedIn); | ||
|
||
expect(screen.getByRole('link', { name: 'Log Out' })).toBeInTheDocument(); | ||
}); | ||
it('does not render a navlink for app home when user logged out', () => { | ||
const isLoggedIn = false; | ||
renderNav(isLoggedIn); | ||
|
||
expect(screen.queryByRole('link', { name: 'Home' })).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render a navlink for app logout when user logged out', () => { | ||
const isLoggedIn = false; | ||
renderNav(isLoggedIn); | ||
|
||
expect(screen.queryByRole('link', { name: 'Log Out' })).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('Navigation', () => { | ||
it('navigates to app home when home link is clicked', async () => { | ||
const isLoggedIn = true; | ||
renderNav(isLoggedIn); | ||
|
||
const homeLink = screen.getByRole('link', { name: 'Home' }); | ||
expect(homeLink).toBeInTheDocument(); | ||
|
||
act(() => { | ||
userEvent.click(homeLink); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.HOME); | ||
}); | ||
}); | ||
|
||
expect(screen.getByRole('link', { name: 'Home' })).toBeInTheDocument(); | ||
it('navigates to app logout when logout link is clicked', async () => { | ||
const isLoggedIn = true; | ||
renderNav(isLoggedIn); | ||
|
||
const logoutLink = screen.getByRole('link', { name: 'Log Out' }); | ||
expect(logoutLink).toBeInTheDocument(); | ||
|
||
act(() => { | ||
userEvent.click(logoutLink); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(mockedUseNavigate).toHaveBeenCalledWith(routes.LOGOUT); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
const renderNavWithRouter = (authOverride?: Partial<Session>) => { | ||
const renderNav = (isLoggedIn: boolean) => { | ||
const auth: Session = { | ||
auth: buildUserAuth(), | ||
isLoggedIn: true, | ||
...authOverride, | ||
}; | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/'], | ||
initialIndex: 1, | ||
}); | ||
render( | ||
<ReactRouter.Router navigator={history} location={'/'}> | ||
<SessionProvider sessionOverride={auth}> | ||
<NavLinks /> | ||
</SessionProvider> | ||
</ReactRouter.Router>, | ||
<SessionProvider sessionOverride={isLoggedIn ? auth : undefined}> | ||
<NavLinks /> | ||
</SessionProvider>, | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import * as ReactRouter from 'react-router'; | ||
import { createMemoryHistory } from 'history'; | ||
import AuthErrorPage from './AuthErrorPage'; | ||
import { LinkProps } from 'react-router-dom'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
__esModule: true, | ||
Link: (props: LinkProps) => <a {...props} role="link" />, | ||
})); | ||
describe('AuthErrorPage', () => { | ||
it('renders unauthorised message', () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/'], | ||
initialIndex: 0, | ||
}); | ||
render( | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<AuthErrorPage /> | ||
</ReactRouter.Router>, | ||
); | ||
render(<AuthErrorPage />); | ||
expect(screen.getByText('You have been logged out')).toBeInTheDocument(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,18 +1,14 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import * as ReactRouter from 'react-router'; | ||
import { createMemoryHistory } from 'history'; | ||
import NotFoundPage from './NotFoundPage'; | ||
import { LinkProps } from 'react-router-dom'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
__esModule: true, | ||
Link: (props: LinkProps) => <a {...props} role="link" />, | ||
})); | ||
describe('NotFoundPage', () => { | ||
it('renders unauthorised message', () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/'], | ||
initialIndex: 0, | ||
}); | ||
render( | ||
<ReactRouter.Router navigator={history} location={history.location}> | ||
<NotFoundPage /> | ||
</ReactRouter.Router>, | ||
); | ||
render(<NotFoundPage />); | ||
expect(screen.getByText('Page not found')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.