mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {bundleAsync} from '../../../src/lib/async/bundle'
2
3describe('bundle', () => {
4 it('bundles multiple simultaneous calls into one execution', async () => {
5 let calls = 0
6 const fn = bundleAsync(async () => {
7 calls++
8 await new Promise(r => setTimeout(r, 1))
9 return 'hello'
10 })
11 const [res1, res2, res3] = await Promise.all([fn(), fn(), fn()])
12 expect(calls).toEqual(1)
13 expect(res1).toEqual('hello')
14 expect(res2).toEqual('hello')
15 expect(res3).toEqual('hello')
16 })
17 it('does not bundle non-simultaneous calls', async () => {
18 let calls = 0
19 const fn = bundleAsync(async () => {
20 calls++
21 await new Promise(r => setTimeout(r, 1))
22 return 'hello'
23 })
24 const res1 = await fn()
25 const res2 = await fn()
26 const res3 = await fn()
27 expect(calls).toEqual(3)
28 expect(res1).toEqual('hello')
29 expect(res2).toEqual('hello')
30 expect(res3).toEqual('hello')
31 })
32 it('is not affected by rejections', async () => {
33 let calls = 0
34 const fn = bundleAsync(async () => {
35 calls++
36 await new Promise(r => setTimeout(r, 1))
37 throw new Error()
38 })
39 const res1 = await fn().catch(() => 'reject')
40 const res2 = await fn().catch(() => 'reject')
41 const res3 = await fn().catch(() => 'reject')
42 expect(calls).toEqual(3)
43 expect(res1).toEqual('reject')
44 expect(res2).toEqual('reject')
45 expect(res3).toEqual('reject')
46 })
47})