fix(auth): fix auth provider test - check token before rendering hook
This commit is contained in:
parent
33272327d8
commit
81b4acfb40
@ -224,13 +224,7 @@ describe('AuthProvider', () => {
|
|||||||
|
|
||||||
describe('authentication state', () => {
|
describe('authentication state', () => {
|
||||||
it('should correctly report isAuthenticated status', async () => {
|
it('should correctly report isAuthenticated status', async () => {
|
||||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
// Set token BEFORE rendering hook so AuthProvider sees it on mount
|
||||||
<AuthProvider>{children}</AuthProvider>
|
|
||||||
)
|
|
||||||
const { result } = renderHook(() => useAuth(), { wrapper })
|
|
||||||
|
|
||||||
expect(result.current.isAuthenticated).toBe(false)
|
|
||||||
|
|
||||||
localStorage.setItem('auth_token', 'test-token')
|
localStorage.setItem('auth_token', 'test-token')
|
||||||
const mockUser = { id: 1, name: 'Test', email: 'test@example.com', created_at: '', updated_at: '' }
|
const mockUser = { id: 1, name: 'Test', email: 'test@example.com', created_at: '', updated_at: '' }
|
||||||
;(apiClient.get as jest.Mock).mockResolvedValueOnce({
|
;(apiClient.get as jest.Mock).mockResolvedValueOnce({
|
||||||
@ -238,9 +232,22 @@ describe('AuthProvider', () => {
|
|||||||
data: mockUser,
|
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(() => {
|
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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user