File tree 2 files changed +38
-2
lines changed
2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change @@ -321,6 +321,42 @@ describe('component props', () => {
321
321
expect ( `Missing required prop: "num"` ) . toHaveBeenWarned ( )
322
322
} )
323
323
324
+ test ( 'warn on type mismatch' , ( ) => {
325
+ class MyClass {
326
+
327
+ }
328
+ const Comp = {
329
+ props : {
330
+ bool : { type : Boolean } ,
331
+ str : { type : String } ,
332
+ num : { type : Number } ,
333
+ arr : { type : Array } ,
334
+ obj : { type : Object } ,
335
+ cls : { type : MyClass } ,
336
+ fn : { type : Function } ,
337
+ } ,
338
+ setup ( ) {
339
+ return ( ) => null
340
+ }
341
+ }
342
+ render ( h ( Comp , {
343
+ bool : 'true' ,
344
+ str : 100 ,
345
+ num : '100' ,
346
+ arr : { } ,
347
+ obj : 'false' ,
348
+ cls : { } ,
349
+ fn : true ,
350
+ } ) , nodeOps . createElement ( 'div' ) )
351
+ expect ( `Invalid prop: type check failed for prop "bool". Expected Boolean, got String` ) . toHaveBeenWarned ( )
352
+ expect ( `Invalid prop: type check failed for prop "str". Expected String with value "100", got Number with value 100.` ) . toHaveBeenWarned ( )
353
+ expect ( `Invalid prop: type check failed for prop "num". Expected Number with value 100, got String with value "100".` ) . toHaveBeenWarned ( )
354
+ expect ( `Invalid prop: type check failed for prop "arr". Expected Array, got Object` ) . toHaveBeenWarned ( )
355
+ expect ( `Invalid prop: type check failed for prop "obj". Expected Object, got String with value "false"` ) . toHaveBeenWarned ( )
356
+ expect ( `Invalid prop: type check failed for prop "fn". Expected Function, got Boolean with value true.` ) . toHaveBeenWarned ( )
357
+ expect ( `Invalid prop: type check failed for prop "cls". Expected MyClass, got Object` ) . toHaveBeenWarned ( )
358
+ } )
359
+
324
360
// #3495
325
361
test ( 'should not warn required props using kebab-case' , async ( ) => {
326
362
const Comp = {
Original file line number Diff line number Diff line change @@ -557,8 +557,8 @@ function validatePropName(key: string) {
557
557
// use function string name to check type constructors
558
558
// so that it works across vms / iframes.
559
559
function getType ( ctor : Prop < any > ) : string {
560
- const match = ctor && ctor . toString ( ) . match ( / ^ \s * f u n c t i o n ( \w + ) / )
561
- return match ? match [ 1 ] : ctor === null ? 'null' : ''
560
+ const match = ctor && ctor . toString ( ) . match ( / ^ \s * ( f u n c t i o n | c l a s s ) ( \w + ) / )
561
+ return match ? match [ 2 ] : ctor === null ? 'null' : ''
562
562
}
563
563
564
564
function isSameType ( a : Prop < any > , b : Prop < any > ) : boolean {
You can’t perform that action at this time.
0 commit comments