@@ -226,5 +226,123 @@ if (_.inBrowser) {
226
226
expect ( el . innerHTML ) . toBe ( '<p>AAA</p><p>DDD</p>' )
227
227
} )
228
228
229
+ describe ( 'assertions' , function ( ) {
230
+
231
+ function makeInstance ( value , type , validator ) {
232
+ return new Vue ( {
233
+ el : document . createElement ( 'div' ) ,
234
+ template : '<test prop="{{val}}"></test>' ,
235
+ data : {
236
+ val : value
237
+ } ,
238
+ components : {
239
+ test : {
240
+ props : [
241
+ {
242
+ name : 'prop' ,
243
+ type : type ,
244
+ validator : validator
245
+ }
246
+ ]
247
+ }
248
+ }
249
+ } )
250
+ }
251
+
252
+ it ( 'string' , function ( ) {
253
+ makeInstance ( 'hello' , String )
254
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
255
+ makeInstance ( 123 , String )
256
+ expect ( hasWarned ( _ , 'Expected String' ) ) . toBe ( true )
257
+ } )
258
+
259
+ it ( 'number' , function ( ) {
260
+ makeInstance ( 123 , Number )
261
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
262
+ makeInstance ( '123' , Number )
263
+ expect ( hasWarned ( _ , 'Expected Number' ) ) . toBe ( true )
264
+ } )
265
+
266
+ it ( 'boolean' , function ( ) {
267
+ makeInstance ( true , Boolean )
268
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
269
+ makeInstance ( '123' , Boolean )
270
+ expect ( hasWarned ( _ , 'Expected Boolean' ) ) . toBe ( true )
271
+ } )
272
+
273
+ it ( 'function' , function ( ) {
274
+ makeInstance ( function ( ) { } , Function )
275
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
276
+ makeInstance ( 123 , Function )
277
+ expect ( hasWarned ( _ , 'Expected Function' ) ) . toBe ( true )
278
+ } )
279
+
280
+ it ( 'object' , function ( ) {
281
+ makeInstance ( { } , Object )
282
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
283
+ makeInstance ( [ ] , Object )
284
+ expect ( hasWarned ( _ , 'Expected Object' ) ) . toBe ( true )
285
+ } )
286
+
287
+ it ( 'array' , function ( ) {
288
+ makeInstance ( [ ] , Array )
289
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
290
+ makeInstance ( { } , Array )
291
+ expect ( hasWarned ( _ , 'Expected Array' ) ) . toBe ( true )
292
+ } )
293
+
294
+ it ( 'custom constructor' , function ( ) {
295
+ function Class ( ) { }
296
+ makeInstance ( new Class ( ) , Class )
297
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
298
+ makeInstance ( { } , Class )
299
+ expect ( hasWarned ( _ , 'Expected custom type' ) ) . toBe ( true )
300
+ } )
301
+
302
+ it ( 'custom validator' , function ( ) {
303
+ makeInstance ( 123 , null , function ( v ) {
304
+ return v === 123
305
+ } )
306
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
307
+ makeInstance ( 123 , null , function ( v ) {
308
+ return v === 234
309
+ } )
310
+ expect ( hasWarned ( _ , 'custom validator check failed' ) ) . toBe ( true )
311
+ } )
312
+
313
+ it ( 'type check + custom validator' , function ( ) {
314
+ makeInstance ( 123 , Number , function ( v ) {
315
+ return v === 123
316
+ } )
317
+ expect ( _ . warn ) . not . toHaveBeenCalled ( )
318
+ makeInstance ( 123 , Number , function ( v ) {
319
+ return v === 234
320
+ } )
321
+ expect ( hasWarned ( _ , 'custom validator check failed' ) ) . toBe ( true )
322
+ makeInstance ( 123 , String , function ( v ) {
323
+ return v === 123
324
+ } )
325
+ expect ( hasWarned ( _ , 'Expected String' ) ) . toBe ( true )
326
+ } )
327
+
328
+ it ( 'required' , function ( ) {
329
+ var vm = new Vue ( {
330
+ el : document . createElement ( 'div' ) ,
331
+ template : '<test></test>' ,
332
+ components : {
333
+ test : {
334
+ props : [
335
+ {
336
+ name : 'prop' ,
337
+ required : true
338
+ }
339
+ ]
340
+ }
341
+ }
342
+ } )
343
+ expect ( hasWarned ( _ , 'Missing required prop' ) ) . toBe ( true )
344
+ } )
345
+
346
+ } )
229
347
} )
230
348
}
0 commit comments