Completed
Push — master ( 64f3a1...18f77b )
by Pieter Epeüs
23s queued 11s
created

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

Complexity

Total Complexity 15
Complexity/F 1

Size

Lines of Code 452
Function Count 15

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 332
mnd 0
bc 0
fnc 15
dl 0
loc 452
rs 10
bpm 0
cpm 1
noi 1
c 0
b 0
f 0
1
import Obj from '../objects';
2
3
const ObjectWithoutSchema = Obj();
4
5
const TestCases = [
6
    {
7
        description: 'Simple object',
8
        input: {
9
            a: 1,
10
            b: 2,
11
        },
12
        expectedResult: {
13
            a: 1,
14
            b: 2,
15
        },
16
    },
17
    {
18
        description: '2 level objects',
19
        input: {
20
            a: {
21
                x: 'test 1',
22
                y: 'test 2',
23
            },
24
            b: {
25
                x: 'test 3',
26
                y: 'test 4',
27
            },
28
        },
29
        expectedResult: {
30
            'a.x': 'test 1',
31
            'a.y': 'test 2',
32
            'b.x': 'test 3',
33
            'b.y': 'test 4',
34
        },
35
    },
36
    {
37
        description: 'Complext object with multiple levels',
38
        input: {
39
            a: 1,
40
            b: 2,
41
            c: [3, 4],
42
            d: { e: 5, f: 6 },
43
            g: { h: { i: 7 } },
44
        },
45
        expectedResult: {
46
            a: 1,
47
            b: 2,
48
            'c.0': 3,
49
            'c.1': 4,
50
            'd.e': 5,
51
            'd.f': 6,
52
            'g.h.i': 7,
53
        },
54
    },
55
];
56
57
describe.each(TestCases)(
0 ignored issues
show
Bug introduced by
The variable describe seems to be never declared. If this is a global, consider adding a /** global: describe */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
58
    'Test objects.js',
59
    ({ description, input, expectedResult }) => {
60
        it(description, () => {
61
            expect(new ObjectWithoutSchema(input).flat).toMatchObject(
62
                expectedResult
63
            );
64
        });
65
    }
66
);
67
68
describe('Test objects.js methods', () => {
69
    it('Get the entries', () => {
70
        const input = {
71
            a: 1,
72
            b: 2,
73
            c: [3, 4],
74
            d: { e: 5, f: 6 },
75
            g: { h: { i: 7 } },
76
        };
77
        const expectedResult = [
78
            ['a', 1],
79
            ['b', 2],
80
            ['c.0', 3],
81
            ['c.1', 4],
82
            ['d.e', 5],
83
            ['d.f', 6],
84
            ['g.h.i', 7],
85
        ];
86
87
        expect(new ObjectWithoutSchema(input).entries()).toMatchObject(
88
            expectedResult
89
        );
90
    });
91
92
    it('Get the keys', () => {
93
        const input = {
94
            a: 1,
95
            b: 2,
96
            c: [3, 4],
97
            d: { e: 5, f: 6 },
98
            g: { h: { i: 7 } },
99
        };
100
        const expectedResult = ['a', 'b', 'c.0', 'c.1', 'd.e', 'd.f', 'g.h.i'];
101
102
        expect(new ObjectWithoutSchema(input).keys()).toMatchObject(
103
            expectedResult
104
        );
105
    });
106
107
    it('Get the values', () => {
108
        const input = {
109
            a: 1,
110
            b: 2,
111
            c: [3, 4],
112
            d: { e: 5, f: 6 },
113
            g: { h: { i: 7 } },
114
        };
115
        const expectedResult = [1, 2, 3, 4, 5, 6, 7];
116
117
        expect(new ObjectWithoutSchema(input).values()).toMatchObject(
118
            expectedResult
119
        );
120
    });
121
122
    it('Get the length', () => {
123
        const input = {
124
            a: 1,
125
            b: 2,
126
            c: [3, 4],
127
            d: { e: 5, f: 6 },
128
            g: { h: { i: 7 } },
129
        };
130
        const expectedResult = 7;
131
132
        expect(new ObjectWithoutSchema(input).length).toBe(expectedResult);
133
    });
134
});
135
136
const getTestCases = [
137
    {
138
        description: 'Key is missing, no defaultValue provided',
139
        arr: {},
140
        key: 'pizza',
141
        expectedValue: undefined,
142
    },
143
    {
144
        description: 'Key is missing, a defaultValue is provided',
145
        arr: {},
146
        key: 'pizza',
147
        defaultValue: 'margherita',
148
        expectedValue: 'margherita',
149
    },
150
    {
151
        description: 'Nested key',
152
        arr: {
153
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
154
            food: ['Pizza'],
155
            mice: ['Splinter'],
156
        },
157
        key: 'turtles.0',
158
        expectedValue: 'Donatello',
159
    },
160
    {
161
        description: 'Nested key',
162
        arr: {
163
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
164
            food: ['Pizza'],
165
            mice: ['Splinter'],
166
        },
167
        key: 'turtles',
168
        expectedValue: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
169
    },
170
    {
171
        description: 'Nested sub keys',
172
        arr: {
173
            a: 1,
174
            b: 2,
175
            c: [3, 4],
176
            d: { e: 5, f: 6 },
177
            g: { h: { i: 7 } },
178
        },
179
        key: 'd',
180
        expectedValue: { e: 5, f: 6 },
181
    },
182
    {
183
        description: 'Nested key from flat object',
184
        arr: {
185
            a: 1,
186
            b: 2,
187
            c: [3, 4],
188
            'd.e': 5,
189
            'd.f': 6,
190
            'g.h.i': 7,
191
        },
192
        key: 'd',
193
        expectedValue: { e: 5, f: 6 },
194
    },
195
    {
196
        description: 'Nested key from flat object with an array',
197
        arr: {
198
            a: 1,
199
            b: 2,
200
            c: [3, 4],
201
            'd.e': 5,
202
            'd.f': 6,
203
            'g.h.i': 7,
204
        },
205
        key: 'c',
206
        expectedValue: [3, 4],
207
    },
208
];
209
210
describe.each(getTestCases)(
211
    'Get value by key',
212
    ({ description, arr, key, defaultValue, expectedValue }) => {
213
        it(description, () => {
214
            expect(
215
                new ObjectWithoutSchema(arr).getByKey(key, defaultValue)
216
            ).toEqual(expectedValue);
217
        });
218
    }
219
);
220
221
const hasTestCases = [
222
    {
223
        description: 'Check a key that doesnt exists',
224
        arr: {},
225
        key: 'pizza',
226
        expectedValue: false,
227
    },
228
    {
229
        description: 'Simple key check',
230
        arr: {
231
            turtle: 'Leonardo',
232
            food: 'Pizza',
233
            mice: 'Splinter',
234
        },
235
        key: 'turtle',
236
        expectedValue: true,
237
    },
238
    {
239
        description: 'Nested key check',
240
        arr: {
241
            turtles: ['Donatello', 'Michelangelo', 'Raphael', 'Leonardo'],
242
            food: ['Pizza'],
243
            mice: ['Splinter'],
244
        },
245
        key: 'turtles.0',
246
        expectedValue: true,
247
    },
248
];
249
250
describe.each(hasTestCases)(
251
    'Check if the object has a key',
252
    ({ description, arr, key, expectedValue }) => {
253
        it(description, () => {
254
            expect(new ObjectWithoutSchema(arr).has(key)).toEqual(
255
                expectedValue
256
            );
257
        });
258
    }
259
);
260
261
const getKeysTestCases = [
262
    {
263
        description: 'Check if we can a single keys',
264
        arr: {
265
            turtle: 'Leonardo',
266
            food: 'Pizza',
267
            mice: 'Splinter',
268
        },
269
        keys: ['turtle'],
270
        expectedValue: {
271
            turtle: 'Leonardo',
272
        },
273
    },
274
    {
275
        description: 'Check if we can get multiple keys',
276
        arr: {
277
            turtle: 'Leonardo',
278
            food: 'Pizza',
279
            mice: 'Splinter',
280
        },
281
        keys: ['food', 'mice'],
282
        expectedValue: {
283
            food: 'Pizza',
284
            mice: 'Splinter',
285
        },
286
    },
287
    {
288
        description: 'Check if we only get existing keys',
289
        arr: {
290
            turtle: 'Leonardo',
291
            food: 'Pizza',
292
            mice: 'Splinter',
293
        },
294
        keys: ['food', 'drink'],
295
        defaultValue: {
296
            result: 'ok',
297
        },
298
        expectedValue: {
299
            food: 'Pizza',
300
        },
301
    },
302
    {
303
        description: 'Check if we get an empty object if no keys exist',
304
        arr: {
305
            turtle: 'Leonardo',
306
            food: 'Pizza',
307
            mice: 'Splinter',
308
        },
309
        keys: ['drink'],
310
        expectedValue: undefined,
311
    },
312
    {
313
        description: 'Check if we get the default value if no keys exists',
314
        arr: {
315
            turtle: 'Leonardo',
316
            food: 'Pizza',
317
            mice: 'Splinter',
318
        },
319
        keys: ['drink'],
320
        defaultValue: {
321
            result: 'ok',
322
        },
323
        expectedValue: {
324
            result: 'ok',
325
        },
326
    },
327
    {
328
        description: 'Check if we get deep nested keys',
329
        arr: {
330
            a: 1,
331
            b: 2,
332
            c: [3, 4],
333
            d: { e: 5, f: 6 },
334
            g: { h: { i: 7 } },
335
        },
336
        keys: ['a', 'c', 'd.e', 'g.h'],
337
        expectedValue: {
338
            a: 1,
339
            c: [3, 4],
340
            'd.e': 5,
341
            'g.h': { i: 7 },
342
        },
343
    },
344
];
345
346
describe.each(getKeysTestCases)(
347
    'Check if the object has the keys',
348
    ({ description, arr, keys, defaultValue, expectedValue }) => {
349
        it(description, () => {
350
            expect(
351
                new ObjectWithoutSchema(arr).getKeys(keys, defaultValue)
352
            ).toEqual(expectedValue);
353
        });
354
    }
355
);
356
357
const getFlatKeysTestCases = [
358
    {
359
        description: 'Check if we can a single keys',
360
        arr: {
361
            turtle: 'Leonardo',
362
            food: 'Pizza',
363
            mice: 'Splinter',
364
        },
365
        keys: ['turtle'],
366
        expectedValue: {
367
            turtle: 'Leonardo',
368
        },
369
    },
370
    {
371
        description: 'Check if we can get multiple keys',
372
        arr: {
373
            turtle: 'Leonardo',
374
            food: 'Pizza',
375
            mice: 'Splinter',
376
        },
377
        keys: ['food', 'mice'],
378
        expectedValue: {
379
            food: 'Pizza',
380
            mice: 'Splinter',
381
        },
382
    },
383
    {
384
        description: 'Check if we only get existing keys',
385
        arr: {
386
            turtle: 'Leonardo',
387
            food: 'Pizza',
388
            mice: 'Splinter',
389
        },
390
        keys: ['food', 'drink'],
391
        defaultValue: {
392
            result: 'ok',
393
        },
394
        expectedValue: {
395
            food: 'Pizza',
396
        },
397
    },
398
    {
399
        description: 'Check if we get an empty object if no keys exist',
400
        arr: {
401
            turtle: 'Leonardo',
402
            food: 'Pizza',
403
            mice: 'Splinter',
404
        },
405
        keys: ['drink'],
406
        expectedValue: undefined,
407
    },
408
    {
409
        description: 'Check if we get the default value if no keys exists',
410
        arr: {
411
            turtle: 'Leonardo',
412
            food: 'Pizza',
413
            mice: 'Splinter',
414
        },
415
        keys: ['drink'],
416
        defaultValue: {
417
            result: 'ok',
418
        },
419
        expectedValue: {
420
            result: 'ok',
421
        },
422
    },
423
    {
424
        description: 'Check if we get deep nested keys',
425
        arr: {
426
            a: 1,
427
            b: 2,
428
            c: [3, 4],
429
            d: { e: 5, f: 6 },
430
            g: { h: { i: 7 } },
431
        },
432
        keys: ['a', 'c', 'd.e', 'g.h'],
433
        expectedValue: {
434
            a: 1,
435
            'c.0': 3,
436
            'c.1': 4,
437
            'd.e': 5,
438
            'g.h.i': 7,
439
        },
440
    },
441
];
442
443
describe.each(getFlatKeysTestCases)(
444
    'Check if the object has the keys (flat',
445
    ({ description, arr, keys, defaultValue, expectedValue }) => {
446
        it(description, () => {
447
            expect(
448
                new ObjectWithoutSchema(arr).getFlatKeys(keys, defaultValue)
449
            ).toEqual(expectedValue);
450
        });
451
    }
452
);
453