|
| 1 | +import { Batcher } from './batcher' |
| 2 | + |
| 3 | +describe('Batcher', () => { |
| 4 | + describe('batch', () => { |
| 5 | + it('should execute the work function immediately', async () => { |
| 6 | + const batcher = Batcher.create<string, number>() |
| 7 | + const workFn = jest.fn().mockResolvedValue(42) |
| 8 | + |
| 9 | + const result = await batcher.batch('key', workFn) |
| 10 | + |
| 11 | + expect(result).toBe(42) |
| 12 | + expect(workFn).toHaveBeenCalledTimes(1) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should batch multiple calls to the same key', async () => { |
| 16 | + const batcher = Batcher.create<string, number>() |
| 17 | + const workFn = jest.fn().mockResolvedValue(42) |
| 18 | + |
| 19 | + const result1 = batcher.batch('key', workFn) |
| 20 | + const result2 = batcher.batch('key', workFn) |
| 21 | + |
| 22 | + expect(result1).toBeInstanceOf(Promise) |
| 23 | + expect(result2).toBeInstanceOf(Promise) |
| 24 | + expect(workFn).toHaveBeenCalledTimes(1) |
| 25 | + |
| 26 | + const [value1, value2] = await Promise.all([result1, result2]) |
| 27 | + |
| 28 | + expect(value1).toBe(42) |
| 29 | + expect(value2).toBe(42) |
| 30 | + expect(workFn).toHaveBeenCalledTimes(1) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should not batch calls to different keys', async () => { |
| 34 | + const batcher = Batcher.create<string, string>() |
| 35 | + const workFn = jest.fn((key) => key) |
| 36 | + |
| 37 | + const result1 = batcher.batch('key1', workFn) |
| 38 | + const result2 = batcher.batch('key2', workFn) |
| 39 | + |
| 40 | + expect(result1).toBeInstanceOf(Promise) |
| 41 | + expect(result2).toBeInstanceOf(Promise) |
| 42 | + expect(workFn).toHaveBeenCalledTimes(2) |
| 43 | + |
| 44 | + const [value1, value2] = await Promise.all([result1, result2]) |
| 45 | + |
| 46 | + expect(value1).toBe('key1') |
| 47 | + expect(value2).toBe('key2') |
| 48 | + expect(workFn).toHaveBeenCalledTimes(2) |
| 49 | + }) |
| 50 | + |
| 51 | + it('should use the cacheKeyFn to generate cache keys', async () => { |
| 52 | + const cacheKeyFn = jest.fn().mockResolvedValue('cache-key') |
| 53 | + const batcher = Batcher.create<string, number>({ cacheKeyFn }) |
| 54 | + const workFn = jest.fn().mockResolvedValue(42) |
| 55 | + |
| 56 | + const result = await batcher.batch('key', workFn) |
| 57 | + |
| 58 | + expect(result).toBe(42) |
| 59 | + expect(cacheKeyFn).toHaveBeenCalledWith('key') |
| 60 | + expect(workFn).toHaveBeenCalledTimes(1) |
| 61 | + }) |
| 62 | + |
| 63 | + it('should use the schedulerFn to schedule work', async () => { |
| 64 | + const schedulerFn = jest.fn().mockImplementation((fn) => fn()) |
| 65 | + const batcher = Batcher.create<string, number>({ schedulerFn }) |
| 66 | + const workFn = jest.fn().mockResolvedValue(42) |
| 67 | + |
| 68 | + const results = await Promise.all([ |
| 69 | + batcher.batch('key', workFn), |
| 70 | + batcher.batch('key', workFn), |
| 71 | + batcher.batch('key', workFn), |
| 72 | + ]) |
| 73 | + |
| 74 | + expect(results).toEqual([42, 42, 42]) |
| 75 | + expect(workFn).toHaveBeenCalledTimes(1) |
| 76 | + }) |
| 77 | + }) |
| 78 | +}) |
0 commit comments