src/__tests__/new-objects.spec.js   A
last analyzed

Complexity

Total Complexity 19
Complexity/F 1

Size

Lines of Code 583
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 431
mnd 0
bc 0
fnc 19
dl 0
loc 583
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
1
import { expect, describe, it } from '@jest/globals';
2
import Obj from '../objects.js';
3
4
const ObjectWithoutSchema = Obj();
5
6
const TestCases = [
7
    {
8
        description: 'Simple object',
9
        input: {
10
            a: 1,
11
            b: 2,
12
        },
13
        expectedResult: {
14
            a: 1,
15
            b: 2,
16
        },
17
    },
18
    {
19
        description: '2 level objects',
20
        input: {
21
            a: {
22
                x: 'test 1',
23
                y: 'test 2',
24
            },
25
            b: {
26
                x: 'test 3',
27
                y: 'test 4',
28
            },
29
        },
30
        expectedResult: {
31
            'a.x': 'test 1',
32
            'a.y': 'test 2',
33
            'b.x': 'test 3',
34
            'b.y': 'test 4',
35
        },
36
    },
37
    {
38
        description: 'Complext object with multiple levels',
39
        input: {
40
            a: 1,
41
            b: 2,
42
            c: [3, 4],
43
            d: { e: 5, f: 6 },
44
            g: { h: { i: 7 } },
45
        },
46
        expectedResult: {
47
            a: 1,
48
            b: 2,
49
            'c.0': 3,
50
            'c.1': 4,
51
            'd.e': 5,
52
            'd.f': 6,
53
            'g.h.i': 7,
54
        },
55
    },
56
];
57
58
describe.each(TestCases)(
59
    'Test objects.js',
60
    ({ description, input, expectedResult }) => {
61
        it(description, () => {
62
            expect(ObjectWithoutSchema.create(input).flat).toMatchObject(
63
                expectedResult
64
            );
65
        });
66
    }
67
);
68
69
describe('Test objects.js methods', () => {
70
    it('Get the entries', () => {
71
        const input = {
72
            a: 1,
73
            b: 2,
74
            c: [3, 4],
75
            d: { e: 5, f: 6 },
76
            g: { h: { i: 7 } },
77
        };
78
        const expectedResult = [
79
            ['a', 1],
80
            ['b', 2],
81
            ['c.0', 3],
82
            ['c.1', 4],
83
            ['d.e', 5],
84
            ['d.f', 6],
85
            ['g.h.i', 7],
86
        ];
87
88
        expect(ObjectWithoutSchema.create(input).entries()).toMatchObject(
89
            expectedResult
90
        );
91
    });
92
93
    it('Get the keys', () => {
94
        const input = {
95
            a: 1,
96
            b: 2,
97
            c: [3, 4],
98
            d: { e: 5, f: 6 },
99
            g: { h: { i: 7 } },
100
        };
101
        const expectedResult = ['a', 'b', 'c.0', 'c.1', 'd.e', 'd.f', 'g.h.i'];
102
103
        expect(ObjectWithoutSchema.create(input).keys()).toMatchObject(
104
            expectedResult
105
        );
106
    });
107
108
    it('Get the values', () => {
109
        const input = {
110
            a: 1,
111
            b: 2,
112
            c: [3, 4],
113
            d: { e: 5, f: 6 },
114
            g: { h: { i: 7 } },
115
        };
116
        const expectedResult = [1, 2, 3, 4, 5, 6, 7];
117
118
        expect(ObjectWithoutSchema.create(input).values()).toMatchObject(
119
            expectedResult
120
        );
121
    });
122
123
    it('Get the length', () => {
124
        const input = {
125
            a: 1,
126
            b: 2,
127
            c: [3, 4],
128
            d: { e: 5, f: 6 },
129
            g: { h: { i: 7 } },
130
        };
131
        const expectedResult = 7;
132
133
        expect(ObjectWithoutSchema.create(input).length).toBe(expectedResult);
134
    });
135
});
136
137
const getTestCases = [
138
    {
139
        description: 'Key is missing, no defaultValue provided',
140
        arr: {},
141
        key: 'pizza',
142
        expectedValue: undefined,
143
    },
144
    {
145
        description: 'Key is missing, a defaultValue is provided',
146
        arr: {},
147
        key: 'pizza',
148
        defaultValue: 'margherita',
149
        expectedValue: 'margherita',
150
    },
151
    {
152
        description: 'Nested key',
153
        arr: {
154
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
155
            food: ['Pizza'],
156
            mice: ['Splinter'],
157
        },
158
        key: 'turtles.0',
159
        expectedValue: 'Donatello',
160
    },
161
    {
162
        description: 'Nested key',
163
        arr: {
164
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
165
            food: ['Pizza'],
166
            mice: ['Splinter'],
167
        },
168
        key: 'turtles',
169
        expectedValue: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
170
    },
171
    {
172
        description: 'Nested sub keys',
173
        arr: {
174
            a: 1,
175
            b: 2,
176
            c: [3, 4],
177
            d: { e: 5, f: 6 },
178
            g: { h: { i: 7 } },
179
        },
180
        key: 'd',
181
        expectedValue: { e: 5, f: 6 },
182
    },
183
    {
184
        description: 'Nested key from flat object',
185
        arr: {
186
            a: 1,
187
            b: 2,
188
            c: [3, 4],
189
            'd.e': 5,
190
            'd.f': 6,
191
            'g.h.i': 7,
192
        },
193
        key: 'd',
194
        expectedValue: { e: 5, f: 6 },
195
    },
196
    {
197
        description: 'Nested key from flat object with an array',
198
        arr: {
199
            a: 1,
200
            b: 2,
201
            c: [3, 4],
202
            'd.e': 5,
203
            'd.f': 6,
204
            'g.h.i': 7,
205
        },
206
        key: 'c',
207
        expectedValue: [3, 4],
208
    },
209
];
210
211
describe.each(getTestCases)(
212
    'Get value by key',
213
    ({ description, arr, key, defaultValue, expectedValue }) => {
214
        it(description, () => {
215
            expect(
216
                ObjectWithoutSchema.create(arr).getByKey(key, defaultValue)
217
            ).toEqual(expectedValue);
218
        });
219
    }
220
);
221
222
const hasTestCases = [
223
    {
224
        description: 'Check a key that doesnt exists',
225
        arr: {},
226
        key: 'pizza',
227
        expectedValue: false,
228
    },
229
    {
230
        description: 'Simple key check',
231
        arr: {
232
            turtle: 'Leonardo',
233
            food: 'Pizza',
234
            mice: 'Splinter',
235
        },
236
        key: 'turtle',
237
        expectedValue: true,
238
    },
239
    {
240
        description: 'Nested key check',
241
        arr: {
242
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
243
            food: ['Pizza'],
244
            mice: ['Splinter'],
245
        },
246
        key: 'turtles.0',
247
        expectedValue: true,
248
    },
249
];
250
251
describe.each(hasTestCases)(
252
    'Check if the object has a key',
253
    ({ description, arr, key, expectedValue }) => {
254
        it(description, () => {
255
            expect(ObjectWithoutSchema.create(arr).has(key)).toEqual(
256
                expectedValue
257
            );
258
        });
259
    }
260
);
261
262
const originalHasTestCases = [
263
    {
264
        description: 'Check a key that doesnt exists',
265
        arr: {},
266
        key: 'pizza',
267
        expectedValue: false,
268
    },
269
    {
270
        description: 'Simple key check',
271
        arr: {
272
            turtle: 'Leonardo',
273
            food: 'Pizza',
274
            mice: 'Splinter',
275
        },
276
        key: 'turtle',
277
        expectedValue: true,
278
    },
279
    {
280
        description: 'Nested key check',
281
        arr: {
282
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
283
            food: ['Pizza'],
284
            mice: ['Splinter'],
285
        },
286
        key: 'turtles.0',
287
        expectedValue: false,
288
    },
289
];
290
291
describe.each(originalHasTestCases)(
292
    'Check if the original object has a key',
293
    ({ description, arr, key, expectedValue }) => {
294
        it(description, () => {
295
            expect(ObjectWithoutSchema.create(arr).originalHas(key)).toEqual(
296
                expectedValue
297
            );
298
        });
299
    }
300
);
301
302
const getKeysTestCases = [
303
    {
304
        description: 'Check if we can a single keys',
305
        arr: {
306
            turtle: 'Leonardo',
307
            food: 'Pizza',
308
            mice: 'Splinter',
309
        },
310
        keys: ['turtle'],
311
        expectedValue: {
312
            turtle: 'Leonardo',
313
        },
314
    },
315
    {
316
        description: 'Check if we can get multiple keys',
317
        arr: {
318
            turtle: 'Leonardo',
319
            food: 'Pizza',
320
            mice: 'Splinter',
321
        },
322
        keys: ['food', 'mice'],
323
        expectedValue: {
324
            food: 'Pizza',
325
            mice: 'Splinter',
326
        },
327
    },
328
    {
329
        description: 'Check if we only get existing keys',
330
        arr: {
331
            turtle: 'Leonardo',
332
            food: 'Pizza',
333
            mice: 'Splinter',
334
        },
335
        keys: ['food', 'drink'],
336
        defaultValue: {
337
            result: 'ok',
338
        },
339
        expectedValue: {
340
            food: 'Pizza',
341
        },
342
    },
343
    {
344
        description: 'Check if we get an empty object if no keys exist',
345
        arr: {
346
            turtle: 'Leonardo',
347
            food: 'Pizza',
348
            mice: 'Splinter',
349
        },
350
        keys: ['drink'],
351
        expectedValue: undefined,
352
    },
353
    {
354
        description: 'Check if we get the default value if no keys exists',
355
        arr: {
356
            turtle: 'Leonardo',
357
            food: 'Pizza',
358
            mice: 'Splinter',
359
        },
360
        keys: ['drink'],
361
        defaultValue: {
362
            result: 'ok',
363
        },
364
        expectedValue: {
365
            result: 'ok',
366
        },
367
    },
368
    {
369
        description: 'Check if we get deep nested keys',
370
        arr: {
371
            a: 1,
372
            b: 2,
373
            c: [3, 4],
374
            d: { e: 5, f: 6 },
375
            g: { h: { i: 7 } },
376
        },
377
        keys: ['a', 'c', 'd.e', 'g.h'],
378
        expectedValue: {
379
            a: 1,
380
            c: [3, 4],
381
            'd.e': 5,
382
            'g.h': { i: 7 },
383
        },
384
    },
385
    {
386
        description: 'Check if we get items with a false value',
387
        arr: {
388
            a: true,
389
            b: false,
390
            c: 0,
391
            d: '',
392
        },
393
        keys: ['a', 'b', 'c', 'd', 'e'],
394
        expectedValue: {
395
            a: true,
396
            b: false,
397
            c: 0,
398
            d: '',
399
        },
400
    },
401
];
402
403
describe.each(getKeysTestCases)(
404
    'Check if the object has the keys',
405
    ({ description, arr, keys, defaultValue, expectedValue }) => {
406
        it(description, () => {
407
            expect(
408
                ObjectWithoutSchema.create(arr).getKeys(keys, defaultValue)
409
            ).toEqual(expectedValue);
410
        });
411
    }
412
);
413
414
const getFlatKeysTestCases = [
415
    {
416
        description: 'Check if we can a single keys',
417
        arr: {
418
            turtle: 'Leonardo',
419
            food: 'Pizza',
420
            mice: 'Splinter',
421
        },
422
        keys: ['turtle'],
423
        expectedValue: {
424
            turtle: 'Leonardo',
425
        },
426
    },
427
    {
428
        description: 'Check if we can get multiple keys',
429
        arr: {
430
            turtle: 'Leonardo',
431
            food: 'Pizza',
432
            mice: 'Splinter',
433
        },
434
        keys: ['food', 'mice'],
435
        expectedValue: {
436
            food: 'Pizza',
437
            mice: 'Splinter',
438
        },
439
    },
440
    {
441
        description: 'Check if we only get existing keys',
442
        arr: {
443
            turtle: 'Leonardo',
444
            food: 'Pizza',
445
            mice: 'Splinter',
446
        },
447
        keys: ['food', 'drink'],
448
        defaultValue: {
449
            result: 'ok',
450
        },
451
        expectedValue: {
452
            food: 'Pizza',
453
        },
454
    },
455
    {
456
        description: 'Check if we get an empty object if no keys exist',
457
        arr: {
458
            turtle: 'Leonardo',
459
            food: 'Pizza',
460
            mice: 'Splinter',
461
        },
462
        keys: ['drink'],
463
        expectedValue: undefined,
464
    },
465
    {
466
        description: 'Check if we get the default value if no keys exists',
467
        arr: {
468
            turtle: 'Leonardo',
469
            food: 'Pizza',
470
            mice: 'Splinter',
471
        },
472
        keys: ['drink'],
473
        defaultValue: {
474
            result: 'ok',
475
        },
476
        expectedValue: {
477
            result: 'ok',
478
        },
479
    },
480
    {
481
        description: 'Check if we get deep nested keys',
482
        arr: {
483
            a: 1,
484
            b: 2,
485
            c: [3, 4],
486
            d: { e: 5, f: 6 },
487
            g: { h: { i: 7 } },
488
        },
489
        keys: ['a', 'c', 'd.e', 'g.h'],
490
        expectedValue: {
491
            a: 1,
492
            'c.0': 3,
493
            'c.1': 4,
494
            'd.e': 5,
495
            'g.h.i': 7,
496
        },
497
    },
498
];
499
500
describe.each(getFlatKeysTestCases)(
501
    'Check if the object has the keys (flat)',
502
    ({ description, arr, keys, defaultValue, expectedValue }) => {
503
        it(description, () => {
504
            expect(
505
                ObjectWithoutSchema.create(arr).getFlatKeys(keys, defaultValue)
506
            ).toEqual(expectedValue);
507
        });
508
    }
509
);
510
511
const includesTestCases = [
512
    {
513
        description: 'Check if the array includes a key',
514
        arr: {
515
            a: 1,
516
            b: 2,
517
            c: [3, 4],
518
            d: { e: 5, f: 6 },
519
            test: { second: { third: 7 } },
520
        },
521
        key: 'a',
522
        expectedValue: true,
523
    },
524
    {
525
        description: 'Check if the array includes a sub key',
526
        arr: {
527
            a: 1,
528
            b: 2,
529
            c: [3, 4],
530
            d: { e: 5, f: 6 },
531
            test: { second: { third: 7 } },
532
        },
533
        key: 'd.e',
534
        expectedValue: true,
535
    },
536
    {
537
        description: 'Check if the array doesnt includes a key',
538
        arr: {
539
            a: 1,
540
            b: 2,
541
            c: [3, 4],
542
            d: { e: 5, f: 6 },
543
            test: { second: { third: 7 } },
544
        },
545
        key: 'd.g',
546
        expectedValue: false,
547
    },
548
    {
549
        description: 'Check if the array includes a part of a key',
550
        arr: {
551
            a: 1,
552
            b: 2,
553
            c: [3, 4],
554
            d: { e: 5, f: 6 },
555
            test: { second: { third: 7 } },
556
        },
557
        key: 'tes',
558
        expectedValue: true,
559
    },
560
    {
561
        description: 'Check if the array includes a part of a sub key',
562
        arr: {
563
            a: 1,
564
            b: 2,
565
            c: [3, 4],
566
            d: { e: 5, f: 6 },
567
            test: { second: { third: 7 } },
568
        },
569
        key: 'test.sec',
570
        expectedValue: true,
571
    },
572
];
573
574
describe.each(includesTestCases)(
575
    'Check if the object includes a key',
576
    ({ description, arr, key, expectedValue }) => {
577
        it(description, () => {
578
            expect(ObjectWithoutSchema.create(arr).includes(key)).toEqual(
579
                expectedValue
580
            );
581
        });
582
    }
583
);
584