Migrate auth hooks to AuthContext and update tests for compatibility

- Refactored `useIsAuthenticated` and `useCurrentUser` to use `useAuth` from `AuthContext` instead of `useAuthStore`.
- Updated test setups to inject `AuthProvider` with mocked store hooks for improved test isolation and consistency.
- Replaced legacy `useAuthStore` mocks with `AuthContext`-compatible implementations in affected tests.
This commit is contained in:
Felipe Cardoso
2025-11-03 14:27:25 +01:00
parent 532577f36c
commit 852c7eceff
3 changed files with 53 additions and 19 deletions

View File

@@ -6,12 +6,40 @@
import { render, screen } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import ProfileSettingsPage from '@/app/(authenticated)/settings/profile/page';
import { AuthProvider } from '@/lib/auth/AuthContext';
import { useAuthStore } from '@/lib/stores/authStore';
// Mock authStore
jest.mock('@/lib/stores/authStore');
const mockUseAuthStore = useAuthStore as jest.MockedFunction<typeof useAuthStore>;
// Mock store hook for AuthProvider
const mockStoreHook = ((selector?: (state: any) => any) => {
const state = {
isAuthenticated: true,
user: {
id: '1',
email: 'test@example.com',
first_name: 'Test',
last_name: 'User',
is_active: true,
is_superuser: false,
created_at: '2024-01-01T00:00:00Z',
},
accessToken: 'token',
refreshToken: 'refresh',
isLoading: false,
tokenExpiresAt: null,
setAuth: jest.fn(),
setTokens: jest.fn(),
setUser: jest.fn(),
clearAuth: jest.fn(),
loadAuthFromStorage: jest.fn(),
isTokenExpired: jest.fn(() => false),
};
return selector ? selector(state) : state;
}) as any;
describe('ProfileSettingsPage', () => {
const queryClient = new QueryClient({
defaultOptions: {
@@ -44,7 +72,9 @@ describe('ProfileSettingsPage', () => {
const renderWithProvider = (component: React.ReactElement) => {
return render(
<QueryClientProvider client={queryClient}>
{component}
<AuthProvider store={mockStoreHook}>
{component}
</AuthProvider>
</QueryClientProvider>
);
};

View File

@@ -11,28 +11,29 @@ import {
useCurrentUser,
useIsAdmin,
} from '@/lib/api/hooks/useAuth';
import { AuthProvider } from '@/lib/auth/AuthContext';
// Mock auth store
let mockAuthState: {
isAuthenticated: boolean;
user: any;
accessToken: string | null;
refreshToken: string | null;
} = {
// Mock auth state (Context-injected)
let mockAuthState: any = {
isAuthenticated: false,
user: null,
accessToken: null,
refreshToken: null,
isLoading: false,
tokenExpiresAt: null,
// Action stubs (unused in these tests)
setAuth: jest.fn(),
setTokens: jest.fn(),
setUser: jest.fn(),
clearAuth: jest.fn(),
loadAuthFromStorage: jest.fn(),
isTokenExpired: jest.fn(() => false),
};
jest.mock('@/lib/stores/authStore', () => ({
useAuthStore: (selector?: (state: any) => any) => {
if (selector) {
return selector(mockAuthState);
}
return mockAuthState;
},
}));
// Mock store hook compatible with AuthContext (Zustand-like hook)
const mockStoreHook = ((selector?: (state: any) => any) => {
return selector ? selector(mockAuthState) : mockAuthState;
}) as any;
// Mock router
jest.mock('next/navigation', () => ({
@@ -51,7 +52,9 @@ const createWrapper = () => {
return ({ children }: { children: React.ReactNode }) => (
<QueryClientProvider client={queryClient}>
{children}
<AuthProvider store={mockStoreHook}>
{children}
</AuthProvider>
</QueryClientProvider>
);
};