-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
/
Copy pathmodel-text.spec.js
394 lines (370 loc) · 10.2 KB
/
model-text.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import Vue from 'vue'
import { isIE9, isAndroid } from 'core/util/env'
describe('Directive v-model text', () => {
it('should update value both ways', done => {
const vm = new Vue({
data: {
test: 'b'
},
template: '<input v-model="test">'
}).$mount()
expect(vm.$el.value).toBe('b')
vm.test = 'a'
waitForUpdate(() => {
expect(vm.$el.value).toBe('a')
vm.$el.value = 'c'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('c')
}).then(done)
})
it('.lazy modifier', () => {
const vm = new Vue({
data: {
test: 'b'
},
template: '<input v-model.lazy="test">'
}).$mount()
expect(vm.$el.value).toBe('b')
expect(vm.test).toBe('b')
vm.$el.value = 'c'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('b')
triggerEvent(vm.$el, 'change')
expect(vm.test).toBe('c')
})
it('.number modifier', () => {
const vm = new Vue({
data: {
test: 1
},
template: '<input v-model.number="test">'
}).$mount()
expect(vm.test).toBe(1)
vm.$el.value = '2'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe(2)
// should let strings pass through
vm.$el.value = 'f'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('f')
})
it('.trim modifier', () => {
const vm = new Vue({
data: {
test: 'hi'
},
template: '<input v-model.trim="test">'
}).$mount()
expect(vm.test).toBe('hi')
vm.$el.value = ' what '
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('what')
})
it('.number focus and typing', (done) => {
const vm = new Vue({
data: {
test: 0,
update: 0
},
template:
'<div>' +
'<input ref="input" v-model.number="test">{{ update }}' +
'<input ref="blur">' +
'</div>'
}).$mount()
document.body.appendChild(vm.$el)
vm.$refs.input.focus()
expect(vm.test).toBe(0)
vm.$refs.input.value = '1.0'
triggerEvent(vm.$refs.input, 'input')
expect(vm.test).toBe(1)
vm.update++
waitForUpdate(() => {
expect(vm.$refs.input.value).toBe('1.0')
vm.$refs.blur.focus()
vm.update++
}).then(() => {
expect(vm.$refs.input.value).toBe('1')
}).then(done)
})
it('.trim focus and typing', (done) => {
const vm = new Vue({
data: {
test: 'abc',
update: 0
},
template:
'<div>' +
'<input ref="input" v-model.trim="test" type="text">{{ update }}' +
'<input ref="blur"/>' +
'</div>'
}).$mount()
document.body.appendChild(vm.$el)
vm.$refs.input.focus()
vm.$refs.input.value = ' abc '
triggerEvent(vm.$refs.input, 'input')
expect(vm.test).toBe('abc')
vm.update++
waitForUpdate(() => {
expect(vm.$refs.input.value).toBe(' abc ')
vm.$refs.blur.focus()
vm.update++
}).then(() => {
expect(vm.$refs.input.value).toBe('abc')
}).then(done)
})
it('multiple inputs', (done) => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
selections: [[1, 2, 3], [4, 5]],
inputList: [
{
name: 'questionA',
data: ['a', 'b', 'c']
},
{
name: 'questionB',
data: ['1', '2']
}
]
},
watch: {
selections: spy
},
template:
'<div>' +
'<div v-for="(inputGroup, idx) in inputList">' +
'<div>' +
'<span v-for="(item, index) in inputGroup.data">' +
'<input v-bind:name="item" type="text" v-model.number="selections[idx][index]" v-bind:id="idx+\'-\'+index"/>' +
'<label>{{item}}</label>' +
'</span>' +
'</div>' +
'</div>' +
'<span ref="rs">{{selections}}</span>' +
'</div>'
}).$mount()
var inputs = vm.$el.getElementsByTagName('input')
inputs[1].value = 'test'
triggerEvent(inputs[1], 'input')
waitForUpdate(() => {
expect(spy).toHaveBeenCalled()
expect(vm.selections).toEqual([[1, 'test', 3], [4, 5]])
}).then(done)
})
if (isIE9) {
it('IE9 selectionchange', done => {
const vm = new Vue({
data: {
test: 'foo'
},
template: '<input v-model="test">'
}).$mount()
const input = vm.$el
input.value = 'bar'
document.body.appendChild(input)
input.focus()
triggerEvent(input, 'selectionchange')
waitForUpdate(() => {
expect(vm.test).toBe('bar')
input.value = 'a'
triggerEvent(input, 'selectionchange')
expect(vm.test).toBe('a')
}).then(done)
})
}
if (!isAndroid) {
it('compositionevents', function (done) {
const vm = new Vue({
data: {
test: 'foo'
},
template: '<input v-model="test">'
}).$mount()
const input = vm.$el
triggerEvent(input, 'compositionstart')
input.value = 'baz'
// input before composition unlock should not call set
triggerEvent(input, 'input')
expect(vm.test).toBe('foo')
// after composition unlock it should work
triggerEvent(input, 'compositionend')
triggerEvent(input, 'input')
expect(vm.test).toBe('baz')
done()
})
}
it('warn invalid tag', () => {
new Vue({
data: {
test: 'foo'
},
template: '<div v-model="test"></div>'
}).$mount()
expect('<div v-model="test">: v-model is not supported on this element type').toHaveBeenWarned()
})
// #3468
it('should have higher priority than user v-on events', () => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
a: 'a'
},
template: '<input v-model="a" @input="onInput">',
methods: {
onInput (e) {
spy(e.target.value)
}
}
}).$mount()
vm.$el.value = 'b'
triggerEvent(vm.$el, 'input')
expect(spy).toHaveBeenCalledWith('b')
})
it('warn binding to v-for alias', () => {
new Vue({
data: {
strings: ['hi']
},
template: `
<div>
<div v-for="str in strings">
<input v-model="str">
</div>
</div>
`
}).$mount()
expect('You are binding v-model directly to a v-for iteration alias').toHaveBeenWarned()
})
it('warn if both v-model and v-bind:value are used on the same text input', () => {
new Vue({
data: {
test: 'foo'
},
template: '<input type="text" v-model="test" v-bind:value="test"/>'
}).$mount()
expect('v-model and v-bind:value used on the same text input').toHaveBeenWarned()
})
it('warn if both v-model and :value are used on the same text input', () => {
new Vue({
data: {
test: 'foo'
},
template: '<input type="text" v-model="test" :value="test"/>'
}).$mount()
expect('v-model and :value used on the same text input').toHaveBeenWarned()
})
if (!isAndroid) {
it('does not trigger extra input events with single compositionend', () => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
a: 'a'
},
template: '<input v-model="a" @input="onInput">',
methods: {
onInput (e) {
spy(e.target.value)
}
}
}).$mount()
expect(spy.calls.count()).toBe(0)
vm.$el.value = 'b'
triggerEvent(vm.$el, 'input')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$el, 'compositionend')
expect(spy.calls.count()).toBe(1)
})
it('triggers extra input on compositionstart + end', () => {
const spy = jasmine.createSpy()
const vm = new Vue({
data: {
a: 'a'
},
template: '<input v-model="a" @input="onInput">',
methods: {
onInput (e) {
spy(e.target.value)
}
}
}).$mount()
expect(spy.calls.count()).toBe(0)
vm.$el.value = 'b'
triggerEvent(vm.$el, 'input')
expect(spy.calls.count()).toBe(1)
triggerEvent(vm.$el, 'compositionstart')
triggerEvent(vm.$el, 'compositionend')
expect(spy.calls.count()).toBe(2)
})
// #4392
it('should not update value with modifiers when in focus if post-conversion values are the same', done => {
const vm = new Vue({
data: {
a: 1,
foo: false
},
template: '<div>{{ foo }}<input ref="input" v-model.number="a"></div>'
}).$mount()
document.body.appendChild(vm.$el)
vm.$refs.input.focus()
vm.$refs.input.value = '1.000'
vm.foo = true
waitForUpdate(() => {
expect(vm.$refs.input.value).toBe('1.000')
}).then(done)
})
// #6552
// This was original introduced due to the microtask between DOM events issue
// but fixed after switching to MessageChannel.
it('should not block input when another input listener with modifier is used', done => {
const vm = new Vue({
data: {
a: 'a',
foo: false
},
template: `
<div>
<input ref="input" v-model="a" @input.capture="onInput">{{ a }}
<div v-if="foo">foo</div>
</div>
`,
methods: {
onInput (e) {
this.foo = true
}
}
}).$mount()
document.body.appendChild(vm.$el)
vm.$refs.input.focus()
vm.$refs.input.value = 'b'
triggerEvent(vm.$refs.input, 'input')
// not using wait for update here because there will be two update cycles
// one caused by onInput in the first listener
setTimeout(() => {
expect(vm.a).toBe('b')
expect(vm.$refs.input.value).toBe('b')
done()
}, 16)
})
it('should create and make reactive non-existent properties', done => {
const vm = new Vue({
data: {
foo: {}
},
template: '<input v-model="foo.bar">'
}).$mount()
expect(vm.$el.value).toBe('')
vm.$el.value = 'a'
triggerEvent(vm.$el, 'input')
expect(vm.foo.bar).toBe('a')
vm.foo.bar = 'b'
waitForUpdate(() => {
expect(vm.$el.value).toBe('b')
vm.foo = {}
}).then(() => {
expect(vm.$el.value).toBe('')
}).then(done)
})
}
})