refactor(tests): simplify waitFor usage in useAuth tests

This commit is contained in:
2026-01-07 16:07:50 +00:00
parent 3d2fc07026
commit b418fa2203
2 changed files with 8 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
import { act, renderHook } from '@testing-library/react'
import { act, renderHook, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
// Mock Prisma client to prevent instantiation errors in test environment
@@ -11,28 +11,6 @@ vi.mock('@/lib/dbal-client/adapter/get-adapter', () => ({
getAdapter: vi.fn(() => ({})),
}))
import { useAuth } from '@/hooks/useAuth'
import { fetchSession } from '@/lib/auth/api/fetch-session'
import { login as loginRequest } from '@/lib/auth/api/login'
import { logout as logoutRequest } from '@/lib/auth/api/logout'
import type { User } from '@/lib/level-types'
// Simple waitFor implementation
const waitFor = async (callback: () => boolean | void, timeout = 1000) => {
const start = Date.now()
while (Date.now() - start < timeout) {
try {
const result = callback()
if (result === false) continue
return
} catch {
await new Promise(resolve => setTimeout(resolve, 50))
}
}
throw new Error('waitFor timed out')
}
vi.mock('@/lib/auth/api/fetch-session', () => ({
fetchSession: vi.fn(),
}))
@@ -45,6 +23,12 @@ vi.mock('@/lib/auth/api/logout', () => ({
logout: vi.fn(),
}))
import { useAuth } from '@/hooks/useAuth'
import { fetchSession } from '@/lib/auth/api/fetch-session'
import { login as loginRequest } from '@/lib/auth/api/login'
import { logout as logoutRequest } from '@/lib/auth/api/logout'
import type { User } from '@/lib/level-types'
const mockFetchSession = vi.mocked(fetchSession)
const mockLogin = vi.mocked(loginRequest)
const mockLogout = vi.mocked(logoutRequest)

View File

@@ -1,4 +1,4 @@
import { act, renderHook } from '@testing-library/react'
import { act, renderHook, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useAuth } from '@/hooks/useAuth'
@@ -8,21 +8,6 @@ import { logout as logoutRequest } from '@/lib/auth/api/logout'
import { register as registerRequest } from '@/lib/auth/api/register'
import type { User } from '@/lib/level-types'
// Simple waitFor implementation
const waitFor = async (callback: () => boolean | void, timeout = 1000) => {
const start = Date.now()
while (Date.now() - start < timeout) {
try {
const result = callback()
if (result === false) continue
return
} catch {
await new Promise(resolve => setTimeout(resolve, 50))
}
}
throw new Error('waitFor timed out')
}
vi.mock('@/lib/auth/api/fetch-session', () => ({
fetchSession: vi.fn(),
}))