Skip to content

Commit b785888

Browse files
committed
Add unit tests for examples in README, fix README examples
1 parent 26b3cbe commit b785888

12 files changed

+131
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Like `asyncFilter()`, but runs iterations non-concurrently.
8585
import { asyncFilterStrict } from '@wojtekmaj/async-array-utils';
8686

8787
const indexes = [];
88-
await asyncFilterStrict(
88+
const asyncFilteredArr = await asyncFilterStrict(
8989
[1, 2, 3],
9090
async (el, index) => {
9191
indexes.push(index);
@@ -156,7 +156,7 @@ Like `asyncMap()`, but runs iterations non-concurrently.
156156
import { asyncMapStrict } from '@wojtekmaj/async-array-utils';
157157

158158
const indexes = [];
159-
await asyncMapStrict(
159+
const asyncMappedArr = await asyncMapStrict(
160160
[1, 2, 3],
161161
async (el, index) => {
162162
indexes.push(index);
@@ -213,7 +213,7 @@ const largerThanZero = await asyncSomeStrict(
213213
return el > 0;
214214
},
215215
); // true
216-
console.log(indexes); // [0, 1, 2]
216+
console.log(indexes); // [0]
217217
```
218218

219219
## License

src/every.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ function largerOrEqualThanZeroInRandomTime(x) {
1919
}
2020

2121
describe('asyncEvery()', () => {
22+
it('example from README works as described', async () => {
23+
const largerThanZero = await asyncEvery([1, 2, 3], async (el) => el > 0);
24+
25+
expect(largerThanZero).toBe(true);
26+
});
27+
2228
it.skip('assertions below are valid for synchronous .every()', () => {
2329
const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
2430

src/every_strict.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ function largerOrEqualThanZeroInRandomTime(x) {
2222
}
2323

2424
describe('asyncEvery()', () => {
25+
it('example from README works as described', async () => {
26+
const indexes = [];
27+
28+
const largerThanZero = await asyncEveryStrict(
29+
[1, 2, 3],
30+
async (el, index) => {
31+
indexes.push(index);
32+
return el > 0;
33+
},
34+
);
35+
36+
expect(largerThanZero).toBe(true);
37+
expect(indexes).toEqual([0, 1, 2]);
38+
});
39+
2540
it.skip('assertions below are valid for synchronous .every()', () => {
2641
const mapper = jest.fn().mockImplementation(largerOrEqualThanZero);
2742

src/filter.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import {
77
} from '../test-utils';
88

99
describe('asyncFilter()', () => {
10+
it('example from README works as described', async () => {
11+
const asyncFilteredArr = await asyncFilter([1, 2, 3], async (el) => el > 1);
12+
13+
expect(asyncFilteredArr).toEqual([2, 3]);
14+
});
15+
1016
it.skip('assertions below are valid for synchronous .filter()', () => {
1117
const filter = jest.fn().mockImplementation(largerThanTwo);
1218

src/filter_strict.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import {
1010
} from '../test-utils';
1111

1212
describe('asyncFilterStrict()', () => {
13+
it('example from README works as described', async () => {
14+
const indexes = [];
15+
16+
const asyncFilteredArr = await asyncFilterStrict(
17+
[1, 2, 3],
18+
async (el, index) => {
19+
indexes.push(index);
20+
return el > 1;
21+
},
22+
);
23+
24+
expect(asyncFilteredArr).toEqual([2, 3]);
25+
expect(indexes).toEqual([0, 1, 2]);
26+
});
27+
1328
it.skip('assertions below are valid for synchronous .filter()', () => {
1429
const filter = jest.fn().mockImplementation(largerThanTwo);
1530

src/forEach.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import {
77
} from '../test-utils';
88

99
describe('asyncForEach()', () => {
10+
it('example from README works as described', async () => {
11+
const consoleLog = jest.fn();
12+
13+
await asyncForEach(
14+
[1, 2, 3],
15+
async (el) => { consoleLog(el * 2); },
16+
); // undefined; 3 console.logs
17+
18+
expect(consoleLog).toHaveBeenCalledTimes(3);
19+
expect(consoleLog).toHaveBeenCalledWith(2);
20+
expect(consoleLog).toHaveBeenCalledWith(4);
21+
expect(consoleLog).toHaveBeenCalledWith(6);
22+
});
23+
1024
it.skip('assertions below are valid for synchronous .forEach()', () => {
1125
const [arr, pushDuplicate] = makePushDuplicate();
1226
const mapper = jest.fn().mockImplementation(pushDuplicate);

src/forEach_strict.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import {
77
} from '../test-utils';
88

99
describe('asyncForEachStrict()', () => {
10+
it('example from README works as described', async () => {
11+
const consoleLog = jest.fn();
12+
const indexes = [];
13+
14+
await asyncForEachStrict(
15+
[1, 2, 3],
16+
async (el, index) => {
17+
indexes.push(index);
18+
consoleLog(el * 2);
19+
},
20+
);
21+
22+
expect(consoleLog).toHaveBeenCalledTimes(3);
23+
expect(consoleLog).toHaveBeenCalledWith(2);
24+
expect(consoleLog).toHaveBeenCalledWith(4);
25+
expect(consoleLog).toHaveBeenCalledWith(6);
26+
27+
expect(indexes).toEqual([0, 1, 2]);
28+
});
29+
1030
it.skip('assertions below are valid for synchronous .forEach()', () => {
1131
const [arr, pushDuplicate] = makePushDuplicate();
1232
const mapper = jest.fn().mockImplementation(pushDuplicate);

src/map.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
} from '../test-utils';
99

1010
describe('asyncMap()', () => {
11+
it('example from README works as described', async () => {
12+
const asyncMappedArr = await asyncMap([1, 2, 3], async (el) => el * 2); // [2, 4, 6]
13+
14+
expect(asyncMappedArr).toEqual([2, 4, 6]);
15+
});
16+
1117
it.skip('assertions below are valid for synchronous .map()', () => {
1218
const mapper = jest.fn().mockImplementation(duplicate);
1319

src/map_strict.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ import {
1010
} from '../test-utils';
1111

1212
describe('asyncMapStrict()', () => {
13+
it('example from README works as described', async () => {
14+
const indexes = [];
15+
16+
const asyncMappedArr = await asyncMapStrict(
17+
[1, 2, 3],
18+
async (el, index) => {
19+
indexes.push(index);
20+
return el * 2;
21+
},
22+
); // [2, 4, 6]
23+
24+
expect(asyncMappedArr).toEqual([2, 4, 6]);
25+
expect(indexes).toEqual([0, 1, 2]);
26+
});
27+
1328
it.skip('assertions below are valid for synchronous .map()', () => {
1429
const mapper = jest.fn().mockImplementation(duplicate);
1530

src/reduce.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import asyncReduce from './reduce';
22

33
describe('asyncReduce()', () => {
4+
it('example from README works as described', async () => {
5+
const result = await asyncReduce(
6+
[1, 2, 3],
7+
async (tmp, cur) => tmp + cur,
8+
0,
9+
);
10+
11+
expect(result).toBe(6);
12+
});
13+
414
it.skip('assertions below are valid for synchronous .reduce()', () => {
515
const mapper = jest.fn().mockImplementation((temp, cur, idx) => [...temp, `${idx}:${cur}`]);
616

0 commit comments

Comments
 (0)