-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
jsUtils.test.ts
82 lines (68 loc) · 2.39 KB
/
jsUtils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {jest} from '@jest/globals';
import _ from 'lodash';
import {mapAsyncSequential, findAsyncSequential} from '../jsUtils';
describe('mapAsyncSequential', () => {
function sleep(timeout: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
it('maps sequentially', async () => {
const itemToTimeout: {[key: string]: number} = {
'1': 200,
'2': 600,
'3': 400,
};
const items = Object.keys(itemToTimeout);
const itemMapStartsAt: {[key: string]: number} = {};
const itemMapEndsAt: {[key: string]: number} = {};
const timeBefore = Date.now();
await expect(
mapAsyncSequential(items, async (item) => {
const itemTimeout = itemToTimeout[item]!;
itemMapStartsAt[item] = Date.now();
await sleep(itemTimeout);
itemMapEndsAt[item] = Date.now();
return `${item} mapped`;
}),
).resolves.toEqual(['1 mapped', '2 mapped', '3 mapped']);
const timeAfter = Date.now();
const timeTotal = timeAfter - timeBefore;
const totalTimeouts = _.sum(Object.values(itemToTimeout));
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 100);
expect(itemMapStartsAt[1]).toBeGreaterThanOrEqual(0);
expect(itemMapStartsAt[2]).toBeGreaterThanOrEqual(itemMapEndsAt[1]! - 100);
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(
itemMapEndsAt[2]! - 100,
);
});
});
describe('findAsyncSequential', () => {
function sleep(timeout: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, timeout);
});
}
it('finds sequentially', async () => {
const items = ['1', '2', '3'];
const findFn = jest.fn(async (item: string) => {
await sleep(400);
return item === '2';
});
const timeBefore = Date.now();
await expect(findAsyncSequential(items, findFn)).resolves.toBe('2');
const timeAfter = Date.now();
expect(findFn).toHaveBeenCalledTimes(2);
expect(findFn).toHaveBeenNthCalledWith(1, '1');
expect(findFn).toHaveBeenNthCalledWith(2, '2');
const timeTotal = timeAfter - timeBefore;
expect(timeTotal).toBeGreaterThanOrEqual(600);
expect(timeTotal).toBeLessThan(1000);
});
});