-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
435 lines (386 loc) · 14.6 KB
/
index.test.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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
const {entries, every, filter, find, findKey, flat, flatMap, forEach, includes, keyOf, keys, map, mapKeys, reduce, some, values} = require('./index');
describe('entries', () => {
test('returns an iterator object that contains the key/value pairs for each entry in `obj`', () => {
const value = {};
const obj = {a: 1, b: 2, c: value};
const keys = Object.keys(obj);
const values = Object.values(obj);
const result = entries(obj);
expect(typeof result[Symbol.iterator]).toBe('function');
for (const [key, value] of result) {
expect(keys).toContain(key);
expect(values).toContain(value);
}
});
});
describe('every', () => {
test('returns whether invoking `fn` for every entry in `obj` returns something truthy', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
expect(every(obj, () => true)).toBe(true);
expect(every(obj, () => false)).toBe(false);
expect(every(obj, val => val)).toBe(false);
expect(every(obj, val => !val)).toBe(true);
expect(every(obj, val => val === 0)).toBe(false);
});
test('only runs `fn` until it returns something falsy', () => {
const obj = {a: 1, b: 2, c: 3};
const fn = jest.fn(() => false);
every(obj, fn);
expect(fn.mock.calls.length).toBe(1);
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
every(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('filter', () => {
test('returns a new object containing each entry in `obj` for which invoking `fn` returns something truthy', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
expect(filter(obj, () => true)).not.toBe(obj);
expect(filter(obj, () => true)).toEqual(obj);
expect(filter(obj, () => false)).toEqual({});
expect(filter(obj, val => val)).toEqual({});
expect(filter(obj, val => !val)).toEqual(obj);
expect(filter(obj, val => val == null)).toEqual({d: undefined, e: null});
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
filter(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('find', () => {
test('returns the value of the first entry in `obj` for which invoking `fn` returns something truthy', () => {
const value = {};
const obj = {a: 1, b: {}, c: value};
expect(find(obj, val => typeof val === 'number')).toBe(1);
expect(find(obj, val => val === value)).toBe(value);
expect(find(obj, (val, key) => key === 'c')).toBe(value);
});
test('returns `undefined` if invoking `fn` for each entry in `obj` returns something falsy', () => {
const obj = {a: 1, b: 2, c: 3};
const result = find(obj, () => false);
expect(result).toBe(undefined);
});
test('returns `undefined` if invoking `fn` for each entry in `obj` returns something falsy even when there is a key of "undefined"', () => {
const obj = {undefined: 1};
const result = find(obj, () => false);
expect(result).toBe(undefined);
});
test('only runs `fn` until it returns something truthy', () => {
const obj = {a: 1, b: 2, c: 3};
const fn = jest.fn(() => true);
find(obj, fn);
expect(fn.mock.calls.length).toBe(1);
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
find(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('findKey', () => {
test('returns the key of the first entry in `obj` for which invoking `fn` returns something truthy', () => {
const result = {};
const obj = {a: 1, b: {}, c: result};
expect(findKey(obj, value => typeof value === 'number')).toBe('a');
expect(findKey(obj, value => value === result)).toBe('c');
expect(findKey(obj, (value, key) => key === 'c')).toBe('c');
});
test('returns `undefined` if invoking `fn` for each entry in `obj` returns something falsy', () => {
const obj = {a: 1, b: 2, c: 3};
const result = findKey(obj, () => false);
expect(result).toBe(undefined);
});
test('only runs `fn` until it returns truthy', () => {
const obj = {a: 1, b: 2, c: 3};
const fn = jest.fn(() => true);
findKey(obj, fn);
expect(fn.mock.calls.length).toBe(1);
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
findKey(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('flat', () => {
test('creates a new object with all sub-elements merged into it', () => {
const obj = {a: 1, b: {c: 1}};
expect(flat(obj)).toEqual({a: 1, c: 1});
});
test('returns a new object even when the depth is 0', () => {
const obj = {a: 1};
expect(flat(obj, 0)).not.toBe(obj);
expect(flat(obj, 0)).toEqual(obj);
});
test('recursively flattens to the specified depth', () => {
const obj = {a: {b: {c: {d: {e: 1}}}}};
expect(flat(obj, 0)).toEqual(obj);
expect(flat(obj, 1)).toEqual({b: {c: {d: {e: 1}}}});
expect(flat(obj, 2)).toEqual({c: {d: {e: 1}}});
expect(flat(obj, 3)).toEqual({d: {e: 1}});
expect(flat(obj, 4)).toEqual({e: 1});
});
test('discontinues when the object is completely flat', () => {
const obj = {a: {b: {c: {d: {e: 1}}}}};
expect(flat(obj, Infinity)).toEqual({e: 1});
});
test('does not treat `null` as an object', () => {
const obj = {a: null};
expect(flat(obj, Infinity)).toEqual(obj);
});
});
describe('flatMap', () => {
test('returns a new flattened object with values returned by invoking `fn` for each entry in `obj`', () => {
const obj = {a: 1, b: 2};
const fn = jest.fn()
.mockReturnValueOnce({c: 3, d: 4})
.mockReturnValueOnce({e: 5, f: 6});
const result = flatMap(obj, fn);
expect(result).not.toBe(obj);
expect(result).toEqual({c: 3, d: 4, e: 5, f: 6});
});
test('only flattens to a depth of 1', () => {
const obj = {a: 1, b: 2};
const fn = jest.fn()
.mockReturnValueOnce({c: {d: 4}})
.mockReturnValueOnce({e: 5});
const result = flatMap(obj, fn);
expect(result).toEqual({c: {d: 4}, e: 5});
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
flatMap(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
test('only flattens objects returned by `fn`', () => {
const obj = {a: 0, b: 1};
const fn = jest.fn()
.mockReturnValueOnce('a')
.mockReturnValueOnce({c: 3});
expect(flatMap(obj, fn)).toEqual({a: 'a', c: 3});
});
test('does not treat `null` as an object', () => {
const obj = {a: 1};
const fn = jest.fn().mockReturnValueOnce(null);
expect(flatMap(obj, fn)).toEqual({a: null});
});
});
describe('forEach', () => {
test('invokes `fn` for each entry in `obj`', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
const fn = jest.fn();
forEach(obj, fn);
expect(fn.mock.calls.length).toBe(Object.entries(obj).length);
});
test('returns `undefined`', () => {
const obj = {a: 1, b: 2, c: 3};
expect(forEach(obj, () => true)).toBe(undefined);
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
forEach(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('includes', () => {
test('returns `true` if `value` is one of the values in `obj`', () => {
const value = {};
const obj = {a: 1, b: {}, c: value};
expect(includes(obj, value)).toBe(true);
});
test('returns `false` if `value` is not one of the values in `obj`', () => {
const obj = {a: 1, b: 2};
expect(includes(obj, {})).toBe(false);
});
});
describe('keyOf', () => {
test('returns the key of the first entry in `obj` that has a value of `value`', () => {
const value = {};
const obj = {a: 1, b: {}, c: value};
expect(keyOf(obj, 1)).toBe('a');
expect(keyOf(obj, value)).toBe('c');
});
test('returns `undefined` if no entry in `obj` has a value of `value`', () => {
const obj = {a: 1, b: 2, c: 3};
expect(keyOf(obj, false)).toBe(undefined);
});
});
describe('keys', () => {
test('returns an iterator object that contains the keys for each entry in `obj`', () => {
const value = {};
const obj = {a: 1, b: 2, c: value};
const objKeys = Object.keys(obj);
const result = keys(obj);
expect(typeof result[Symbol.iterator]).toBe('function');
for (const key of result) {
expect(objKeys).toContain(key);
}
});
});
describe('map', () => {
test('returns a new object with values returned by invoking `fn` for each entry in `obj`', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
expect(map(obj, value => value)).not.toBe(obj);
expect(map(obj, value => value)).toEqual(obj);
expect(map(obj, value => typeof value)).toEqual({a: 'boolean', b: 'number', c: 'string', d: 'undefined', e: 'object'});
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
map(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('mapKeys', () => {
test('returns a new object with keys returned by invoking `fn` for each entry in `obj`', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
expect(mapKeys(obj, (value, key) => key)).not.toBe(obj);
expect(mapKeys(obj, (value, key) => key)).toEqual(obj);
expect(mapKeys(obj, value => typeof value)).toEqual({boolean: false, number: 0, string: '', undefined: undefined, object: null});
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
mapKeys(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('reduce', () => {
test('returns the result of invoking `fn` for each entry in `obj`', () => {
const obj = {a: 1, b: 2, c: 3, d: 4};
expect(reduce(obj, (acc, total) => acc + total)).toBe(10);
expect(reduce(obj, (acc, total) => acc + total, 0)).toBe(10);
expect(reduce(obj, (acc, total) => acc * total)).toBe(24);
expect(reduce(obj, (acc, total) => acc * total, 1)).toBe(24);
});
test('throws a `TypeError` if `initialValue` is not provided and `obj` is empty', () => {
const obj = {};
expect(() => reduce(obj, () => {})).toThrowError(TypeError);
});
test('does not invoke `fn` if `initialValue` is provided and `obj` is empty', () => {
const obj = {};
const fn = jest.fn();
const initialValue = {};
reduce(obj, fn, initialValue);
expect(fn.mock.calls.length).toBe(0);
});
test('does not invoke `fn` if `initialValue` is not provided and `obj` has one value', () => {
const obj = {a: 1};
const fn = jest.fn();
reduce(obj, fn);
expect(fn.mock.calls.length).toBe(0);
});
test('invokes `fn` first with `initialValue` if provided and `obj` is not empty', () => {
const obj = {a: 1};
const fn = jest.fn();
const initialValue = {};
reduce(obj, fn, initialValue);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(initialValue);
});
test('invokes `fn` with a value from `obj` first if `initialValue` is not provided and `obj` has more than one value', () => {
const obj = {a: {}, b: {}};
const fn = jest.fn();
reduce(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(Object.values(obj)).toContain(fn.mock.calls[0][0]);
});
test('invokes `fn` with a value from `obj` first if `initialValue` is not provided and `obj` has more than one value', () => {
const obj = {a: {}, b: {}};
const fn = jest.fn();
reduce(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(Object.values(obj)).toContain(fn.mock.calls[0][0]);
});
test('invokes `fn` with `accumulator`, `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
const initialValue = {};
reduce(obj, fn, initialValue);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(initialValue);
expect(fn.mock.calls[0][1]).toBe(value);
expect(fn.mock.calls[0][2]).toBe('a');
expect(fn.mock.calls[0][3]).toBe(obj);
});
});
describe('some', () => {
test('returns whether invoking `fn` for at least one entry in `obj` returns something truthy', () => {
const obj = {a: false, b: 0, c: '', d: undefined, e: null};
expect(some(obj, () => true)).toBe(true);
expect(some(obj, () => false)).toBe(false);
expect(some(obj, val => val)).toBe(false);
expect(some(obj, val => !val)).toBe(true);
expect(some(obj, val => val === 0)).toBe(true);
});
test('only runs `fn` until it returns something truthy', () => {
const obj = {a: 1, b: 2, c: 3};
const fn = jest.fn(() => true);
some(obj, fn);
expect(fn.mock.calls.length).toBe(1);
});
test('invokes `fn` with `value`, `key`, `obj`', () => {
const value = {};
const obj = {a: value};
const fn = jest.fn();
some(obj, fn);
expect(fn.mock.calls.length).toBe(1);
expect(fn.mock.calls[0][0]).toBe(value);
expect(fn.mock.calls[0][1]).toBe('a');
expect(fn.mock.calls[0][2]).toBe(obj);
});
});
describe('values', () => {
test('returns an iterator object that contains the values for each entry in `obj`', () => {
const value = {};
const obj = {a: 1, b: 2, c: value};
const objValues = Object.values(obj);
const result = values(obj);
expect(typeof result[Symbol.iterator]).toBe('function');
for (const value of result) {
expect(objValues).toContain(value);
}
});
});