|
1 |
| -import { describe, expect, it } from 'vitest' |
2 |
| -import { ThrottledQueue } from './throttle' |
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import type { UpdateUrlFunction } from '../../adapters/lib/defs' |
| 3 | +import { defaultRateLimit } from './rate-limiting' |
| 4 | +import { ThrottledQueue, type UpdateQueueAdapterContext } from './throttle' |
3 | 5 |
|
4 | 6 | describe('throttle: ThrottleQueue value queueing', () => {
|
5 | 7 | it('should enqueue key & values', () => {
|
@@ -62,4 +64,52 @@ describe('throttle: ThrottleQueue option combination logic', () => {
|
62 | 64 | queue.push({ key: 'c', query: null, options: { shallow: true } })
|
63 | 65 | expect(queue.options.shallow).toEqual(false)
|
64 | 66 | })
|
| 67 | + it('should compose transitions', async () => { |
| 68 | + const mockStartTransition = (callback: () => void) => { |
| 69 | + callback() |
| 70 | + } |
| 71 | + const startTransitionA = vi.fn().mockImplementation(mockStartTransition) |
| 72 | + const startTransitionB = vi.fn().mockImplementation(mockStartTransition) |
| 73 | + const mockAdapter: UpdateQueueAdapterContext = { |
| 74 | + updateUrl: vi.fn<UpdateUrlFunction>(), |
| 75 | + getSearchParamsSnapshot() { |
| 76 | + return new URLSearchParams() |
| 77 | + } |
| 78 | + } |
| 79 | + const queue = new ThrottledQueue() |
| 80 | + queue.push({ |
| 81 | + key: 'a', |
| 82 | + query: null, |
| 83 | + options: { startTransition: startTransitionA } |
| 84 | + }) |
| 85 | + queue.push({ |
| 86 | + key: 'b', |
| 87 | + query: null, |
| 88 | + options: { startTransition: startTransitionB } |
| 89 | + }) |
| 90 | + await queue.flush(mockAdapter) |
| 91 | + expect(startTransitionA).toHaveBeenCalledOnce() |
| 92 | + expect(startTransitionB).toHaveBeenCalledOnce() |
| 93 | + expect(startTransitionA).toHaveBeenCalledBefore(startTransitionB) |
| 94 | + }) |
| 95 | + it('keeps the maximum value for timeMs', () => { |
| 96 | + const queue = new ThrottledQueue() |
| 97 | + queue.push({ key: 'a', query: null, options: {}, timeMs: 100 }) |
| 98 | + queue.push({ key: 'b', query: null, options: {}, timeMs: 200 }) |
| 99 | + queue.push({ key: 'c', query: null, options: {}, timeMs: 300 }) |
| 100 | + expect(queue.timeMs).toEqual(300) |
| 101 | + }) |
| 102 | + it('clamps the minimum value for timeMs to the default rate limit', () => { |
| 103 | + expect(defaultRateLimit.timeMs).toBeGreaterThan(10) // precondition |
| 104 | + const queue = new ThrottledQueue() |
| 105 | + queue.push({ key: 'a', query: null, options: {}, timeMs: 10 }) |
| 106 | + expect(queue.timeMs).toEqual(defaultRateLimit.timeMs) |
| 107 | + }) |
| 108 | + it('supports passing Infinity to the timeMs option (but can be cleared)', () => { |
| 109 | + const queue = new ThrottledQueue() |
| 110 | + queue.push({ key: 'a', query: null, options: {}, timeMs: Infinity }) |
| 111 | + expect(queue.timeMs).toBe(Infinity) |
| 112 | + queue.push({ key: 'b', query: null, options: {}, timeMs: 100 }) |
| 113 | + expect(queue.timeMs).toBe(100) |
| 114 | + }) |
65 | 115 | })
|
0 commit comments