generated from NHSDigital/nhs-notify-repository-template
-
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
4cfbb23
commit ddc6406
Showing
13 changed files
with
229 additions
and
83 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,3 +1,4 @@ | ||
# SEE: https://github.com/gitleaks/gitleaks/blob/master/README.md#gitleaksignore | ||
|
||
cd9c0efec38c5d63053dd865e5d4e207c0760d91:docs/guides/Perform_static_analysis.md:generic-api-key:37 | ||
src/__tests__/components/molecules/LoginStatus.test.tsx:jwt:23 |
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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MockAuthPage 1`] = ` | ||
<DocumentFragment> | ||
<p> | ||
redirect | ||
</p> | ||
</DocumentFragment> | ||
`; |
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,19 @@ | ||
import { ReactNode, ComponentType } from 'react'; | ||
import MockAuthPage from '@app/auth/page.dev'; | ||
import { render } from '@testing-library/react'; | ||
|
||
jest.mock('@aws-amplify/ui-react', () => ({ | ||
Authenticator: { | ||
Provider: ({ children }: { children: ReactNode }) => children, | ||
}, | ||
withAuthenticator: (Component: ComponentType) => Component, | ||
})); | ||
jest.mock('@molecules/Redirect/Redirect', () => ({ | ||
Redirect: () => <p>redirect</p>, | ||
})); | ||
|
||
test('MockAuthPage', () => { | ||
const container = render(<MockAuthPage />); | ||
|
||
expect(container.asFragment()).toMatchSnapshot(); | ||
}); |
9 changes: 9 additions & 0 deletions
9
src/__tests__/app/auth/signout/__snapshots__/page.test.tsx.snap
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,9 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MockSignoutPage 1`] = ` | ||
<DocumentFragment> | ||
<p> | ||
redirect | ||
</p> | ||
</DocumentFragment> | ||
`; |
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,29 @@ | ||
import { ReactNode, ComponentType } from 'react'; | ||
import MockSignoutPage from '@app/auth/signout/page.dev'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
jest.mock('@aws-amplify/auth', () => ({ | ||
signOut: () => Promise.resolve(), | ||
})); | ||
|
||
jest.mock('@molecules/Redirect/Redirect', () => ({ | ||
Redirect: () => <p>redirect</p>, | ||
})); | ||
|
||
jest.mock('@aws-amplify/ui-react', () => ({ | ||
Authenticator: { | ||
Provider: ({ children }: { children: ReactNode }) => children, | ||
}, | ||
withAuthenticator: (Component: ComponentType) => Component, | ||
})); | ||
jest.mock('@molecules/Redirect/Redirect', () => ({ | ||
Redirect: () => <p>redirect</p>, | ||
})); | ||
|
||
test('MockSignoutPage', async () => { | ||
const container = render(<MockSignoutPage />); | ||
|
||
await screen.findByText('redirect'); | ||
|
||
expect(container.asFragment()).toMatchSnapshot(); | ||
}); |
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,28 @@ | ||
/* eslint-disable unicorn/no-document-cookie */ | ||
import { render } from '@testing-library/react'; | ||
import { LoginStatus } from '@molecules/LoginStatus/LoginStatus'; | ||
|
||
test('LoginStatus - no cookie', () => { | ||
document.cookie = ''; | ||
|
||
const container = render(<LoginStatus />); | ||
|
||
expect(container.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('LoginStatus - invalid cookie', () => { | ||
document.cookie = 'CognitoIdentityServiceProvider.idToken=lemons'; | ||
|
||
const container = render(<LoginStatus />); | ||
|
||
expect(container.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('LoginStatus - valid cookie', () => { | ||
document.cookie = | ||
'CognitoIdentityServiceProvider.idToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImxvY2FsaG9zdEBuaHMubmV0In0.R0rk7pjJoU07efveI4p6W-xrTM-BnP8N-pU-RYczPBA'; | ||
|
||
const container = render(<LoginStatus />); | ||
|
||
expect(container.asFragment()).toMatchSnapshot(); | ||
}); |
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,40 @@ | ||
import { mockDeep } from 'jest-mock-extended'; | ||
import { render } from '@testing-library/react'; | ||
import { Redirect } from '@molecules/Redirect/Redirect'; | ||
import { | ||
useSearchParams, | ||
ReadonlyURLSearchParams, | ||
redirect, | ||
} from 'next/navigation'; | ||
|
||
jest.mock('next/navigation', () => ({ | ||
...jest.requireActual('next/navigation'), | ||
redirect: jest.fn(), | ||
useSearchParams: jest.fn(), | ||
})); | ||
|
||
test('Redirect - URL provided', () => { | ||
const mockRedirect = jest.fn(mockDeep<typeof redirect>()); | ||
jest.mocked(redirect).mockImplementation(mockRedirect); | ||
|
||
const mockSearchParams = new ReadonlyURLSearchParams({ | ||
redirect: 'redirect', | ||
}); | ||
jest.mocked(useSearchParams).mockReturnValue(mockSearchParams); | ||
|
||
render(<Redirect />); | ||
|
||
expect(mockRedirect).toHaveBeenCalledWith('redirect'); | ||
}); | ||
|
||
test('Redirect - URL not provided', () => { | ||
const mockRedirect = jest.fn(mockDeep<typeof redirect>()); | ||
jest.mocked(redirect).mockImplementation(mockRedirect); | ||
|
||
const mockSearchParams = new ReadonlyURLSearchParams({}); | ||
jest.mocked(useSearchParams).mockReturnValue(mockSearchParams); | ||
|
||
render(<Redirect />); | ||
|
||
expect(mockRedirect).toHaveBeenCalledWith('/'); | ||
}); |
55 changes: 55 additions & 0 deletions
55
src/__tests__/components/molecules/__snapshots__/LoginStatus.test.tsx.snap
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,55 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`LoginStatus - invalid cookie 1`] = ` | ||
<DocumentFragment> | ||
<li | ||
class="nhsuk-header__navigation-item" | ||
> | ||
<a | ||
class="nhsuk-header__navigation-link" | ||
href="/auth~featuredomain-testing?redirect=%2Ftemplatesnull" | ||
> | ||
Sign in | ||
</a> | ||
</li> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`LoginStatus - no cookie 1`] = ` | ||
<DocumentFragment> | ||
<li | ||
class="nhsuk-header__navigation-item" | ||
> | ||
<a | ||
class="nhsuk-header__navigation-link" | ||
href="/auth~featuredomain-testing?redirect=%2Ftemplatesnull" | ||
> | ||
Sign in | ||
</a> | ||
</li> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`LoginStatus - valid cookie 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="nhsuk-header__transactional-service-name" | ||
> | ||
<a | ||
class="nhsuk-header__transactional-service-name--link" | ||
> | ||
[email protected] | ||
</a> | ||
</div> | ||
<li | ||
class="nhsuk-header__navigation-item" | ||
> | ||
<a | ||
class="nhsuk-header__navigation-link" | ||
href="/auth~featuredomain-testing/signout?redirect=%2Ftemplatesnull" | ||
> | ||
Sign out | ||
</a> | ||
</li> | ||
</DocumentFragment> | ||
`; |
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
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
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,12 @@ | ||
'use client'; | ||
|
||
import { getBasePath } from '@utils/get-base-path'; | ||
import { useSearchParams, redirect } from 'next/navigation'; | ||
|
||
export const Redirect = () => { | ||
const searchParams = useSearchParams(); | ||
|
||
const redirectPath = searchParams.get('redirect') ?? '/'; | ||
|
||
redirect(redirectPath.replace(getBasePath(), '')); | ||
}; |