1
1
import packageJson from '../package.json'
2
- import Vue from 'vue'
2
+ import Vue from 'vue/dist/vue.js '
3
3
4
4
const VueTextMask = ( isVerify ( ) ) ?
5
5
require ( `../${ packageJson . main } ` ) :
@@ -17,6 +17,24 @@ function mountComponent(Component, propsData) {
17
17
return new Ctor ( { propsData} ) . $mount ( )
18
18
}
19
19
20
+ const eventTest = Vue . extend ( {
21
+ template : `<div>
22
+ <masked-input
23
+ ref="maskedInput"
24
+ type="text"
25
+ name="test"
26
+ :mask="[/\d/,/\d/,/\d/]"
27
+ @focus="callback('focus')"
28
+ @blur="callback('blur')"
29
+ @keypress="callback('keypress')">
30
+ </masked-input>
31
+ </div>` ,
32
+ components : { maskedInput} ,
33
+ methods : {
34
+ callback ( e ) { } ,
35
+ } ,
36
+ } )
37
+
20
38
describe ( 'inputMask' , ( ) => {
21
39
it ( 'renders' , ( ) => {
22
40
const vm = mountComponent ( maskedInput , {
@@ -219,18 +237,15 @@ describe('inputMask', () => {
219
237
} )
220
238
221
239
it ( 'emits focus and blur events for parent components' , ( ) => {
222
- const vm = mountComponent ( maskedInput , {
223
- value : '123' ,
224
- mask : [ '(' , / [ 1 - 9 ] / , / \d / , / \d / , ')' , ' ' , / \d / , / \d / , / \d / , '-' , / \d / , / \d / , / \d / , / \d / ]
225
- } )
240
+ const vm = mountComponent ( eventTest )
226
241
227
- vm . emitEvent = sinon . spy ( )
242
+ vm . callback = sinon . spy ( )
228
243
229
- vm . $el . focus ( )
230
- vm . $el . blur ( )
231
- expect ( vm . emitEvent . callCount ) . to . equal ( 2 )
232
- expect ( vm . emitEvent . getCall ( 0 ) . args [ 0 ] . type ) . to . equal ( 'focus' )
233
- expect ( vm . emitEvent . getCall ( 1 ) . args [ 0 ] . type ) . to . equal ( 'blur' )
244
+ vm . $refs . maskedInput . $ el. focus ( )
245
+ vm . $refs . maskedInput . $ el. blur ( )
246
+ expect ( vm . callback . callCount ) . to . equal ( 2 )
247
+ expect ( vm . callback . getCall ( 0 ) . args [ 0 ] ) . to . equal ( 'focus' )
248
+ expect ( vm . callback . getCall ( 1 ) . args [ 0 ] ) . to . equal ( 'blur' )
234
249
} )
235
250
236
251
it ( 'does not emit "input" event after component mount' , ( ) => {
@@ -248,22 +263,19 @@ describe('inputMask', () => {
248
263
} )
249
264
250
265
it ( 'emits keypress event for parent components' , ( ) => {
251
- const vm = mountComponent ( maskedInput , {
252
- value : '123' ,
253
- mask : [ '(' , / [ 1 - 9 ] / , / \d / , / \d / , ')' , ' ' , / \d / , / \d / , / \d / , '-' , / \d / , / \d / , / \d / , / \d / ]
254
- } )
266
+ const vm = mountComponent ( eventTest )
255
267
256
- vm . emitEvent = sinon . spy ( )
268
+ vm . callback = sinon . spy ( )
257
269
258
270
const e = new window . KeyboardEvent ( 'keypress' , {
259
271
key : 'e' ,
260
272
bubbles : true ,
261
273
cancelable : true
262
274
} )
263
- vm . $el . dispatchEvent ( e )
275
+ vm . $refs . maskedInput . $ el. dispatchEvent ( e )
264
276
265
- expect ( vm . emitEvent . callCount ) . to . equal ( 1 )
266
- expect ( vm . emitEvent . getCall ( 0 ) . args [ 0 ] . type ) . to . equal ( 'keypress' )
277
+ expect ( vm . callback . callCount ) . to . equal ( 1 )
278
+ expect ( vm . callback . getCall ( 0 ) . args [ 0 ] ) . to . equal ( 'keypress' )
267
279
} )
268
280
} )
269
281
0 commit comments