@@ -12,6 +12,9 @@ const checkRequired = (type) => {
12
12
} )
13
13
}
14
14
15
+ // Vue.js does keep the context for validators, so there is no `this`
16
+ const forceNoContext = ( validator ) => validator . bind ( undefined )
17
+
15
18
describe ( 'VuePropTypes' , ( ) => {
16
19
17
20
describe ( '`.any`' , ( ) => {
@@ -194,9 +197,10 @@ describe('VuePropTypes', () => {
194
197
} )
195
198
196
199
it ( 'should provide a validator function that returns true on integer values' , ( ) => {
197
- expect ( VueTypes . integer . validator ( 100 ) ) . toBe ( true )
198
- expect ( VueTypes . integer . validator ( Infinity ) ) . toBe ( false )
199
- expect ( VueTypes . integer . validator ( 0.1 ) ) . toBe ( false )
200
+ const validator = forceNoContext ( VueTypes . integer . validator )
201
+ expect ( validator ( 100 ) ) . toBe ( true )
202
+ expect ( validator ( Infinity ) ) . toBe ( false )
203
+ expect ( validator ( 0.1 ) ) . toBe ( false )
200
204
} )
201
205
202
206
} )
@@ -226,8 +230,9 @@ describe('VuePropTypes', () => {
226
230
} )
227
231
228
232
it ( 'should provide a custom validator function' , ( ) => {
229
- expect ( customType . validator ( 'mytest' ) ) . toBe ( true )
230
- expect ( customType . validator ( 0 ) ) . toBe ( false )
233
+ const validator = forceNoContext ( customType . validator )
234
+ expect ( validator ( 'mytest' ) ) . toBe ( true )
235
+ expect ( validator ( 0 ) ) . toBe ( false )
231
236
} )
232
237
233
238
} )
@@ -266,8 +271,9 @@ describe('VuePropTypes', () => {
266
271
} )
267
272
268
273
it ( 'should provide a custom validator function' , ( ) => {
269
- expect ( customType . validator ( 0 ) ) . toBe ( true )
270
- expect ( customType . validator ( 5 ) ) . toBe ( false )
274
+ const validator = forceNoContext ( customType . validator )
275
+ expect ( validator ( 0 ) ) . toBe ( true )
276
+ expect ( validator ( 5 ) ) . toBe ( false )
271
277
} )
272
278
273
279
it ( 'should filter `null` values type checking' , ( ) => {
@@ -343,24 +349,25 @@ describe('VuePropTypes', () => {
343
349
344
350
it ( 'should validate an array of same-type values' , ( ) => {
345
351
const customType = VueTypes . arrayOf ( Number )
346
- expect ( customType . validator ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
352
+ expect ( forceNoContext ( customType . validator ) ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
347
353
348
354
} )
349
355
350
356
it ( 'should NOT validate an array of mixed-type values' , ( ) => {
351
357
const customType = VueTypes . arrayOf ( Number )
352
- expect ( customType . validator ( [ 0 , 1 , 'string' ] ) ) . toBe ( false )
358
+ expect ( forceNoContext ( customType . validator ) ( [ 0 , 1 , 'string' ] ) ) . toBe ( false )
353
359
} )
354
360
355
361
it ( 'should allow validation of VuePropTypes native types' , ( ) => {
356
362
const customType = VueTypes . arrayOf ( VueTypes . number )
357
- expect ( customType . validator ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
363
+ expect ( forceNoContext ( customType . validator ) ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
358
364
} )
359
365
360
366
it ( 'should allow validation of VuePropTypes custom types' , ( ) => {
361
367
const customType = VueTypes . arrayOf ( VueTypes . integer )
362
- expect ( customType . validator ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
363
- expect ( customType . validator ( [ 0 , 1.2 , 2 ] ) ) . toBe ( false )
368
+ const validator = forceNoContext ( customType . validator )
369
+ expect ( validator ( [ 0 , 1 , 2 ] ) ) . toBe ( true )
370
+ expect ( validator ( [ 0 , 1.2 , 2 ] ) ) . toBe ( false )
364
371
} )
365
372
366
373
} )
@@ -391,24 +398,25 @@ describe('VuePropTypes', () => {
391
398
392
399
it ( 'should validate an object of same-type values' , ( ) => {
393
400
const customType = VueTypes . objectOf ( Number )
394
- expect ( customType . validator ( { id : 10 , age : 30 } ) ) . toBe ( true )
401
+ expect ( forceNoContext ( customType . validator ) ( { id : 10 , age : 30 } ) ) . toBe ( true )
395
402
396
403
} )
397
404
398
405
it ( 'should NOT validate an array of mixed-type values' , ( ) => {
399
406
const customType = VueTypes . objectOf ( Number )
400
- expect ( customType . validator ( { id : '10' , age : 30 } ) ) . toBe ( false )
407
+ expect ( forceNoContext ( customType . validator ) ( { id : '10' , age : 30 } ) ) . toBe ( false )
401
408
} )
402
409
403
410
it ( 'should allow validation of VuePropTypes native types' , ( ) => {
404
411
const customType = VueTypes . objectOf ( VueTypes . number )
405
- expect ( customType . validator ( { id : 10 , age : 30 } ) ) . toBe ( true )
412
+ expect ( forceNoContext ( customType . validator ) ( { id : 10 , age : 30 } ) ) . toBe ( true )
406
413
} )
407
414
408
415
it ( 'should allow validation of VuePropTypes custom types' , ( ) => {
409
416
const customType = VueTypes . objectOf ( VueTypes . integer )
410
- expect ( customType . validator ( { id : 10 , age : 30 } ) ) . toBe ( true )
411
- expect ( customType . validator ( { id : 10.2 , age : 30 } ) ) . toBe ( false )
417
+ const validator = forceNoContext ( customType . validator )
418
+ expect ( validator ( { id : 10 , age : 30 } ) ) . toBe ( true )
419
+ expect ( validator ( { id : 10.2 , age : 30 } ) ) . toBe ( false )
412
420
} )
413
421
414
422
} )
@@ -433,7 +441,7 @@ describe('VuePropTypes', () => {
433
441
it ( 'should validate an object with a given shape' , ( ) => {
434
442
435
443
const customType = VueTypes . shape ( shape )
436
- expect ( customType . validator ( {
444
+ expect ( forceNoContext ( customType . validator ) ( {
437
445
id : 10 ,
438
446
name : 'John' ,
439
447
age : 30
@@ -443,7 +451,7 @@ describe('VuePropTypes', () => {
443
451
it ( 'should NOT validate an object without a given shape' , ( ) => {
444
452
445
453
const customType = VueTypes . shape ( shape )
446
- expect ( customType . validator ( {
454
+ expect ( forceNoContext ( customType . validator ) ( {
447
455
id : '10' ,
448
456
name : 'John' ,
449
457
age : 30
@@ -453,7 +461,7 @@ describe('VuePropTypes', () => {
453
461
it ( 'should NOT validate an object with keys NOT present in the shape' , ( ) => {
454
462
455
463
const customType = VueTypes . shape ( shape )
456
- expect ( customType . validator ( {
464
+ expect ( forceNoContext ( customType . validator ) ( {
457
465
id : 10 ,
458
466
name : 'John' ,
459
467
age : 30 ,
@@ -464,7 +472,7 @@ describe('VuePropTypes', () => {
464
472
it ( 'should validate an object with keys NOT present in the shape on `loose` mode' , ( ) => {
465
473
466
474
const customType = VueTypes . shape ( shape ) . loose
467
- expect ( customType . validator ( {
475
+ expect ( forceNoContext ( customType . validator ) ( {
468
476
id : 10 ,
469
477
name : 'John' ,
470
478
age : 30 ,
@@ -474,7 +482,8 @@ describe('VuePropTypes', () => {
474
482
475
483
it ( 'should NOT validate a value which is NOT an object' , ( ) => {
476
484
const customType = VueTypes . shape ( shape )
477
- expect ( customType . validator ( 'a string' ) ) . toBe ( false )
485
+ const validator = forceNoContext ( customType . validator )
486
+ expect ( validator ( 'a string' ) ) . toBe ( false )
478
487
479
488
class MyClass {
480
489
constructor ( ) {
@@ -484,7 +493,7 @@ describe('VuePropTypes', () => {
484
493
}
485
494
}
486
495
487
- expect ( customType . validator ( new MyClass ( ) ) ) . toBe ( false )
496
+ expect ( validator ( new MyClass ( ) ) ) . toBe ( false )
488
497
} )
489
498
490
499
it ( 'should provide a method to set a custom default' , ( ) => {
@@ -515,12 +524,13 @@ describe('VuePropTypes', () => {
515
524
id : VueTypes . integer . isRequired ,
516
525
name : String
517
526
} )
527
+ const validator = forceNoContext ( customType . validator )
518
528
519
- expect ( customType . validator ( {
529
+ expect ( validator ( {
520
530
name : 'John'
521
531
} ) ) . toBe ( false )
522
532
523
- expect ( customType . validator ( {
533
+ expect ( validator ( {
524
534
id : 10
525
535
} ) ) . toBe ( true )
526
536
} )
@@ -531,12 +541,13 @@ describe('VuePropTypes', () => {
531
541
myKey : VueTypes . any . isRequired ,
532
542
name : null
533
543
} )
544
+ const validator = forceNoContext ( customType . validator )
534
545
535
- expect ( customType . validator ( {
546
+ expect ( validator ( {
536
547
name : 'John'
537
548
} ) ) . toBe ( false )
538
549
539
- expect ( customType . validator ( {
550
+ expect ( validator ( {
540
551
myKey : null
541
552
} ) ) . toBe ( true )
542
553
@@ -602,14 +613,15 @@ describe('VuePropTypes', () => {
602
613
603
614
it ( 'should validate custom types with complex shapes' , ( ) => {
604
615
const customType = VueTypes . oneOfType ( complexTypes )
616
+ const validator = forceNoContext ( customType . validator )
605
617
606
- expect ( customType . validator ( 1 ) ) . toBe ( true )
618
+ expect ( validator ( 1 ) ) . toBe ( true )
607
619
608
620
// validates types not values!
609
- expect ( customType . validator ( 5 ) ) . toBe ( true )
621
+ expect ( validator ( 5 ) ) . toBe ( true )
610
622
611
- expect ( customType . validator ( { id : 10 } ) ) . toBe ( true )
612
- expect ( customType . validator ( { id : '10' } ) ) . toBe ( false )
623
+ expect ( validator ( { id : 10 } ) ) . toBe ( true )
624
+ expect ( validator ( { id : '10' } ) ) . toBe ( false )
613
625
614
626
} )
615
627
0 commit comments