Skip to content

Commit

Permalink
auth-redirect hook test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mairisb committed Jan 22, 2024
1 parent 7876a2a commit 4ce594f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/client/src/features/auth/hooks/auth-redirect.hook.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { renderHook } from '@testing-library/react';
import { useNavigate } from 'react-router-dom';
import { useAuthRedirect } from './auth-redirect.hook';
import { RootStore, useRootStore } from '../../../core/root.store';
import { AuthStore } from '../auth.store';

jest.mock('react-router-dom', () => ({
useNavigate: jest.fn(),
}));

jest.mock('../../../core/root.store', () => ({
...jest.requireActual('../../../core/root.store'),
useRootStore: jest.fn(),
}));

describe('useAuthRedirect', () => {
let navigate: jest.Mock;

Expand All @@ -15,12 +22,20 @@ describe('useAuthRedirect', () => {
});

it('redirects to the specified path when the user is logged in', () => {
const rootStore = new RootStore();
rootStore.authStore.isAuthenticated = true;
(useRootStore as jest.Mock).mockReturnValue(rootStore);

renderHook(() => useAuthRedirect('/test'));

expect(navigate).toHaveBeenCalledWith('/test');
});

it('does not redirect when the user is not logged in', () => {
const rootStore = new RootStore();
rootStore.authStore.isAuthenticated = false;
(useRootStore as jest.Mock).mockReturnValue(rootStore);

renderHook(() => useAuthRedirect('/test'));

expect(navigate).not.toHaveBeenCalled();
Expand Down

0 comments on commit 4ce594f

Please sign in to comment.