useLongRunningTransitionState.test.ts53 lines · main
1import { act, renderHook } from '@testing-library/react'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3
4import { useLongRunningTransitionState } from '../useLongRunningTransitionState'
5
6describe('useLongRunningTransitionState', () => {
7 beforeEach(() => {
8 vi.useFakeTimers()
9 vi.setSystemTime(new Date('2026-01-01T00:00:00.000Z'))
10 window.localStorage.clear()
11 })
12
13 afterEach(() => {
14 vi.useRealTimers()
15 window.localStorage.clear()
16 })
17
18 it('immediately marks the transition as long-running when the persisted timer has already elapsed', () => {
19 const storageKey = 'project-transition-start'
20 window.localStorage.setItem(storageKey, String(Date.now() - 61_000))
21
22 const { result } = renderHook(() =>
23 useLongRunningTransitionState({ storageKey, thresholdMs: 60_000 })
24 )
25
26 expect(result.current).toBe(true)
27 })
28
29 it('keeps a stable in-memory timer when no storage key is available', () => {
30 const { result, rerender } = renderHook(
31 ({ thresholdMs }: { thresholdMs: number }) =>
32 useLongRunningTransitionState({ storageKey: null, thresholdMs }),
33 {
34 initialProps: { thresholdMs: 120_000 },
35 }
36 )
37
38 expect(result.current).toBe(false)
39
40 act(() => {
41 vi.advanceTimersByTime(30_000)
42 })
43
44 rerender({ thresholdMs: 60_000 })
45 expect(result.current).toBe(false)
46
47 act(() => {
48 vi.advanceTimersByTime(30_000)
49 })
50
51 expect(result.current).toBe(true)
52 })
53})