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

Complexity

Total Complexity 29
Complexity/F 1

Size

Lines of Code 259
Function Count 29

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 29
eloc 186
mnd 0
bc 0
fnc 29
dl 0
loc 259
rs 10
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
1
/* eslint-disable max-statements */
2
/* eslint-disable no-new */
3
import { expect, describe, it } from '@jest/globals';
4
import Obj from '../objects.js';
5
import test1Schema from '../schemas/test1.js';
6
import Test2 from '../schemas/test2.js';
7
8
describe('Object test', () => {
9
    const addressSchema = {
10
        street: String,
11
        number: 'number',
12
        postalCode: String,
13
        city: String,
14
        country: String,
15
        build: Function,
16
        'example?': 'mixed',
17
        'obj?': 'object|string',
18
    };
19
20
    //  deepcode ignore ExpectsArray: False error, it should allow an object
21
    const Address = Obj({ schema: addressSchema });
22
    //  deepcode ignore ExpectsArray: False error, it should allow an object
23
    const Test = Obj({ schema: test1Schema });
24
    const test2 = new Test2('me');
25
26
    it('It should validate the input and set the original object', () => {
27
        const build = () => {};
28
        const myAddress = Address.create({
29
            street: 'Abc',
30
            number: 42,
31
            postalCode: '1234AB',
32
            city: 'Example',
33
            country: 'The Netherlands',
34
            build,
35
            example: 'ok',
36
            obj: { test: 'ok' },
37
        });
38
39
        expect(myAddress).toEqual({
40
            street: 'Abc',
41
            number: 42,
42
            postalCode: '1234AB',
43
            city: 'Example',
44
            country: 'The Netherlands',
45
            build,
46
            example: 'ok',
47
            obj: { test: 'ok' },
48
        });
49
50
        expect(myAddress.keys()).toEqual([
51
            'street',
52
            'number',
53
            'postalCode',
54
            'city',
55
            'country',
56
            'build',
57
            'example',
58
            'obj.test',
59
        ]);
60
    });
61
62
    it('It should throw an exception', () => {
63
        expect(() => {
64
            Address.create({
65
                street: 'Abc',
66
                number: 'xyz',
67
                postalCode: '1234AB',
68
                city: 'Example',
69
                country: 'The Netherlands',
70
            });
71
        }).toThrowError('The field number should be a number ("xyz")');
72
    });
73
74
    it('It should throw an exception', () => {
75
        expect(() => {
76
            Address.create({
77
                street: 'Abc',
78
                number: 42,
79
                postalCode: '1234AB',
80
                city: 'Example',
81
            });
82
        }).toThrowError('The field country should be a String (undefined)');
83
    });
84
85
    it('It should throw an exception', () => {
86
        expect(() => {
87
            Address.create({
88
                street: 'Abc',
89
                number: 42,
90
                postalCode: '1234AB',
91
                city: 'Example',
92
                country: '',
93
            });
94
        }).toThrowError('The field country should be a String ("")');
95
    });
96
97
    it('It should valid with this custom optional type', () => {
98
        const data = {
99
            name: 'test',
100
        };
101
        const test = Test.create(data);
102
        expect(test).toEqual({
103
            name: 'test',
104
        });
105
    });
106
107
    it('It should valid with this custom type', () => {
108
        const data = {
109
            name: 'test',
110
            test: test2,
111
            test3: { example: 'test' },
112
        };
113
        const test = Test.create(data);
114
        expect(test).toEqual({
115
            name: 'test',
116
            test: test2,
117
            test3: { example: 'test' },
118
        });
119
    });
120
121
    it('It should valid with this custom type and sub array', () => {
122
        const data = {
123
            name: 'test',
124
            test: test2,
125
            test3: [{ example: 'test' }],
126
        };
127
        const test = Test.create(data);
128
        expect(test).toEqual({
129
            name: 'test',
130
            test: test2,
131
            test3: [{ example: 'test' }],
132
        });
133
    });
134
135
    it('It should valid with this custom type and sub array v2', () => {
136
        const data = {
137
            name: 'test',
138
            test: test2,
139
            test5: [{ example: 'test', example2: 'test' }],
140
        };
141
        const test = Test.create(data);
142
        expect(test).toEqual({
143
            name: 'test',
144
            test: test2,
145
            test5: [{ example: 'test', example2: 'test' }],
146
        });
147
    });
148
149
    it('It should valid with this custom type and sub array v6', () => {
150
        const data = {
151
            name: 'test',
152
            test: test2,
153
            test6: { test7: [{ example: 'test' }] },
154
        };
155
        const test = Test.create(data);
156
        expect(test).toEqual({
157
            name: 'test',
158
            test: test2,
159
            test6: { test7: [{ example: 'test' }] },
160
        });
161
    });
162
163
    it('It should valid with a not existing key', () => {
164
        const data = {
165
            name: 'test',
166
            test: test2,
167
            test8: [{ example: 'test' }],
168
        };
169
        const test = Test.create(data);
170
        expect(test).toEqual({
171
            name: 'test',
172
            test: test2,
173
            test8: [{ example: 'test' }],
174
        });
175
    });
176
177
    it('It should valid with a string for test4', () => {
178
        const data = {
179
            name: 'test',
180
            test: test2,
181
            test4: 'ok',
182
        };
183
        const test = Test.create(data);
184
        expect(test).toEqual({
185
            name: 'test',
186
            test: test2,
187
            test4: 'ok',
188
        });
189
    });
190
191
    it('It should valid with a string for test4', () => {
192
        const data = {
193
            name: 'test',
194
            test: test2,
195
            test4: 42,
196
        };
197
        const test = Test.create(data);
198
        expect(test).toEqual({
199
            name: 'test',
200
            test: test2,
201
            test4: 42,
202
        });
203
    });
204
205
    it('It should throw an exception if test4 is invalid', () => {
206
        expect(() => {
207
            Test.create({
208
                name: 'test',
209
                test: test2,
210
                test4: true,
211
            });
212
        }).toThrowError('The field test4? should be a string|number (true)');
213
    });
214
215
    it('It should throw an exception if the custom type is invalid', () => {
216
        expect(() => {
217
            Test.create({
218
                name: 'test',
219
                test: 'test',
220
            });
221
        }).toThrowError('The field ?test should be a Test2 ("test")');
222
    });
223
224
    it('It should throw an exception if the sub schema is invalid', () => {
225
        expect(() => {
226
            Test.create({
227
                name: 'test',
228
                test3: 'test',
229
            });
230
        }).toThrowError(
231
            'The field test3?.example should be a String (undefined)'
232
        );
233
    });
234
235
    it('It should throw an exception if the custom type is invalid', () => {
236
        expect(() => {
237
            Test.create({
238
                name: 'test',
239
                obj: 'test',
240
            });
241
        }).toThrowError('The field obj? should be a Object ("test")');
242
    });
243
244
    it('It should throw an exception if the custom type is invalid', () => {
245
        expect(() => {
246
            Test.create({
247
                name: 'test',
248
                test3: [{ example: 42 }],
249
            });
250
        }).toThrowError('The field test3?.example should be a String (42)');
251
    });
252
253
    it('It should throw an exception if the custom type is invalid', () => {
254
        expect(() => {
255
            Test.create({
256
                name: 'test',
257
                test5: [{ example: 42 }],
258
            });
259
        }).toThrowError('The field ?test5.example should be a string (42)');
260
    });
261
});
262