From 81b4acfb40f4913f40fe44fceee52b780178e8e1 Mon Sep 17 00:00:00 2001 From: Alexis Bruneteau Date: Fri, 17 Oct 2025 23:34:51 +0200 Subject: [PATCH] fix(auth): fix auth provider test - check token before rendering hook --- components/auth/auth-provider.test.tsx | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/components/auth/auth-provider.test.tsx b/components/auth/auth-provider.test.tsx index 63a538e..3020619 100644 --- a/components/auth/auth-provider.test.tsx +++ b/components/auth/auth-provider.test.tsx @@ -224,13 +224,7 @@ describe('AuthProvider', () => { describe('authentication state', () => { it('should correctly report isAuthenticated status', async () => { - const wrapper = ({ children }: { children: React.ReactNode }) => ( - {children} - ) - const { result } = renderHook(() => useAuth(), { wrapper }) - - expect(result.current.isAuthenticated).toBe(false) - + // Set token BEFORE rendering hook so AuthProvider sees it on mount localStorage.setItem('auth_token', 'test-token') const mockUser = { id: 1, name: 'Test', email: 'test@example.com', created_at: '', updated_at: '' } ;(apiClient.get as jest.Mock).mockResolvedValueOnce({ @@ -238,9 +232,22 @@ describe('AuthProvider', () => { data: mockUser, }) + const wrapper = ({ children }: { children: React.ReactNode }) => ( + {children} + ) + const { result } = renderHook(() => useAuth(), { wrapper }) + + // Should be loading initially + expect(result.current.isLoading).toBe(true) + + // After loading completes await waitFor(() => { - expect(result.current.isAuthenticated).toBe(true) + expect(result.current.isLoading).toBe(false) }) + + // Token and authentication should be set + expect(result.current.token).toBe('test-token') + expect(result.current.isAuthenticated).toBe(true) }) }) })