-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create an utility component to render content only for authenti…
…cated users Refs: SHELL-265
- Loading branch information
Showing
4 changed files
with
69 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
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,38 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import React from 'react'; | ||
|
||
import { AuthGuard } from './auth-guard'; | ||
import * as accountStoreHooks from '../store/account/hooks'; | ||
import { setup, screen } from '../tests/utils'; | ||
|
||
describe('AuthGuard', () => { | ||
it('should render the child component when the user is authenticated', () => { | ||
const useAuthenticated = jest.spyOn(accountStoreHooks, 'useAuthenticated'); | ||
useAuthenticated.mockReturnValue(true); | ||
|
||
setup( | ||
<AuthGuard> | ||
<p>Authenticated</p> | ||
</AuthGuard> | ||
); | ||
|
||
expect(screen.getByText('Authenticated')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not render the child component when the user is authenticated', () => { | ||
const useAuthenticated = jest.spyOn(accountStoreHooks, 'useAuthenticated'); | ||
useAuthenticated.mockReturnValue(false); | ||
|
||
setup( | ||
<AuthGuard> | ||
<p>Authenticated</p> | ||
</AuthGuard> | ||
); | ||
|
||
expect(screen.queryByText('Authenticated')).not.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
import type { ReactNode } from 'react'; | ||
import React from 'react'; | ||
|
||
import { useAuthenticated } from '../store/account'; | ||
|
||
export type AuthGuardProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
export const AuthGuard = ({ children }: AuthGuardProps): ReactNode => { | ||
const isAuthenticated = useAuthenticated(); | ||
|
||
return isAuthenticated ? <>{children}</> : null; | ||
}; |