forked from rlwhatley3/awaits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awaits.spec.ts
393 lines (322 loc) · 11 KB
/
awaits.spec.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// build checking
import { until, s, zip, unzip, series } from './dist/awaits.js'
// direct ts checking
// import { until, s, zip, unzip, series } from './awaits';
type pFactory = (resolves: number, rejects: number) => Array<Promise<string>>;
const RESOLVESTR = 'da-bears';
const REJECTSTR = 'rejected-da-bears';
const promiseFactory:pFactory = (resolves: number = 0, rejects: number = 0): Array<Promise<any>> => {
let ret = [];
for(let i = 0; i < resolves; i++) {
let p = new Promise((resolve, reject) => { setTimeout(() => { return resolve(RESOLVESTR) }, 800) });
p.catch(() => {});
ret.push(p);
}
for(let j = 0; j < rejects; j++) {
let p = new Promise((resolve, reject) => { setTimeout(() => { return reject(REJECTSTR) }, 800) });
p.catch(() => {})
ret.push(p)
}
return ret;
}
describe('exported objects: ', () => {
test('until should be a function', () => {
expect(typeof until === 'function').toEqual(true);;
});
test('s should be a function', () => {
expect(typeof s === 'function').toEqual(true)
});
test('zip should be a function', () => {
expect(typeof zip === 'function').toEqual(true);;
});
test('unzip should be a function', () => {
expect(typeof unzip === 'function').toEqual(true);;
});
test('series should be a function', () => {
expect(typeof series === 'function').toEqual(true);
});
});
describe('until/s: when passed a single promise: ', () => {
describe('and the promise resolves correctly: ', () => {
it('should return [null, data].', async () => {
let resolveCount = 1;
let rejectCount = 0;
let p = promiseFactory(resolveCount, rejectCount)[0];
let [err, data] = await until(p);
expect(Object.is(err, null)).toEqual(true);
expect(data).toEqual(RESOLVESTR)
expect(Object.is(data, RESOLVESTR)).toEqual(true);
});
});
describe('and the promise gets rejected', () => {
it('should return an error', async () => {
let resolveCount = 0;
let rejectCount = 1;
let p = promiseFactory(resolveCount, rejectCount)[0];
let [err, data] = await until(p);
expect(Object.is(err, null)).toEqual(false);
expect(err instanceof Error).toEqual(true);
expect(Object.is(data, null)).toEqual(true);
});
});
describe('and it is passed an invalid object', () => {
let resolveCount = 1;
let rejectCount = 0;
let promises = promiseFactory(resolveCount, rejectCount)
promises[0].then = null;
it('should return an error: ', async () => {
let [err, data] = await until(promises[0]);
expect(err instanceof Error).toEqual(true);
expect(Object.is(data, null)).toEqual(true);
});
});
});
describe('until/s: When passed an array of promises: ', () => {
describe('and the promise resolves correctly: ', () => {
let promiseCount = 3;
let promises = promiseFactory(promiseCount, 0);
test('it should return [null, data]', async () => {
let [err, data] = await until(promises);
expect(Object.is(err, null)).toEqual(true);
expect(data.length).toEqual(promiseCount);
});
});
describe('and one promise rejects', () => {
let resolveCount = 3;
let rejectCount = 1;
let promises = promiseFactory(resolveCount, rejectCount);
test('it should return [Error, null]', async () => {
let [err, data] = await until(promises);
expect(Object.is(data, null)).toEqual(true);
expect(err instanceof Error).toEqual(true);
});
});
describe('and it is passed an invalid object', () => {
let pCount = 3;
let promises = promiseFactory(pCount, 0)
promises[2].then = null;
test('it should return an error: ', async () => {
let [err, data] = await until(promises);
expect(Object.is(data, null)).toEqual(true);
expect(err instanceof Error).toEqual(true);
});
});
});
describe('zip: when passed an object with promises and promise arrays for values: ', () => {
describe('and the promises all resolve correctly: ', () => {
it('should return [null data]', async () => {
let mC = 2;
let pObj = {
"A": promiseFactory(1, 0)[0],
"B": promiseFactory(1, 0)[0],
"C": promiseFactory(mC, 0),
}
let [err, data] = await zip(pObj);
expect(Object.is(err, null)).toEqual(true);
expect(data["A"]).toEqual(RESOLVESTR);
expect(data["B"]).toEqual(RESOLVESTR);
expect(Array.isArray(data["C"])).toEqual(true);
expect(data["C"].length).toEqual(mC);
});
});
describe('with an invalid promise passed on the object: ', () => {
it('should return [Error, null]: ', async () => {
let mC = 2;
let d = promiseFactory(1, 0)[0];
d.then = null;
let pObj = {
"A": promiseFactory(1, 0)[0],
"B": promiseFactory(1, 0)[0],
"C": promiseFactory(mC, 0),
"D": d
}
let [errs, data] = await zip(pObj);
expect(Object.keys(errs).length).toEqual(Object.keys(pObj).length);
expect(errs['A']).toEqual(null);
expect(errs['B']).toEqual(null);
expect(errs['C']).toEqual(null);
expect(errs['D'] instanceof Error).toEqual(true);
expect(Object.is(data, null)).toEqual(false);
});
});
describe('with a rejecting promise passed: ', () => {
it('should return an equally shaped object of correlated errors', async () => {
let mC = 2;
let pObj = {
"A": promiseFactory(0, 1)[0],
"B": promiseFactory(1, 1),
"C": promiseFactory(mC, 0),
}
let [errs, data] = await zip(pObj);
expect(Object.is(errs, null)).toEqual(false);
expect(Object.keys(errs).length).toEqual(3);
expect(errs['A'] instanceof Error).toEqual(true);
expect(errs['B'] instanceof Error).toEqual(true);
expect(errs['C']).toEqual(null);
expect(Object.is(data, null)).toEqual(false);
});
});
});
describe('unzip: when passed an object with promises for values', () => {
describe('and all promises resolve correctly: ', () => {
it('should return the resolved key value store', async () => {
let mC = 2;
let pObj = {
"A": promiseFactory(1, 0)[0],
"B": promiseFactory(1, 0)[0],
"C": promiseFactory(mC, 0),
}
let data = await unzip(pObj);
expect(Object.keys(data).length).toEqual(Object.keys(pObj).length);
expect(data._valid).toEqual(true);
let pKeys = Object.keys(pObj);
for(let key in data) {
expect(pKeys.includes(key)).toEqual(true);
expect(Array.isArray(data[key])).toEqual(true);
expect(data[key].length).toEqual(2);
expect(Object.is(data[key][0], null)).toEqual(true);
if(Array.isArray(data[key][1])) {
expect(data[key][1].every((k: any) => Object.is(k, RESOLVESTR))).toEqual(true);
} else {
expect(Object.is(data[key][1], RESOLVESTR)).toEqual(true);
}
}
});
});
describe('and an invalid object is passed', () => {
it('should return an invalidated object', async () => {
let mC = 2;
let d = promiseFactory(1, 0)[0];
d.then = null;
let pObj = {
"A": promiseFactory(1, 0)[0],
"B": promiseFactory(1, 0)[0],
"C": promiseFactory(mC, 0),
"D": d
}
let data = await unzip(pObj);
expect(data._valid).toEqual(false);
});
});
describe('and a promise rejects: ', () => {
it('should have the rejected promise on the key value store', async () => {
let mC = 2;
let pObj = {
A: promiseFactory(0, 1)[0],
B: promiseFactory(1, 1),
C: promiseFactory(mC, 0)
}
let data = await unzip(pObj);
expect(data._valid).toEqual(true);
for(let key in data) {
expect(Array.isArray(data[key])).toEqual(true);
expect(data[key].length).toEqual(2);
}
expect(Object.is(data['A'][0], null)).toEqual(false);
expect(data['A'][0] instanceof Error).toEqual(true);
expect(Object.is(data['A'][1], null)).toEqual(true);
expect(Object.is(data['B'][0], null)).toEqual(false);
expect(data['B'][0] instanceof Error).toEqual(true);
expect(Object.is(data['B'][1], null)).toEqual(true);
expect(Object.is(data['C'][0], null)).toEqual(true);
expect(Array.isArray(data['C'][1])).toEqual(true);
expect(data['C'][1].length).toEqual(mC);
});
});
});
describe('series', () => {
describe('with an object', () => {
it('should execute the given iterator in series', async () => {
let initializerValue = Promise.resolve({ 'pre-a': 0 });
let seriesData: any = {
'a': 1,
'b': 2,
'c': 3
}
let [err, data] = await series(seriesData, (value:any, key:any, lastReturnedValue:any) => {
return Promise.resolve(value + 1);
}, initializerValue);
for(let key in data) {
expect(data[key]).toEqual(seriesData[key] + 1);
}
expect(Object.is(err, null)).toEqual(true);
});
describe('using the default function', () => {
it('should return the resolved values in order', async () => {
let initializerValue = Promise.resolve({ 'pre-a': 0 });
let seriesData: any = {
'a': 1,
'b': 2,
'c': 3,
'd': Promise.resolve(4)
}
let seriesDataTest: any = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
}
let [err, data] = await series(seriesData, null);
for (let key in data) {
expect(data[key]).toEqual(seriesDataTest[key]);
}
expect(Object.is(err, null)).toEqual(true);
});
});
describe('with an error', () => {
it('should return the correctly shaped error', async () => {
let initializerValue = Promise.resolve({ 'pre-a': 0 });
let seriesData: any = {
'a': 1,
'b': 2,
'c': 3
}
let [errs, data] = await series(seriesData, (value:any, key:any, lastReturnedValue:any) => {
return Promise.reject(value + 1);
}, initializerValue);
expect(Object.keys(errs).length).toEqual(3);
for(let key in errs) { expect(errs[key] instanceof Error).toEqual(true); }
for(let key in data) { expect(Object.is(data[key], null)).toEqual(true) }
});
});
});
describe('with an array', () => {
it('should execute the given array in series', async () => {
let initializerValue = 'pre-a';
let initializerValuePromise = Promise.resolve(initializerValue);
let seriesData: any = ['a', 'b', 'c'];
let [err, data] = await series(seriesData, (value:any, index:any, accumulater:any) => {
if(index == 0) {
expect(accumulater).toEqual(initializerValue);
} else {
expect(accumulater).toEqual(seriesData[index - 1] + 1);
}
return Promise.resolve(value + 1);
}, initializerValue);
for(let index in data) { expect(data[index]).toEqual(seriesData[index] + 1); }
expect(Object.is(err, null)).toEqual(true);
});
describe('using the default function', () => {
it('should return the resolved values in order', async () => {
let seriesData: any = ['a', 'b', 'c', Promise.resolve('d')];
let [err, data] = await series(seriesData);
expect(Object.is(err, null)).toEqual(true);
});
});
describe('with errors', () => {
it('should return an array of errors', async () => {
let initializerValue = 'pre-a';
let initializerValuePromise = Promise.resolve(initializerValue);
let seriesData: any = ['a', 'b', 'c'];
let [errs, data] = await series(seriesData, (value:any, index:any, accumulater:any) => {
return Promise.reject(value + 1);
}, initializerValue);
expect(errs.length).toEqual(3);
for(let index in errs) {
expect(Object.is(errs[index], null)).toEqual(false);
expect(errs[index] instanceof Error).toEqual(true);
}
});
});
});
});