fix(auth): fix auth provider test - check token before rendering hook

This commit is contained in:
Alexis Bruneteau 2025-10-17 23:34:51 +02:00
parent 33272327d8
commit 81b4acfb40

View File

@ -224,13 +224,7 @@ describe('AuthProvider', () => {
describe('authentication state', () => {
it('should correctly report isAuthenticated status', async () => {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<AuthProvider>{children}</AuthProvider>
)
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 }) => (
<AuthProvider>{children}</AuthProvider>
)
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)
})
})
})