1
1
'use strict'
2
2
3
- const { countNestedProperties, assertObjectMatchSpec } = require ( '../../src/core/assertions' )
3
+ const {
4
+ countNestedProperties,
5
+ assertObjectMatchSpec,
6
+ buildObjectKeysArray,
7
+ } = require ( '../../src/core/assertions' )
4
8
5
9
beforeAll ( ( ) => {
6
10
const MockDate = ( lastDate ) => ( ) => new lastDate ( 2018 , 4 , 1 )
@@ -13,17 +17,17 @@ afterAll(() => {
13
17
14
18
beforeEach ( ( ) => { } )
15
19
16
- test ( 'should allow to count object properties' , ( ) => {
20
+ test ( 'should allow to build an array of object properties' , ( ) => {
17
21
expect (
18
- countNestedProperties ( {
22
+ buildObjectKeysArray ( {
19
23
a : true ,
20
24
b : true ,
21
25
c : true ,
22
26
} )
23
- ) . toBe ( 3 )
27
+ ) . toStrictEqual ( [ 'a' , 'b' , 'c' ] )
24
28
25
29
expect (
26
- countNestedProperties ( {
30
+ buildObjectKeysArray ( {
27
31
a : true ,
28
32
b : true ,
29
33
c : true ,
@@ -32,53 +36,67 @@ test('should allow to count object properties', () => {
32
36
b : true ,
33
37
} ,
34
38
} )
35
- ) . toBe ( 5 )
39
+ ) . toStrictEqual ( [ 'a' , 'b' , 'c' , 'd.a' , 'd.b' ] )
36
40
} )
37
41
38
- test ( 'should allow to count nested objects properties' , ( ) => {
42
+ test ( 'should allow to build an array of object propertieswith nested objects properties' , ( ) => {
39
43
expect (
40
- countNestedProperties ( {
44
+ buildObjectKeysArray ( {
41
45
a : true ,
42
46
b : true ,
43
47
c : {
44
48
d : 'value1' ,
45
49
e : 'value2' ,
46
50
} ,
47
51
} )
48
- ) . toBe ( 4 )
52
+ ) . toStrictEqual ( [ 'a' , 'b' , 'c.d' , 'c.e' ] )
49
53
} )
50
54
51
- test ( 'should allow to count object properties with null, undefined properties ' , ( ) => {
55
+ test ( 'should allow to build an array of object properties with null, undefined properties ' , ( ) => {
52
56
expect (
53
- countNestedProperties ( {
57
+ buildObjectKeysArray ( {
54
58
a : null ,
55
59
b : undefined ,
56
60
c : 'value3' ,
57
61
} )
58
- ) . toBe ( 3 )
62
+ ) . toStrictEqual ( [ 'a' , 'b' , 'c' ] )
59
63
} )
60
64
61
- test ( 'should allow to count object with properties array property' , ( ) => {
65
+ test ( 'should allow to build an array of object properties with properties array property' , ( ) => {
62
66
expect (
63
- countNestedProperties ( {
67
+ buildObjectKeysArray ( {
64
68
a : [ 1 , 2 ] ,
65
69
b : true ,
66
70
c : true ,
67
71
} )
68
- ) . toBe ( 4 )
72
+ ) . toStrictEqual ( [ 'a.0' , 'a.1' , 'b' , 'c' ] )
69
73
} )
70
74
71
- test ( 'should allow to count object properties with empty array property' , ( ) => {
75
+ test ( 'should allow to build an array of object properties with empty array property' , ( ) => {
72
76
expect (
73
- countNestedProperties ( {
77
+ buildObjectKeysArray ( {
74
78
a : true ,
75
79
b : true ,
76
80
c : {
77
81
d : '' ,
78
82
e : [ ] ,
79
83
} ,
80
84
} )
81
- ) . toBe ( 4 )
85
+ ) . toStrictEqual ( [ 'a' , 'b' , 'c.d' , 'c.e' ] )
86
+ } )
87
+
88
+ test ( 'should allow to build an array of object properties from nested object' , ( ) => {
89
+ expect (
90
+ buildObjectKeysArray ( {
91
+ a : true ,
92
+ b : {
93
+ b1 : true ,
94
+ b2 : true ,
95
+ b3 : { } ,
96
+ } ,
97
+ c : true ,
98
+ } )
99
+ ) . toStrictEqual ( [ 'a' , 'b.b1' , 'b.b2' , 'b.b3' , 'c' ] )
82
100
} )
83
101
84
102
test ( 'object property is defined' , ( ) => {
@@ -140,7 +158,12 @@ test('object property is not defined', () => {
140
158
)
141
159
expect ( ( ) =>
142
160
assertObjectMatchSpec (
143
- { name : 'john' , gender : 'male' , city : 'paris' , street : 'rue du chat qui pêche' } ,
161
+ {
162
+ name : 'john' ,
163
+ gender : 'male' ,
164
+ city : 'paris' ,
165
+ street : 'rue du chat qui pêche' ,
166
+ } ,
144
167
spec
145
168
)
146
169
) . toThrow ( `Property 'name' is defined: expected 'john' to be undefined` )
@@ -227,35 +250,60 @@ test('check object property does not contain value', () => {
227
250
228
251
expect ( ( ) =>
229
252
assertObjectMatchSpec (
230
- { first_name : 'foo' , last_name : 'bar' , city : 'miami' , street : 'calle ocho' } ,
253
+ {
254
+ first_name : 'foo' ,
255
+ last_name : 'bar' ,
256
+ city : 'miami' ,
257
+ street : 'calle ocho' ,
258
+ } ,
231
259
spec
232
260
)
233
261
) . not . toThrow ( )
234
262
expect ( ( ) =>
235
263
assertObjectMatchSpec (
236
- { first_name : 'johnny' , last_name : 'bar' , city : 'miami' , street : 'calle ocho' } ,
264
+ {
265
+ first_name : 'johnny' ,
266
+ last_name : 'bar' ,
267
+ city : 'miami' ,
268
+ street : 'calle ocho' ,
269
+ } ,
237
270
spec
238
271
)
239
272
) . toThrow (
240
273
`Property 'first_name' (johnny) contains 'john': expected 'johnny' to not include 'john'`
241
274
)
242
275
expect ( ( ) =>
243
276
assertObjectMatchSpec (
244
- { first_name : 'foo' , last_name : 'doet' , city : 'miami' , street : 'calle ocho' } ,
277
+ {
278
+ first_name : 'foo' ,
279
+ last_name : 'doet' ,
280
+ city : 'miami' ,
281
+ street : 'calle ocho' ,
282
+ } ,
245
283
spec
246
284
)
247
285
) . toThrow ( `Property 'last_name' (doet) contains 'doe': expected 'doet' to not include 'doe'` )
248
286
expect ( ( ) =>
249
287
assertObjectMatchSpec (
250
- { first_name : 'foo' , last_name : 'bar' , city : 'new york' , street : 'calle ocho' } ,
288
+ {
289
+ first_name : 'foo' ,
290
+ last_name : 'bar' ,
291
+ city : 'new york' ,
292
+ street : 'calle ocho' ,
293
+ } ,
251
294
spec
252
295
)
253
296
) . toThrow (
254
297
`Property 'city' (new york) contains 'york': expected 'new york' to not include 'york'`
255
298
)
256
299
expect ( ( ) =>
257
300
assertObjectMatchSpec (
258
- { first_name : 'foo' , last_name : 'bar' , city : 'miami' , street : 'krome avenue' } ,
301
+ {
302
+ first_name : 'foo' ,
303
+ last_name : 'bar' ,
304
+ city : 'miami' ,
305
+ street : 'krome avenue' ,
306
+ } ,
259
307
spec
260
308
)
261
309
) . toThrow (
@@ -303,36 +351,6 @@ test('check object property does not match regexp', () => {
303
351
)
304
352
} )
305
353
306
- test ( 'check object fully matches spec' , ( ) => {
307
- const spec = [
308
- {
309
- field : 'first_name' ,
310
- matcher : 'equal' ,
311
- value : 'john' ,
312
- } ,
313
- {
314
- field : 'last_name' ,
315
- matcher : 'match' ,
316
- value : '^doe' ,
317
- } ,
318
- ]
319
-
320
- expect ( ( ) =>
321
- assertObjectMatchSpec ( { first_name : 'john' , last_name : 'doet' } , spec , true )
322
- ) . not . toThrow ( )
323
- expect ( ( ) =>
324
- assertObjectMatchSpec ( { first_name : 'john' , last_name : 'doet' , gender : 'male' } , spec , true )
325
- ) . toThrow ( `Expected json response to fully match spec, but it does not: expected 3 to equal 2` )
326
- expect ( ( ) =>
327
- assertObjectMatchSpec ( { first_name : 'john' , last_name : 'john' } , spec , true )
328
- ) . toThrow ( `Property 'last_name' (john) does not match '^doe': expected 'john' to match /^doe/` )
329
- expect ( ( ) =>
330
- assertObjectMatchSpec ( { first_name : 'doe' , last_name : 'doe' } , spec , true )
331
- ) . toThrow (
332
- `Expected property 'first_name' to equal 'john', but found 'doe': expected 'doe' to deeply equal 'john'`
333
- )
334
- } )
335
-
336
354
test ( 'check object property type' , ( ) => {
337
355
const spec = [
338
356
{
@@ -531,3 +549,69 @@ test('check unsupported matcher should fail', () => {
531
549
`Matcher "unknown" did not match any supported assertions`
532
550
)
533
551
} )
552
+
553
+ test ( 'check object fully matches spec' , ( ) => {
554
+ const spec = [
555
+ {
556
+ field : 'first_name' ,
557
+ matcher : 'equal' ,
558
+ value : 'john' ,
559
+ } ,
560
+ {
561
+ field : 'last_name' ,
562
+ matcher : 'match' ,
563
+ value : '^doe' ,
564
+ } ,
565
+ {
566
+ field : 'address' ,
567
+ matcher : 'type' ,
568
+ value : 'object' ,
569
+ } ,
570
+ {
571
+ field : 'phone.mobile' ,
572
+ matcher : 'match' ,
573
+ value : '^06' ,
574
+ } ,
575
+ {
576
+ field : 'phone.mobile' ,
577
+ matcher : 'type' ,
578
+ value : 'string' ,
579
+ } ,
580
+ ]
581
+
582
+ expect ( ( ) =>
583
+ assertObjectMatchSpec (
584
+ {
585
+ first_name : 'john' ,
586
+ last_name : 'doet' ,
587
+ address : { } ,
588
+ phone : { mobile : '0600000000' } ,
589
+ } ,
590
+ spec ,
591
+ true
592
+ )
593
+ ) . not . toThrow ( )
594
+ expect ( ( ) =>
595
+ assertObjectMatchSpec (
596
+ {
597
+ first_name : 'john' ,
598
+ last_name : 'doet' ,
599
+ gender : 'male' ,
600
+ address : { } ,
601
+ phone : { mobile : '0600000000' } ,
602
+ } ,
603
+ spec ,
604
+ true
605
+ )
606
+ ) . toThrow (
607
+ `Expected json response to fully match spec, but it does not: expected [ Array(5) ] to deeply equal [ Array(4) ]`
608
+ )
609
+ expect ( ( ) =>
610
+ assertObjectMatchSpec ( { first_name : 'john' , last_name : 'john' } , spec , true )
611
+ ) . toThrow ( `Property 'last_name' (john) does not match '^doe': expected 'john' to match /^doe/` )
612
+ expect ( ( ) =>
613
+ assertObjectMatchSpec ( { first_name : 'doe' , last_name : 'doe' } , spec , true )
614
+ ) . toThrow (
615
+ `Expected property 'first_name' to equal 'john', but found 'doe': expected 'doe' to deeply equal 'john'`
616
+ )
617
+ } )
0 commit comments