Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

adds Unauthenticated component #86

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/__tests__/Unauthenticated.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { Unauthenticated } from '../components/Unauthenticated';
import { FusionAuthProvider } from '../providers/FusionAuthProvider';
import { TEST_CONFIG } from './mocks/testConfig';

describe('FusionAuthLogoutButton', () => {
afterEach(() => {
jest.clearAllMocks();
});

test('Only renders children if not authenticated', async () => {
// set up provider to fetch user, which will authenticate
Object.defineProperty(document, 'cookie', {
writable: true,
value: `app.idt=abc123;`,
});
const mockUser = { name: 'AuthGuy5000' };
const response = {
ok: true,
json: () => Promise.resolve(mockUser),
} as Response;
jest.spyOn(global, 'fetch').mockResolvedValue(response);

const contentForTheUnauthenticated = 'for unauthenticated eyes only';
const { queryByText } = render(
<FusionAuthProvider {...TEST_CONFIG}>
<Unauthenticated>
<p>{contentForTheUnauthenticated}</p>
</Unauthenticated>
</FusionAuthProvider>,
);

// content appears before user is fetched
expect(queryByText(contentForTheUnauthenticated)).toBeInTheDocument();

// user is authenticated -- content disappears
await waitFor(() => {
expect(
queryByText(contentForTheUnauthenticated),
).not.toBeInTheDocument();
});
});
});
8 changes: 8 additions & 0 deletions src/components/Unauthenticated.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React, { FC, PropsWithChildren } from 'react';
import { useFusionAuth } from '../providers/FusionAuthProvider';

export const Unauthenticated: FC<PropsWithChildren> = props => {
const { isAuthenticated } = useFusionAuth();

return isAuthenticated ? null : <>{props.children}</>;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './providers/FusionAuthProvider';

export * from './components/FusionAuthLoginButton';
export * from './components/FusionAuthLogoutButton';
export * from './components/FusionAuthRegisterButton';
export * from './components/RequireAuth';
export * from './components/Unauthenticated';
export * from './components/withFusionAuth';
Loading