-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-dynamic.spec.js
126 lines (115 loc) · 3.01 KB
/
model-dynamic.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
import Vue from 'vue'
describe('Directive v-model dynamic input type', () => {
it('should work', done => {
const vm = new Vue({
data: {
inputType: null,
test: 'b'
},
template: `<input :type="inputType" v-model="test">`
}).$mount()
document.body.appendChild(vm.$el)
// test text
assertInputWorks(vm, 'inputType').then(done)
})
it('with v-if', done => {
const vm = new Vue({
data: {
ok: true,
type: null,
test: 'b'
},
template: `<input v-if="ok" :type="type" v-model="test"><div v-else>haha</div>`
}).$mount()
document.body.appendChild(vm.$el)
const chain = assertInputWorks(vm).then(() => {
vm.ok = false
}).then(() => {
expect(vm.$el.textContent).toBe('haha')
}).then(() => {
// reset
vm.ok = true
vm.type = null
vm.test = 'b'
})
assertInputWorks(vm, chain).then(done)
})
it('with v-for', done => {
const vm = new Vue({
data: {
data: {
text: 'foo',
checkbox: true
},
types: ['text', 'checkbox']
},
template: `<div>
<input v-for="type in types" :type="type" v-model="data[type]">
</div>`
}).$mount()
document.body.appendChild(vm.$el)
let el1 = vm.$el.children[0]
expect(el1.type).toBe('text')
expect(el1.value).toBe('foo')
el1.value = 'bar'
triggerEvent(el1, 'input')
expect(vm.data.text).toBe('bar')
let el2 = vm.$el.children[1]
expect(el2.type).toBe('checkbox')
expect(el2.checked).toBe(true)
el2.click()
expect(vm.data.checkbox).toBe(false)
// now in reverse!
vm.types.reverse()
waitForUpdate(() => {
el1 = vm.$el.children[0]
expect(el1.type).toBe('checkbox')
expect(el1.checked).toBe(false)
el1.click()
expect(vm.data.checkbox).toBe(true)
el2 = vm.$el.children[1]
expect(el2.type).toBe('text')
expect(el2.value).toBe('bar')
el2.value = 'foo'
triggerEvent(el2, 'input')
expect(vm.data.text).toBe('foo')
}).then(done)
})
})
function assertInputWorks (vm, type, chain) {
if (typeof type !== 'string') {
if (!chain) chain = type
type = 'type'
}
if (!chain) chain = waitForUpdate()
chain.then(() => {
expect(vm.$el.value).toBe('b')
vm.test = 'a'
}).then(() => {
expect(vm.$el.value).toBe('a')
vm.$el.value = 'c'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('c')
}).then(() => {
// change it to password
vm[type] = 'password'
vm.test = 'b'
}).then(() => {
expect(vm.$el.type).toBe('password')
expect(vm.$el.value).toBe('b')
vm.$el.value = 'c'
triggerEvent(vm.$el, 'input')
expect(vm.test).toBe('c')
}).then(() => {
// change it to checkbox...
vm[type] = 'checkbox'
}).then(() => {
expect(vm.$el.type).toBe('checkbox')
expect(vm.$el.checked).toBe(true)
}).then(() => {
vm.$el.click()
expect(vm.$el.checked).toBe(false)
expect(vm.test).toBe(false)
})
return chain
}