Passed
Push — master ( 527212...0fe9bd )
by Pieter Epeüs
02:47 queued 52s
created

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

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 114
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 80
mnd 0
bc 0
fnc 4
dl 0
loc 114
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';
3
import Int from '../int';
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: 3,
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: '3',
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