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

Complexity

Total Complexity 9
Complexity/F 1

Size

Lines of Code 86
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 55
mnd 0
bc 0
fnc 9
dl 0
loc 86
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 testSchema = {
5
    a: Number,
6
    b: Number,
7
};
8
9
//  deepcode ignore ExpectsArray: False error, it should allow an object
10
const Test = Obj({ schema: testSchema });
11
12
describe('Test objects.js map', () => {
13
    it('It should map the object like the array map', () => {
14
        const input = {
15
            a: 1,
16
            b: 2,
17
        };
18
        const expectedResult = {
19
            a: 2,
20
            b: 4,
21
        };
22
23
        expect(Test.create(input).map((x) => x * 2)).toMatchObject(
24
            expectedResult
25
        );
26
    });
27
28
    it('It should map the flat object like the array map', () => {
29
        const input = {
30
            a: 1,
31
            b: 2,
32
            c: [3, 4],
33
            d: { e: 5, f: 6 },
34
            g: { h: { i: 7 } },
35
        };
36
        const expectedResult = {
37
            a: 2,
38
            b: 4,
39
            'c.0': 6,
40
            'c.1': 8,
41
            'd.e': 10,
42
            'd.f': 12,
43
            'g.h.i': 14,
44
        };
45
46
        expect(Test.create(input).flatMap((x) => x * 2)).toMatchObject(
47
            expectedResult
48
        );
49
    });
50
51
    it('It should use the key of the map', () => {
52
        const input = {
53
            a: 1,
54
            b: 2,
55
        };
56
        const expectedResult = {
57
            a: 'a',
58
            b: 'b',
59
        };
60
61
        expect(Test.create(input).map((x, y) => y)).toMatchObject(
62
            expectedResult
63
        );
64
    });
65
66
    it('It should use the original result of the map', () => {
67
        const input = {
68
            a: 1,
69
            b: 2,
70
        };
71
        const expectedResult = {
72
            a: {
73
                a: 1,
74
                b: 2,
75
            },
76
            b: {
77
                a: 1,
78
                b: 2,
79
            },
80
        };
81
82
        expect(Test.create(input).map((x, y, z) => z)).toMatchObject(
83
            expectedResult
84
        );
85
    });
86
});
87