forked from bootstrap-vue/bootstrap-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisteners.spec.js
245 lines (216 loc) · 7.4 KB
/
listeners.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
import { isVue3 } from '../vue'
import { mount } from '@vue/test-utils'
import { listenersMixin } from './listeners'
// Note: The following tests indirectly test `utils/cache`
describe('mixins > listeners', () => {
it('works', async () => {
const BTest = {
compatConfig: {
MODE: 3,
RENDER_FUNCTION: 'suppress-warning',
INSTANCE_LISTENERS: 'suppress-warning'
},
name: 'BTest',
mixins: [listenersMixin],
inheritAttrs: false,
render(h) {
return h('button', { on: this.bvListeners })
}
}
const App = {
compatConfig: { MODE: 3, RENDER_FUNCTION: 'suppress-warning' },
name: 'App',
props: ['listenClick', 'listenFocus', 'listenBlur'],
computed: {
listeners() {
const listeners = {}
if (this.listenClick) {
listeners.click = event => this.$emit('click', event)
}
if (this.listenFocus) {
listeners.focus = event => this.$emit('focus', event)
}
if (this.listenBlur) {
listeners.blur = event => this.$emit('blur', event)
}
return listeners
}
},
render(h) {
return h(BTest, { on: this.listeners })
}
}
const wrapper = mount(App)
expect(wrapper).toBeDefined()
expect(wrapper.vm).toBeDefined()
expect(wrapper.element.tagName).toBe('BUTTON')
const $test = wrapper.findComponent(BTest)
expect($test.exists()).toBe(true)
expect($test.vm).toBeDefined()
expect($test.vm.bvListeners).toBeDefined()
expect($test.vm.bvListeners.click).toBeUndefined()
expect($test.vm.bvListeners.focus).toBeUndefined()
expect($test.vm.bvListeners.blur).toBeUndefined()
// Correctly adds new listeners
await wrapper.setProps({
listenClick: true,
listenFocus: true
})
expect($test.vm.bvListeners.click).toBeDefined()
expect($test.vm.bvListeners.focus).toBeDefined()
expect($test.vm.bvListeners.blur).toBeUndefined()
// Correctly updates listeners
await wrapper.setProps({
listenClick: false,
listenBlur: true
})
expect($test.vm.bvListeners.click).toBeUndefined()
expect($test.vm.bvListeners.focus).toBeDefined()
expect($test.vm.bvListeners.blur).toBeDefined()
// Correctly removes listeners
await wrapper.setProps({
listenClick: false,
listenFocus: false,
listenBlur: false
})
expect($test.vm.bvListeners.click).toBeUndefined()
expect($test.vm.bvListeners.focus).toBeUndefined()
expect($test.vm.bvListeners.blur).toBeUndefined()
wrapper.destroy()
})
it('does not re-render parent child components', async () => {
let input1RenderCount = 0
let input2RenderCount = 0
const Input1 = {
compatConfig: {
MODE: 3,
RENDER_FUNCTION: 'suppress-warning',
INSTANCE_LISTENERS: 'suppress-warning'
},
props: ['value'],
render(h) {
input1RenderCount++
return h('input', {
attrs: { value: this.value },
domProps: { value: this.value },
on: { ...this.$listeners, input: e => this.$emit('input', e.target.value) }
})
}
}
const Input2 = {
compatConfig: {
MODE: 3,
RENDER_FUNCTION: 'suppress-warning',
INSTANCE_LISTENERS: 'suppress-warning'
},
props: ['value'],
mixins: [listenersMixin],
render(h) {
input2RenderCount++
return h('input', {
attrs: { value: this.value },
domProps: { value: this.value },
on: { ...this.bvListeners, input: e => this.$emit('input', e.target.value) }
})
}
}
const App1 = {
components: { Input1 },
props: ['listenFocus1', 'listenFocus2'],
methods: {
emit1($event) {
if (this.listenFocus1) {
this.$emit('focus1', $event)
}
},
emit2($event) {
if (this.listenFocus2) {
this.$emit('focus2', $event)
}
}
},
template: `<div>
<Input1 @focus="emit1" />
<Input1 @focus="emit2" />
</div>`
}
const App2 = {
components: { Input2 },
props: ['listenFocus1', 'listenFocus2'],
methods: {
emit1($event) {
if (this.listenFocus1) {
this.$emit('focus1', $event)
}
},
emit2($event) {
if (this.listenFocus2) {
this.$emit('focus2', $event)
}
}
},
template: `<div>
<Input2 @focus="emit1" />
<Input2 @focus="emit2" />
</div>`
}
const wrapper1 = mount(App1, { attachTo: document.body })
const wrapper2 = mount(App2, { attachTo: document.body })
// --- `Input1` tests ---
const $inputs1 = wrapper1.findAllComponents(Input1)
expect($inputs1.length).toBe(2)
expect($inputs1.at(0)).toBeDefined()
expect($inputs1.at(1)).toBeDefined()
expect(wrapper1.emitted().focus1).not.toBeTruthy()
expect(wrapper1.emitted().focus2).not.toBeTruthy()
expect(input1RenderCount).toBe(2)
await $inputs1.at(0).trigger('focus')
expect(wrapper1.emitted().focus1).not.toBeTruthy()
await $inputs1.at(1).trigger('focus')
expect(wrapper1.emitted().focus2).not.toBeTruthy()
expect(input1RenderCount).toBe(2)
// Enable focus events for the first input and trigger it
await wrapper1.setProps({ listenFocus1: true })
await $inputs1.at(0).trigger('focus')
expect(wrapper1.emitted().focus1).toBeTruthy()
expect(wrapper1.emitted().focus2).not.toBeTruthy()
// Both `Input1`'s are re-rendered (See: https://github.com/vuejs/vue/issues/7257)
expect(input1RenderCount).toBe(isVue3 ? 2 : 4)
// Enable focus events for the second input and trigger it
await wrapper1.setProps({ listenFocus2: true })
await $inputs1.at(1).trigger('focus')
expect(wrapper1.emitted().focus1).toBeTruthy()
expect(wrapper1.emitted().focus2).toBeTruthy()
// Both `Input1`'s are re-rendered (See: https://github.com/vuejs/vue/issues/7257)
expect(input1RenderCount).toBe(isVue3 ? 2 : 6)
// --- `Input2` tests ---
const $inputs2 = wrapper2.findAllComponents(Input2)
expect($inputs2.length).toBe(2)
expect($inputs2.at(0)).toBeDefined()
expect($inputs2.at(1)).toBeDefined()
expect(wrapper2.emitted().focus1).not.toBeTruthy()
expect(wrapper2.emitted().focus2).not.toBeTruthy()
expect(input2RenderCount).toBe(2)
await $inputs2.at(0).trigger('focus')
expect(wrapper2.emitted().focus1).not.toBeTruthy()
await $inputs2.at(1).trigger('focus')
expect(wrapper2.emitted().focus2).not.toBeTruthy()
expect(input2RenderCount).toBe(2)
// Enable focus events for the first input and trigger it
await wrapper2.setProps({ listenFocus1: true })
await $inputs2.at(0).trigger('focus')
expect(wrapper2.emitted().focus1).toBeTruthy()
expect(wrapper2.emitted().focus2).not.toBeTruthy()
// With `listenersMixin` only the affected `Input2` is re-rendered
expect(input2RenderCount).toBe(2)
// Enable focus events for the second input and trigger it
await wrapper2.setProps({ listenFocus2: true })
await $inputs2.at(1).trigger('focus')
expect(wrapper2.emitted().focus1).toBeTruthy()
expect(wrapper2.emitted().focus2).toBeTruthy()
// With `listenersMixin` only the affected `Input2` is re-rendered
expect(input2RenderCount).toBe(2)
wrapper1.destroy()
wrapper2.destroy()
})
})