src/__tests__/parse-object.spec.js   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 1

Size

Lines of Code 158
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 110
mnd 0
bc 0
fnc 6
dl 0
loc 158
rs 10
bpm 0
cpm 1
noi 1
c 0
b 0
f 0
1
import { expect, describe, it } from '@jest/globals';
2
import Obj from '../objects.js';
3
import Int from '../int.js';
4
5
const subSchema = {
6
    a: Number,
7
    b: Boolean,
8
    'c?': String,
9
};
10
11
const testSchema = {
12
    a: Number,
13
    b: Boolean,
14
    'c?': String,
15
    '?d': Int,
16
    subSchema,
17
};
18
19
//  deepcode ignore ExpectsArray: False error, it should allow an object
20
const Test = Obj({ schema: testSchema });
21
22
describe('Test objects.js parse', () => {
23
    it('It should return the parsed object', () => {
24
        const input = {
25
            a: 1,
26
            b: true,
27
            c: 'test',
28
            d: 42,
29
            subSchema: {
30
                a: 1,
31
                b: true,
32
                c: 'test',
33
            },
34
        };
35
36
        expect(Test.parse(input)).toEqual({
37
            a: 1,
38
            b: true,
39
            c: 'test',
40
            d: 42,
41
            subSchema: {
42
                a: 1,
43
                b: true,
44
                c: 'test',
45
            },
46
        });
47
    });
48
49
    it('It should parse the values', () => {
50
        const input = {
51
            a: '1',
52
            b: 'true',
53
            c: 3,
54
            d: 42,
55
            subSchema: {
56
                a: '1',
57
                b: 'true',
58
                c: 3,
59
            },
60
        };
61
62
        expect(Test.parse(input)).toEqual({
63
            a: 1,
64
            b: true,
65
            c: '3',
66
            d: 42,
67
            subSchema: {
68
                a: 1,
69
                b: true,
70
                c: '3',
71
            },
72
        });
73
    });
74
75
    it('It should ignore optional fields', () => {
76
        const input = {
77
            a: '1',
78
            b: 'true',
79
            c: null,
80
            subSchema: {
81
                a: '1',
82
                b: 'true',
83
            },
84
        };
85
86
        expect(Test.parse(input)).toEqual({
87
            a: 1,
88
            b: true,
89
            c: null,
90
            subSchema: {
91
                a: 1,
92
                b: true,
93
            },
94
        });
95
96
        const input2 = {
97
            a: '1',
98
            b: 'true',
99
            subSchema: {
100
                a: '1',
101
                b: 'true',
102
            },
103
        };
104
105
        expect(Test.parse(input2)).toEqual({
106
            a: 1,
107
            b: true,
108
            subSchema: {
109
                a: 1,
110
                b: true,
111
            },
112
        });
113
    });
114
115
    it('It should validate the object', () => {
116
        const input = {
117
            a: '101',
118
            b: 'true',
119
            c: 102,
120
            subSchema: {
121
                a: '103',
122
                b: 'true',
123
                c: 'test with validation',
124
            },
125
        };
126
127
        expect(Test.parse(input, { validate: true })).toEqual({
128
            a: 101,
129
            b: true,
130
            c: '102',
131
            subSchema: {
132
                a: 103,
133
                b: true,
134
                c: 'test with validation',
135
            },
136
        });
137
    });
138
139
    it('It should handle a boolean with a string false', () => {
140
        const input = {
141
            a: '101',
142
            b: 'false',
143
            subSchema: {
144
                a: '103',
145
                b: 'false'
0 ignored issues
show
introduced by
Insert ,
Loading history...
146
            },
147
        };
148
149
        expect(Test.parse(input)).toEqual({
150
            a: 101,
151
            b: false,
152
            subSchema: {
153
                a: 103,
154
                b: false,
155
            },
156
        });
157
    });
158
});
159