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>
);
};