-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
value-promise.unit.ts
361 lines (317 loc) · 10.9 KB
/
value-promise.unit.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
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/context
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {expect} from '@loopback/testlab';
import {
getDeepProperty,
resolveList,
resolveMap,
resolveUntil,
transformValueOrPromise,
tryWithFinally,
} from '../..';
describe('tryWithFinally', () => {
it('performs final action for a fulfilled promise', async () => {
let finalActionInvoked = false;
const action = () => Promise.resolve(1);
const finalAction = () => (finalActionInvoked = true);
await tryWithFinally(action, finalAction);
expect(finalActionInvoked).to.be.true();
});
it('performs final action for a resolved value', () => {
let finalActionInvoked = false;
const action = () => 1;
const finalAction = () => (finalActionInvoked = true);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
tryWithFinally(action, finalAction);
expect(finalActionInvoked).to.be.true();
});
it('performs final action for a rejected promise', async () => {
let finalActionInvoked = false;
const action = () => Promise.reject(new Error('error'));
const finalAction = () => (finalActionInvoked = true);
await expect(tryWithFinally(action, finalAction)).be.rejectedWith('error');
expect(finalActionInvoked).to.be.true();
});
it('performs final action for an action that throws an error', () => {
let finalActionInvoked = false;
const action = () => {
throw new Error('error');
};
const finalAction = () => (finalActionInvoked = true);
expect(() => tryWithFinally(action, finalAction)).to.throw('error');
expect(finalActionInvoked).to.be.true();
});
});
describe('getDeepProperty', () => {
it('gets the root value if path is empty', () => {
const obj = {x: {y: 1}};
expect(getDeepProperty(obj, '')).to.eql(obj);
});
it('gets the root value with a name', () => {
const obj = {x: {y: 1}};
expect(getDeepProperty(obj, 'x')).to.eql({y: 1});
});
it('gets the root value with a path', () => {
const obj = {x: {y: 1}};
expect(getDeepProperty(obj, 'x.y')).to.eql(1);
});
it('returns undefined for non-existent path', () => {
const obj = {x: {y: 1}};
expect(getDeepProperty(obj, 'x.z')).to.be.undefined();
});
it('allows undefined value', () => {
expect(getDeepProperty(undefined, 'x.z')).to.be.undefined();
expect(getDeepProperty(null, 'x.z')).to.be.undefined();
});
it('allows null value', () => {
expect(getDeepProperty({x: {z: null}}, 'x.z')).to.be.null();
expect(getDeepProperty(null, '')).to.be.null();
});
it('allows boolean value', () => {
expect(getDeepProperty(true, 'x.z')).to.be.undefined();
});
it('allows number value', () => {
expect(getDeepProperty(1, 'x.z')).to.be.undefined();
expect(getDeepProperty(NaN, 'x.z')).to.be.undefined();
});
it('allows to get length string value', () => {
expect(getDeepProperty('xyz', 'length')).to.eql(3);
});
it('allows to get length and items of an array by index', () => {
const arr = ['x', 'y'];
expect(getDeepProperty(arr, 'length')).to.eql(2);
expect(getDeepProperty(arr, '0')).to.eql('x');
expect(getDeepProperty(arr, '1')).to.eql('y');
});
it('allows to get items of a nested array by index', () => {
const obj = {a: ['x', 'y']};
expect(getDeepProperty(obj, 'a.0')).to.eql('x');
expect(getDeepProperty(obj, 'a.1')).to.eql('y');
});
it('allows to use parameter types', () => {
const arr = ['x', 'y'];
expect(getDeepProperty<number>(arr, 'length')).to.eql(2);
expect(getDeepProperty<string>(arr, '0')).to.eql('x');
expect(getDeepProperty<string, string[]>(arr, '1')).to.eql('y');
expect(getDeepProperty<string>(arr, '1')).to.eql('y');
});
});
describe('resolveList', () => {
it('resolves an array of values', () => {
const source = ['a', 'b'];
const result = resolveList(source, v => v.toUpperCase());
expect(result).to.eql(['A', 'B']);
});
it('resolves an array of promises', async () => {
const source = ['a', 'b'];
const result = await resolveList(source, v =>
Promise.resolve(v.toUpperCase()),
);
expect(result).to.eql(['A', 'B']);
});
it('resolves an array of promises or values', async () => {
const source = ['a', 'b'];
const result = await resolveList(source, v =>
v === 'a' ? 'A' : Promise.resolve(v.toUpperCase()),
);
expect(result).to.eql(['A', 'B']);
});
it('resolves an array of promises or values with index', async () => {
const source = ['a', 'b'];
const result = await resolveList(source, (v, i) =>
v === 'a' ? 'A' + i : Promise.resolve(v.toUpperCase() + i),
);
expect(result).to.eql(['A0', 'B1']);
});
it('resolves an object of values with index and array', () => {
const result = resolveList(['a', 'b'], (v, i, list) => {
return v.toUpperCase() + i + list.length;
});
expect(result).to.eql(['A02', 'B12']);
});
});
describe('resolveMap', () => {
it('resolves an object of values', () => {
const source = {a: 'x', b: 'y'};
const result = resolveMap(source, v => v.toUpperCase());
expect(result).to.eql({a: 'X', b: 'Y'});
});
it('does not set a key with value undefined', () => {
const source = {a: 'x', b: undefined};
const result = resolveMap(source, v => v && v.toUpperCase());
expect(result).to.not.have.property('b');
expect(result).to.eql({a: 'X'});
});
it('resolves an object of promises', async () => {
const source = {a: 'x', b: 'y'};
const result = await resolveMap(source, v =>
Promise.resolve(v.toUpperCase()),
);
expect(result).to.eql({a: 'X', b: 'Y'});
});
it('does not set a key with promise resolved to undefined', async () => {
const source = {a: 'x', b: undefined};
const result = await resolveMap(source, v =>
Promise.resolve(v && v.toUpperCase()),
);
expect(result).to.not.have.property('b');
expect(result).to.eql({a: 'X'});
});
it('resolves an object of promises or values', async () => {
const source = {a: 'x', b: 'y'};
const result = await resolveMap(source, v =>
v === 'x' ? 'X' : Promise.resolve(v.toUpperCase()),
);
expect(result).to.eql({a: 'X', b: 'Y'});
});
it('resolves an object of promises or values with key', async () => {
const source = {a: 'x', b: 'y'};
const result = await resolveMap(source, (v, p) =>
v === 'x' ? 'X' + p : Promise.resolve(v.toUpperCase() + p),
);
expect(result).to.eql({a: 'Xa', b: 'Yb'});
});
it('resolves an object of values with key and object', () => {
const result = resolveMap({a: 'x', b: 'y'}, (v, p, map) => {
return v.toUpperCase() + p + Object.keys(map).length;
});
expect(result).to.eql({a: 'Xa2', b: 'Yb2'});
});
});
describe('resolveUntil', () => {
it('resolves an iterator of values', () => {
const source = ['a', 'b', 'c'];
const result = resolveUntil<string, string>(
source[Symbol.iterator](),
v => v.toUpperCase(),
(s, v) => s === 'a',
);
expect(result).to.eql('A');
});
it('resolves an iterator of values until the end', () => {
const source = ['a', 'b', 'c'];
const result = resolveUntil<string, string>(
source[Symbol.iterator](),
v => v.toUpperCase(),
(s, v) => false,
);
expect(result).to.be.undefined();
});
it('resolves an iterator of promises', async () => {
const source = ['a', 'b', 'c'];
const result = await resolveUntil<string, string>(
source[Symbol.iterator](),
v => Promise.resolve(v.toUpperCase()),
(s, v) => true,
);
expect(result).to.eql('A');
});
it('handles a rejected promise from resolver', async () => {
const source = ['a', 'b', 'c'];
const result = resolveUntil<string, string>(
source[Symbol.iterator](),
v => Promise.reject(new Error(v)),
(s, v) => true,
);
await expect(result).be.rejectedWith('a');
});
it('reports an error thrown from resolver', () => {
const source = ['a', 'b', 'c'];
const task = () =>
resolveUntil<string, string>(
source[Symbol.iterator](),
v => {
throw new Error(v);
},
(s, v) => true,
);
expect(task).throw('a');
});
it('handles a rejected promise from evaluator', async () => {
const source = ['a', 'b', 'c'];
const result = resolveUntil<string, string>(
source[Symbol.iterator](),
async v => v.toUpperCase(),
(s, v) => {
throw new Error(v);
},
);
await expect(result).be.rejectedWith('A');
});
it('handles mixed value and promise items ', async () => {
const source = ['b', 'C', 'B', 'c', 'a', 'A'];
const result = await resolveUntil<string, string>(
source[Symbol.iterator](),
v => {
const up = v.toUpperCase();
if (up === v) return up;
// Produces a value for upper case
else return Promise.resolve(up); // Produces a promise for lower case
},
(s, v) => s === 'a',
);
expect(result).to.eql('A');
});
it('does not cause stack overflow for large # of value items', () => {
// Create a large array of 1000 items
const source = new Array<string>(1000);
// Fill 0-949 with 'b'
source.fill('b', 0, 950);
// Fill 950-999 with 'a'
source.fill('a', 950);
const result = resolveUntil<string, string>(
source[Symbol.iterator](),
v => v.toUpperCase(),
(s, v) => s === 'a',
);
expect(result).to.eql('A');
});
it('does not cause stack overflow for large # of promise items', async () => {
// Create a large array of 1000 items
const source = new Array<string>(1000);
// Fill 0-949 with 'b'
source.fill('b', 0, 950);
// Fill 950-999 with 'a'
source.fill('a', 950);
const result = await resolveUntil<string, string>(
source[Symbol.iterator](),
v => Promise.resolve(v.toUpperCase()),
(s, v) => s === 'a',
);
expect(result).to.eql('A');
});
});
describe('transformValueOrPromise', () => {
it('transforms a value', () => {
const result = transformValueOrPromise('a', v => v && v.toUpperCase());
expect(result).to.eql('A');
});
it('transforms a promise', async () => {
const result = await transformValueOrPromise(
Promise.resolve('a'),
v => v && v.toUpperCase(),
);
expect(result).to.eql('A');
});
it('transforms a value to promise', async () => {
const result = await transformValueOrPromise('a', v =>
Promise.resolve(v && v.toUpperCase()),
);
expect(result).to.eql('A');
});
it('handles a rejected promise from the transformer', async () => {
const result = transformValueOrPromise('a', v =>
Promise.reject(new Error(v)),
);
await expect(result).be.rejectedWith('a');
});
it('handles an error thrown from the transformer', () => {
expect(() =>
transformValueOrPromise('a', v => {
throw new Error(v);
}),
).to.throw('a');
});
});