-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathspec.js
134 lines (115 loc) · 2.88 KB
/
spec.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const reduce = require('.');
const result = () => new Promise(resolve => setTimeout(() => resolve('A'), 40));
describe('await-reduce', async() => {
let array;
beforeEach(() => {
array = [
result(),
result(),
result(),
];
});
function verifyArray(arr = array) {
expect(arr).to.have.lengthOf(3);
assert(
arr.every(i => (i instanceof Promise)),
'Should be an array of promises',
);
}
it('Should reduce array to promises', verifyArray);
it('Should not mutate original array', async() => {
await reduce(array, () => null);
verifyArray();
});
it('Should reduce array to promises results', async() =>
assert(
(await reduce(
array,
async (results, result) => [ ...results, result ],
[],
)).every(item => (item === 'A')),
'Should be an array of results',
),
);
it('Should use first items value as default initial value', async() =>
expect(
await reduce(
array,
(collector, item) => [ collector, item ].join(''),
),
'Should be an array of results',
).to.equal('AAA'),
);
it('Should use passed initial value', async() =>
expect(
await reduce(
array,
(collector, item) => [ collector, item ].join(''),
'B',
),
'Should be an array of results',
).to.equal('BAAA'),
);
it('Should wait for async reducer functions as well', async() =>
expect(
await reduce(
array,
(collector, item) => new Promise(
resolve => setTimeout(
() => resolve([ collector, item ].join('')),
40,
),
),
'B',
),
'Should be an array of results',
).to.equal('BAAA'),
);
it('Should pass the index as third argument to reducer', async() => {
const results = [];
await reduce(
array,
(collector, item, index) => results.push(index),
{},
);
expect(results).to.include(0);
expect(results).to.include(1);
expect(results).to.include(2);
expect(results).to.not.include(3);
});
it('Should skip first iteration when initial value is passed', async() => {
const results = [];
await reduce(
array,
(collector, item, index) => results.push(index),
);
expect(results).to.not.include(0);
expect(results).to.include(1);
expect(results).to.include(2);
expect(results).to.not.include(3);
});
it('Should pass the array of results as fourth argument to reducer', async() =>
await reduce(
array,
(collector, item, index, arr) => (arr === 'AAA'),
),
);
it('Works on a normal array as well', async() =>
expect(await reduce(
[ 'B', 'B', 'B' ],
(collector, item) => [ ...collector, item ],
[],
)).to.deep.equal([ 'B', 'B', 'B' ]),
);
it('Should resolve promises in the reduced array', async() => {
expect(await reduce(
[
new Promise(resolve => resolve('A')),
new Promise(resolve => resolve('B')),
new Promise(resolve => resolve('C')),
],
(collector, item) => [ ...collector, item ],
[],
)).to.deep.equal([ 'A', 'B', 'C' ]);
});
});