-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test-utils.ts
77 lines (57 loc) · 1.61 KB
/
test-utils.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
export function getRandomTime() {
return Math.random() * 100;
}
export function getTimer() {
let startTime: Date;
function start() {
startTime = new Date();
}
function stop() {
const endTime = new Date();
return endTime.getTime() - startTime.getTime();
}
return { start, stop };
}
export function makeDelayed<T extends unknown[], R>(
fn: (...args: T) => R,
delay = getRandomTime(),
): (...args: T) => Promise<R> {
return function delayedFunction(...args) {
return new Promise((resolve) =>
setTimeout(() => {
resolve(fn(...args));
}, delay),
);
};
}
export function duplicate(x: number) {
return x * 2;
}
export const duplicateInRandomTime = makeDelayed(duplicate);
export function largerThanTwo(x: number) {
return x > 2;
}
export const largerThanTwoInRandomTime = makeDelayed(largerThanTwo);
export function largerThanOneHundred(x: number) {
return x > 100;
}
export const largerThanOneHundredInRandomTime = makeDelayed(largerThanOneHundred);
export function makePushDuplicate(): [number[], (x: number) => void] {
const arr: number[] = [];
function pushDuplicate(x: number) {
arr.push(x * 2);
}
return [arr, pushDuplicate];
}
export function makePushDuplicateInRandomTime(): [number[], (x: number) => Promise<void>] {
const arr: number[] = [];
async function pushDuplicate(x: number) {
arr.push(await duplicateInRandomTime(x));
}
return [arr, pushDuplicate];
}
export function throws() {
throw new Error('Some error');
}
export const inputArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
export const doubleInputArr = inputArr.map(duplicate);