useLongRunningTransitionState.test.ts 1.5 KB

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