|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { ThrottledQueue } from './throttle' |
| 3 | + |
| 4 | +describe('throttle: ThrottleQueue value queueing', () => { |
| 5 | + it('should enqueue key & values', () => { |
| 6 | + const queue = new ThrottledQueue() |
| 7 | + queue.push({ key: 'key', query: 'value', options: {} }) |
| 8 | + expect(queue.getQueuedQuery('key')).toEqual('value') |
| 9 | + }) |
| 10 | + it('should replace more recent values with the same key', () => { |
| 11 | + const queue = new ThrottledQueue() |
| 12 | + queue.push({ key: 'key', query: 'a', options: {} }) |
| 13 | + queue.push({ key: 'key', query: 'b', options: {} }) |
| 14 | + expect(queue.getQueuedQuery('key')).toEqual('b') |
| 15 | + }) |
| 16 | + it('should enqueue multiple keys', () => { |
| 17 | + const queue = new ThrottledQueue() |
| 18 | + queue.push({ key: 'key1', query: 'a', options: {} }) |
| 19 | + queue.push({ key: 'key2', query: 'b', options: {} }) |
| 20 | + expect(queue.getQueuedQuery('key1')).toEqual('a') |
| 21 | + expect(queue.getQueuedQuery('key2')).toEqual('b') |
| 22 | + }) |
| 23 | + it('should enqueue null values (to clear a key from the URL)', () => { |
| 24 | + const queue = new ThrottledQueue() |
| 25 | + queue.push({ key: 'key', query: 'a', options: {} }) |
| 26 | + queue.push({ key: 'key', query: null, options: {} }) |
| 27 | + expect(queue.getQueuedQuery('key')).toBeNull() |
| 28 | + }) |
| 29 | + it('should return an undefined queued value if no push occurred', () => { |
| 30 | + const queue = new ThrottledQueue() |
| 31 | + expect(queue.getQueuedQuery('key')).toBeUndefined() |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +describe('throttle: ThrottleQueue option combination logic', () => { |
| 36 | + it('should resolve with the default options', () => { |
| 37 | + const queue = new ThrottledQueue() |
| 38 | + expect(queue.options).toEqual({ |
| 39 | + history: 'replace', |
| 40 | + scroll: false, |
| 41 | + shallow: true |
| 42 | + }) |
| 43 | + }) |
| 44 | + it('should combine history options (push takes precedence)', () => { |
| 45 | + const queue = new ThrottledQueue() |
| 46 | + queue.push({ key: 'a', query: null, options: { history: 'replace' } }) |
| 47 | + queue.push({ key: 'b', query: null, options: { history: 'push' } }) |
| 48 | + queue.push({ key: 'c', query: null, options: { history: 'replace' } }) |
| 49 | + expect(queue.options.history).toEqual('push') |
| 50 | + }) |
| 51 | + it('should combine scroll options (true takes precedence)', () => { |
| 52 | + const queue = new ThrottledQueue() |
| 53 | + queue.push({ key: 'a', query: null, options: { scroll: false } }) |
| 54 | + queue.push({ key: 'b', query: null, options: { scroll: true } }) |
| 55 | + queue.push({ key: 'c', query: null, options: { scroll: false } }) |
| 56 | + expect(queue.options.scroll).toEqual(true) |
| 57 | + }) |
| 58 | + it('should combine shallow options (false takes precedence)', () => { |
| 59 | + const queue = new ThrottledQueue() |
| 60 | + queue.push({ key: 'a', query: null, options: { shallow: true } }) |
| 61 | + queue.push({ key: 'b', query: null, options: { shallow: false } }) |
| 62 | + queue.push({ key: 'c', query: null, options: { shallow: true } }) |
| 63 | + expect(queue.options.shallow).toEqual(false) |
| 64 | + }) |
| 65 | +}) |
0 commit comments