39246-vm/frontend/__tests__/useCountdown.test.ts
abbashkyt-creator 7d8ce0e322 V0.1
2026-03-14 04:02:22 +03:00

35 lines
958 B
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { useCountdown } from '../hooks/useCountdown'
vi.mock('../lib/api/listings', () => ({
fetchCountdownSync: vi.fn().mockResolvedValue([
{ id: 1, time_left_mins: 30 },
{ id: 2, time_left_mins: 5 },
]),
}))
describe('useCountdown', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('returns null for unknown id before sync', () => {
const { result } = renderHook(() => useCountdown())
expect(result.current(999)).toBeNull()
})
it('returns mins for known id after sync', async () => {
const { result } = renderHook(() => useCountdown())
await act(async () => {
// Wait for the initial fetch to complete
await new Promise(resolve => setTimeout(resolve, 50))
})
const mins = result.current(1)
expect(mins).not.toBeNull()
expect(mins).toBeCloseTo(30, 0)
})
})