@@ -232,7 +232,7 @@ describe('component props (vapor)', () => {
232
232
expect ( props . bar ) . toEqual ( { a : 1 } )
233
233
expect ( props . baz ) . toEqual ( defaultBaz )
234
234
// expect(defaultFn).toHaveBeenCalledTimes(1) // failed: (caching is not supported)
235
- expect ( defaultFn ) . toHaveBeenCalledTimes ( 2 )
235
+ expect ( defaultFn ) . toHaveBeenCalledTimes ( 3 )
236
236
expect ( defaultBaz ) . toHaveBeenCalledTimes ( 0 )
237
237
238
238
// #999: updates should not cause default factory of unchanged prop to be
@@ -358,25 +358,138 @@ describe('component props (vapor)', () => {
358
358
reset ( )
359
359
} )
360
360
361
- test . todo ( 'validator' , ( ) => {
362
- // TODO: impl validator
361
+ describe ( 'validator' , ( ) => {
362
+ test ( 'validator should be called with two arguments' , ( ) => {
363
+ let args : any
364
+ const mockFn = vi . fn ( ( ..._args : any [ ] ) => {
365
+ args = _args
366
+ return true
367
+ } )
368
+
369
+ const Comp = defineComponent ( {
370
+ props : {
371
+ foo : {
372
+ type : Number ,
373
+ validator : ( value : any , props : any ) => mockFn ( value , props ) ,
374
+ } ,
375
+ bar : {
376
+ type : Number ,
377
+ } ,
378
+ } ,
379
+ render ( ) {
380
+ const t0 = template ( '<div/>' )
381
+ const n0 = t0 ( )
382
+ return n0
383
+ } ,
384
+ } )
385
+
386
+ const props = {
387
+ get foo ( ) {
388
+ return 1
389
+ } ,
390
+ get bar ( ) {
391
+ return 2
392
+ } ,
393
+ }
394
+
395
+ render ( Comp , props , host )
396
+ expect ( mockFn ) . toHaveBeenCalled ( )
397
+ // NOTE: Vapor Component props defined by getter. So, `props` not Equal to `{ foo: 1, bar: 2 }`
398
+ // expect(mockFn).toHaveBeenCalledWith(1, { foo: 1, bar: 2 })
399
+ expect ( args . length ) . toBe ( 2 )
400
+ expect ( args [ 0 ] ) . toBe ( 1 )
401
+ expect ( args [ 1 ] . foo ) . toEqual ( 1 )
402
+ expect ( args [ 1 ] . bar ) . toEqual ( 2 )
403
+ } )
404
+
405
+ // TODO: impl setter and warnner
406
+ test . todo (
407
+ 'validator should not be able to mutate other props' ,
408
+ async ( ) => {
409
+ const mockFn = vi . fn ( ( ...args : any [ ] ) => true )
410
+ const Comp = defineComponent ( {
411
+ props : {
412
+ foo : {
413
+ type : Number ,
414
+ validator : ( value : any , props : any ) => ! ! ( props . bar = 1 ) ,
415
+ } ,
416
+ bar : {
417
+ type : Number ,
418
+ validator : ( value : any ) => mockFn ( value ) ,
419
+ } ,
420
+ } ,
421
+ render ( ) {
422
+ const t0 = template ( '<div/>' )
423
+ const n0 = t0 ( )
424
+ return n0
425
+ } ,
426
+ } )
427
+
428
+ render (
429
+ Comp ,
430
+ {
431
+ get foo ( ) {
432
+ return 1
433
+ } ,
434
+ get bar ( ) {
435
+ return 2
436
+ } ,
437
+ } ,
438
+ host ,
439
+ )
440
+ expect (
441
+ `Set operation on key "bar" failed: target is readonly.` ,
442
+ ) . toHaveBeenWarnedLast ( )
443
+ expect ( mockFn ) . toHaveBeenCalledWith ( 2 )
444
+ } ,
445
+ )
363
446
} )
364
447
365
448
test . todo ( 'warn props mutation' , ( ) => {
366
449
// TODO: impl warn
367
450
} )
368
451
369
- test . todo ( 'warn absent required props' , ( ) => {
370
- // TODO: impl warn
452
+ test ( 'warn absent required props' , ( ) => {
453
+ const Comp = defineComponent ( {
454
+ props : {
455
+ bool : { type : Boolean , required : true } ,
456
+ str : { type : String , required : true } ,
457
+ num : { type : Number , required : true } ,
458
+ } ,
459
+ setup ( ) {
460
+ return ( ) => null
461
+ } ,
462
+ } )
463
+ render ( Comp , { } , host )
464
+ expect ( `Missing required prop: "bool"` ) . toHaveBeenWarned ( )
465
+ expect ( `Missing required prop: "str"` ) . toHaveBeenWarned ( )
466
+ expect ( `Missing required prop: "num"` ) . toHaveBeenWarned ( )
371
467
} )
372
468
373
- test . todo ( 'warn on type mismatch' , ( ) => {
374
- // TODO: impl warn
375
- } )
469
+ // NOTE: type check is not supported in vapor
470
+ // test('warn on type mismatch', () => {})
376
471
377
472
// #3495
378
- test . todo ( 'should not warn required props using kebab-case' , async ( ) => {
379
- // TODO: impl warn
473
+ test ( 'should not warn required props using kebab-case' , async ( ) => {
474
+ const Comp = defineComponent ( {
475
+ props : {
476
+ fooBar : { type : String , required : true } ,
477
+ } ,
478
+ setup ( ) {
479
+ return ( ) => null
480
+ } ,
481
+ } )
482
+
483
+ render (
484
+ Comp ,
485
+ {
486
+ get [ 'foo-bar' ] ( ) {
487
+ return 'hello'
488
+ } ,
489
+ } ,
490
+ host ,
491
+ )
492
+ expect ( `Missing required prop: "fooBar"` ) . not . toHaveBeenWarned ( )
380
493
} )
381
494
382
495
test ( 'props type support BigInt' , ( ) => {
0 commit comments