Skip to content

Commit 87ed79c

Browse files
committed
Add unit tests ensuring utils take exactly the time necessary to execute
1 parent 006dbac commit 87ed79c

12 files changed

+208
-2
lines changed

src/every.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncEvery from './every';
22

33
import {
4+
getTimer,
45
inputArr,
56
largerThanOneHundred,
67
largerThanOneHundredInRandomTime,
8+
makeDelayed,
79
} from '../test-utils';
810

911
function largerOrEqualThanZero(x) {
@@ -25,6 +27,21 @@ describe('asyncEvery()', () => {
2527
expect(largerThanZero).toBe(true);
2628
});
2729

30+
it('takes exactly the time necessary to execute', async () => {
31+
const delay = 100;
32+
33+
const timer = getTimer();
34+
35+
timer.start();
36+
37+
await asyncEvery([1, 2, 3], makeDelayed((el) => el > 0, delay));
38+
39+
const timeElapsed = timer.stop();
40+
41+
expect(timeElapsed).toBeGreaterThanOrEqual(delay);
42+
expect(timeElapsed).toBeLessThan(delay + 10);
43+
});
44+
2845
it.skip('assertions below are valid for synchronous .every()', () => {
2946
const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
3047

src/every_strict.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import asyncEveryStrict from './every_strict';
22

33
import {
44
doubleInputArr,
5+
getTimer,
56
inputArr,
67
largerThanOneHundred,
78
largerThanOneHundredInRandomTime,
9+
makeDelayed,
810
makePushDuplicate,
911
makePushDuplicateInRandomTime,
1012
} from '../test-utils';
@@ -37,6 +39,21 @@ describe('asyncEvery()', () => {
3739
expect(indexes).toEqual([0, 1, 2]);
3840
});
3941

42+
it('takes exactly the time necessary to execute', async () => {
43+
const delay = 100;
44+
45+
const timer = getTimer();
46+
47+
timer.start();
48+
49+
await asyncEveryStrict([1, 2, 3], makeDelayed((el) => el > 0, delay));
50+
51+
const timeElapsed = timer.stop();
52+
53+
expect(timeElapsed).toBeGreaterThan(delay * 3);
54+
expect(timeElapsed).toBeLessThan((delay + 10) * 3);
55+
});
56+
4057
it.skip('assertions below are valid for synchronous .every()', () => {
4158
const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
4259

src/filter.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import asyncFilter from './filter';
22

33
import {
4+
getTimer,
45
inputArr,
56
largerThanTwo,
67
largerThanTwoInRandomTime,
8+
makeDelayed,
79
} from '../test-utils';
810

911
describe('asyncFilter()', () => {
@@ -13,6 +15,21 @@ describe('asyncFilter()', () => {
1315
expect(asyncFilteredArr).toEqual([2, 3]);
1416
});
1517

18+
it('takes exactly the time necessary to execute', async () => {
19+
const delay = 100;
20+
21+
const timer = getTimer();
22+
23+
timer.start();
24+
25+
await asyncFilter([1, 2, 3], makeDelayed((el) => el > 1, delay));
26+
27+
const timeElapsed = timer.stop();
28+
29+
expect(timeElapsed).toBeGreaterThanOrEqual(delay);
30+
expect(timeElapsed).toBeLessThan(delay + 10);
31+
});
32+
1633
it.skip('assertions below are valid for synchronous .filter()', () => {
1734
const filter = jest.fn().mockImplementation(largerThanTwo);
1835

src/filter_strict.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import asyncFilterStrict from './filter_strict';
22

33
import {
44
doubleInputArr,
5+
getTimer,
56
inputArr,
67
largerThanTwo,
78
largerThanTwoInRandomTime,
9+
makeDelayed,
810
makePushDuplicate,
911
makePushDuplicateInRandomTime,
1012
} from '../test-utils';
@@ -25,6 +27,21 @@ describe('asyncFilterStrict()', () => {
2527
expect(indexes).toEqual([0, 1, 2]);
2628
});
2729

30+
it('takes exactly the time necessary to execute', async () => {
31+
const delay = 100;
32+
33+
const timer = getTimer();
34+
35+
timer.start();
36+
37+
await asyncFilterStrict([1, 2, 3], makeDelayed((el) => el > 1, delay));
38+
39+
const timeElapsed = timer.stop();
40+
41+
expect(timeElapsed).toBeGreaterThan(delay * 3);
42+
expect(timeElapsed).toBeLessThan((delay + 10) * 3);
43+
});
44+
2845
it.skip('assertions below are valid for synchronous .filter()', () => {
2946
const filter = jest.fn().mockImplementation(largerThanTwo);
3047

src/forEach.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncForEach from './forEach';
22

33
import {
4+
getTimer,
45
inputArr,
6+
makeDelayed,
57
makePushDuplicate,
68
makePushDuplicateInRandomTime,
79
} from '../test-utils';
@@ -21,6 +23,21 @@ describe('asyncForEach()', () => {
2123
expect(consoleLog).toHaveBeenCalledWith(6);
2224
});
2325

26+
it('takes exactly the time necessary to execute', async () => {
27+
const delay = 100;
28+
29+
const timer = getTimer();
30+
31+
timer.start();
32+
33+
await asyncForEach([1, 2, 3], makeDelayed((el) => el * 2, delay));
34+
35+
const timeElapsed = timer.stop();
36+
37+
expect(timeElapsed).toBeGreaterThanOrEqual(delay);
38+
expect(timeElapsed).toBeLessThan(delay + 10);
39+
});
40+
2441
it.skip('assertions below are valid for synchronous .forEach()', () => {
2542
const [arr, pushDuplicate] = makePushDuplicate();
2643
const mapper = jest.fn().mockImplementation(pushDuplicate);

src/forEach_strict.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import asyncForEachStrict from './forEach_strict';
22
import {
33
doubleInputArr,
4+
getTimer,
45
inputArr,
6+
makeDelayed,
57
makePushDuplicate,
68
makePushDuplicateInRandomTime,
79
} from '../test-utils';
@@ -27,6 +29,21 @@ describe('asyncForEachStrict()', () => {
2729
expect(indexes).toEqual([0, 1, 2]);
2830
});
2931

32+
it('takes exactly the time necessary to execute', async () => {
33+
const delay = 100;
34+
35+
const timer = getTimer();
36+
37+
timer.start();
38+
39+
await asyncForEachStrict([1, 2, 3], makeDelayed((el) => el * 2, delay));
40+
41+
const timeElapsed = timer.stop();
42+
43+
expect(timeElapsed).toBeGreaterThan(delay * 3);
44+
expect(timeElapsed).toBeLessThan((delay + 10) * 3);
45+
});
46+
3047
it.skip('assertions below are valid for synchronous .forEach()', () => {
3148
const [arr, pushDuplicate] = makePushDuplicate();
3249
const mapper = jest.fn().mockImplementation(pushDuplicate);

src/map.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
doubleInputArr,
55
duplicate,
66
duplicateInRandomTime,
7+
getTimer,
78
inputArr,
9+
makeDelayed,
810
} from '../test-utils';
911

1012
describe('asyncMap()', () => {
@@ -14,6 +16,21 @@ describe('asyncMap()', () => {
1416
expect(asyncMappedArr).toEqual([2, 4, 6]);
1517
});
1618

19+
it('takes exactly the time necessary to execute', async () => {
20+
const delay = 100;
21+
22+
const timer = getTimer();
23+
24+
timer.start();
25+
26+
await asyncMap([1, 2, 3], makeDelayed((el) => el * 2, delay));
27+
28+
const timeElapsed = timer.stop();
29+
30+
expect(timeElapsed).toBeGreaterThanOrEqual(delay);
31+
expect(timeElapsed).toBeLessThan(delay + 10);
32+
});
33+
1734
it.skip('assertions below are valid for synchronous .map()', () => {
1835
const mapper = jest.fn().mockImplementation(duplicate);
1936

src/map_strict.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
doubleInputArr,
55
duplicate,
66
duplicateInRandomTime,
7+
getTimer,
78
inputArr,
9+
makeDelayed,
810
makePushDuplicate,
911
makePushDuplicateInRandomTime,
1012
} from '../test-utils';
@@ -25,6 +27,21 @@ describe('asyncMapStrict()', () => {
2527
expect(indexes).toEqual([0, 1, 2]);
2628
});
2729

30+
it('takes exactly the time necessary to execute', async () => {
31+
const delay = 100;
32+
33+
const timer = getTimer();
34+
35+
timer.start();
36+
37+
await asyncMapStrict([1, 2, 3], makeDelayed((el) => el * 2, delay));
38+
39+
const timeElapsed = timer.stop();
40+
41+
expect(timeElapsed).toBeGreaterThan(delay * 3);
42+
expect(timeElapsed).toBeLessThan((delay + 10) * 3);
43+
});
44+
2845
it.skip('assertions below are valid for synchronous .map()', () => {
2946
const mapper = jest.fn().mockImplementation(duplicate);
3047

src/reduce.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getTimer, makeDelayed } from '../test-utils';
12
import asyncReduce from './reduce';
23

34
describe('asyncReduce()', () => {
@@ -11,6 +12,25 @@ describe('asyncReduce()', () => {
1112
expect(result).toBe(6);
1213
});
1314

15+
it('takes exactly the time necessary to execute', async () => {
16+
const delay = 100;
17+
18+
const timer = getTimer();
19+
20+
timer.start();
21+
22+
await asyncReduce(
23+
[1, 2, 3],
24+
makeDelayed((tmp, cur) => tmp + cur, delay),
25+
0,
26+
);
27+
28+
const timeElapsed = timer.stop();
29+
30+
expect(timeElapsed).toBeGreaterThan(delay * 3);
31+
expect(timeElapsed).toBeLessThan((delay + 10) * 3);
32+
});
33+
1434
it.skip('assertions below are valid for synchronous .reduce()', () => {
1535
const mapper = jest.fn().mockImplementation((temp, cur, idx) => [...temp, `${idx}:${cur}`]);
1636

src/some.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
largerThanTwoInRandomTime,
77
largerThanOneHundred,
88
largerThanOneHundredInRandomTime,
9+
getTimer,
10+
makeDelayed,
911
} from '../test-utils';
1012

1113
const firstElementLargerThanTwo = inputArr.findIndex(largerThanTwo);
@@ -17,6 +19,21 @@ describe('asyncSome()', () => {
1719
expect(largerThanZero).toBe(true);
1820
});
1921

22+
it('takes exactly the time necessary to execute', async () => {
23+
const delay = 100;
24+
25+
const timer = getTimer();
26+
27+
timer.start();
28+
29+
await asyncSome([1, 2, 3], makeDelayed((el) => el < 0, delay));
30+
31+
const timeElapsed = timer.stop();
32+
33+
expect(timeElapsed).toBeGreaterThanOrEqual(delay);
34+
expect(timeElapsed).toBeLessThan(delay + 10);
35+
});
36+
2037
it.skip('assertions below are valid for synchronous .some()', () => {
2138
const mapper = jest.fn().mockImplementation(largerThanTwo);
2239

0 commit comments

Comments
 (0)