|
| 1 | +import { flushSync } from 'svelte' |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +import { useSlowRequest } from '../use-slow-request.svelte.ts' |
| 5 | + |
| 6 | +describe('useSlowRequest', () => { |
| 7 | + beforeEach(() => { |
| 8 | + vi.useFakeTimers() |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + vi.useRealTimers() |
| 13 | + }) |
| 14 | + |
| 15 | + it('is not slow initially when not fetching', () => { |
| 16 | + const cleanup = $effect.root(() => { |
| 17 | + const result = useSlowRequest(() => false) |
| 18 | + flushSync() |
| 19 | + expect(result.isSlow).toBe(false) |
| 20 | + }) |
| 21 | + cleanup() |
| 22 | + }) |
| 23 | + |
| 24 | + it('is not slow before the 5s threshold elapses', () => { |
| 25 | + let isFetching = $state(false) |
| 26 | + const cleanup = $effect.root(() => { |
| 27 | + const result = useSlowRequest(() => isFetching) |
| 28 | + flushSync() |
| 29 | + |
| 30 | + isFetching = true |
| 31 | + flushSync() |
| 32 | + vi.advanceTimersByTime(4999) |
| 33 | + expect(result.isSlow).toBe(false) |
| 34 | + }) |
| 35 | + cleanup() |
| 36 | + }) |
| 37 | + |
| 38 | + it('becomes slow after the 5s threshold elapses while fetching', () => { |
| 39 | + let isFetching = $state(false) |
| 40 | + const cleanup = $effect.root(() => { |
| 41 | + const result = useSlowRequest(() => isFetching) |
| 42 | + flushSync() |
| 43 | + |
| 44 | + isFetching = true |
| 45 | + flushSync() |
| 46 | + vi.advanceTimersByTime(5000) |
| 47 | + expect(result.isSlow).toBe(true) |
| 48 | + }) |
| 49 | + cleanup() |
| 50 | + }) |
| 51 | + |
| 52 | + it('resets to not slow when fetching completes', () => { |
| 53 | + let isFetching = $state(false) |
| 54 | + const cleanup = $effect.root(() => { |
| 55 | + const result = useSlowRequest(() => isFetching) |
| 56 | + flushSync() |
| 57 | + |
| 58 | + isFetching = true |
| 59 | + flushSync() |
| 60 | + vi.advanceTimersByTime(5000) |
| 61 | + expect(result.isSlow).toBe(true) |
| 62 | + |
| 63 | + isFetching = false |
| 64 | + flushSync() |
| 65 | + expect(result.isSlow).toBe(false) |
| 66 | + }) |
| 67 | + cleanup() |
| 68 | + }) |
| 69 | + |
| 70 | + it('does not become slow if fetching completes before the threshold', () => { |
| 71 | + let isFetching = $state(false) |
| 72 | + const cleanup = $effect.root(() => { |
| 73 | + const result = useSlowRequest(() => isFetching) |
| 74 | + flushSync() |
| 75 | + |
| 76 | + isFetching = true |
| 77 | + flushSync() |
| 78 | + vi.advanceTimersByTime(4000) |
| 79 | + isFetching = false |
| 80 | + flushSync() |
| 81 | + vi.advanceTimersByTime(5000) |
| 82 | + expect(result.isSlow).toBe(false) |
| 83 | + }) |
| 84 | + cleanup() |
| 85 | + }) |
| 86 | + |
| 87 | + it('restarts the timer when a new fetch begins', () => { |
| 88 | + let isFetching = $state(false) |
| 89 | + const cleanup = $effect.root(() => { |
| 90 | + const result = useSlowRequest(() => isFetching) |
| 91 | + flushSync() |
| 92 | + |
| 93 | + isFetching = true |
| 94 | + flushSync() |
| 95 | + vi.advanceTimersByTime(5000) |
| 96 | + expect(result.isSlow).toBe(true) |
| 97 | + |
| 98 | + isFetching = false |
| 99 | + flushSync() |
| 100 | + expect(result.isSlow).toBe(false) |
| 101 | + |
| 102 | + isFetching = true |
| 103 | + flushSync() |
| 104 | + vi.advanceTimersByTime(4999) |
| 105 | + expect(result.isSlow).toBe(false) |
| 106 | + vi.advanceTimersByTime(1) |
| 107 | + expect(result.isSlow).toBe(true) |
| 108 | + }) |
| 109 | + cleanup() |
| 110 | + }) |
| 111 | +}) |
0 commit comments