Total Complexity | 5 |
Complexity/F | 1 |
Lines of Code | 97 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /* eslint-disable no-new */ |
||
2 | import { expect, describe, it } from '@jest/globals'; |
||
3 | import Parser from '../parser.js'; |
||
4 | |||
5 | const testSchema = { |
||
6 | a: Number, |
||
7 | b: Boolean, |
||
8 | 'c?': String, |
||
9 | }; |
||
10 | |||
11 | describe('Test parser.js', () => { |
||
12 | it('It should parse the values', () => { |
||
13 | const input = { |
||
14 | a: '42', |
||
15 | b: '1', |
||
16 | c: 42, |
||
17 | d: 'test', |
||
18 | }; |
||
19 | const parse = new Parser({ schema: testSchema }); |
||
20 | |||
21 | expect(parse.parseObject(input)).toEqual({ |
||
22 | a: 42, |
||
23 | b: true, |
||
24 | c: '42', |
||
25 | d: 'test', |
||
26 | }); |
||
27 | }); |
||
28 | |||
29 | it('It should parse the undefined values', () => { |
||
30 | const input = { |
||
31 | a: undefined, |
||
32 | b: undefined, |
||
33 | c: undefined, |
||
34 | d: undefined, |
||
35 | }; |
||
36 | const parse = new Parser({ schema: testSchema }); |
||
37 | |||
38 | expect(parse.parseObject(input)).toEqual({ |
||
39 | a: undefined, |
||
40 | b: undefined, |
||
41 | c: undefined, |
||
42 | d: undefined, |
||
43 | }); |
||
44 | }); |
||
45 | |||
46 | it('It should parse the nullish values', () => { |
||
47 | const input = { |
||
48 | a: null, |
||
49 | b: null, |
||
50 | c: null, |
||
51 | d: null, |
||
52 | }; |
||
53 | const parse = new Parser({ schema: testSchema }); |
||
54 | |||
55 | expect(parse.parseObject(input)).toEqual({ |
||
56 | a: null, |
||
57 | b: null, |
||
58 | c: null, |
||
59 | d: null, |
||
60 | }); |
||
61 | }); |
||
62 | |||
63 | it.each([ |
||
64 | { a: 'y', b: true }, |
||
65 | { a: 'yes', b: true }, |
||
66 | { a: 'Y', b: true }, |
||
67 | { a: 'YES', b: true }, |
||
68 | { a: 'true', b: true }, |
||
69 | { a: 'on', b: true }, |
||
70 | { a: 1, b: true }, |
||
71 | { a: true, b: true }, |
||
72 | { a: '1', b: true }, |
||
73 | { a: 'n', b: false }, |
||
74 | { a: 'bla', b: false }, |
||
75 | { a: 'false', b: false }, |
||
76 | { a: '0', b: false }, |
||
77 | { a: 0, b: false }, |
||
78 | { a: 2, b: false }, |
||
79 | { a: -1, b: false }, |
||
80 | { a: {}, b: false }, |
||
81 | { a: [], b: false }, |
||
82 | ])('It should parse the boolean values $a -> $b', ({ a, b }) => { |
||
83 | const input = { |
||
84 | a: null, |
||
85 | b: a, |
||
86 | c: null, |
||
87 | d: null, |
||
88 | }; |
||
89 | const parse = new Parser({ schema: testSchema }); |
||
90 | |||
91 | expect(parse.parseObject(input)).toEqual({ |
||
92 | a: null, |
||
93 | b, |
||
94 | c: null, |
||
95 | d: null, |
||
96 | }); |
||
97 | }); |
||
98 | }); |
||
99 |