Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouder committed Aug 23, 2023
1 parent 2a0c25b commit a829340
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
27 changes: 0 additions & 27 deletions src/hooks/use-auth.test.ts

This file was deleted.

69 changes: 69 additions & 0 deletions src/hooks/use-auth.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import keycloak from '@src/utils/keycloak';
import { act, renderHook } from '@testing-library/react';
import { AuthProvider } from 'react-oidc-context';
import { RecoilRoot } from 'recoil';
import useAuth from './use-auth';

interface ContextWrapperProps {
children: React.ReactNode;
}

describe('useAuth', () => {
const OLD_ENV = process.env;
beforeEach(() => {
process.env = { ...OLD_ENV };
});

const contextWrapper = ({ children }: ContextWrapperProps) => (
<AuthProvider {...keycloak}>
<RecoilRoot>{children}</RecoilRoot>
</AuthProvider>
);

test('should call signIn successfully', () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

act(() => {
result.current.signIn(false);
});
expect(result.current.signIn).toBeTruthy();
});

test('should call signIn with SSO and no configs', () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

act(() => {
result.current.signIn(true);
});
expect(result.current.signIn).toBeTruthy();
});

test('should call signIn with SSO and available configs', () => {
process.env.SSO_AUTHORITY = 'http://localhost';
process.env.SSO_CLIENT_ID = 'dev-client';

const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

act(() => {
result.current.signIn(true);
});
expect(result.current.signIn).toBeTruthy();
});

test('should call signOut successfully', () => {
const { result } = renderHook(() => useAuth(), {
wrapper: contextWrapper,
});

act(() => {
result.current.signOut();
});
expect(result.current.signOut).toBeTruthy();
});
});

0 comments on commit a829340

Please sign in to comment.