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

Commit

Permalink
adds Unauthenticated component
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLo123 committed Feb 15, 2024
1 parent 6921f0a commit bba4e34
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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();
});
});
});
14 changes: 14 additions & 0 deletions src/components/Unauthenticated.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { FC, PropsWithChildren } from 'react';
import { useFusionAuth } from '../providers/FusionAuthProvider';

export const Unauthenticated: FC<PropsWithChildren> = (

Check failure on line 4 in src/components/Unauthenticated.tsx

View workflow job for this annotation

GitHub Actions / lint

Type '(props: PropsWithChildren) => React.ReactNode' is not assignable to type 'FC<{ children?: ReactNode; }>'.
props: PropsWithChildren,
) => {
const { isAuthenticated } = useFusionAuth();

if (isAuthenticated) {
return null;
}

return props.children;
};

0 comments on commit bba4e34

Please sign in to comment.