/
VDialog.spec.ts
355 lines (292 loc) Β· 8.7 KB
/
VDialog.spec.ts
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
// Components
import VDialog from '../VDialog'
// Utilities
import {
mount,
Wrapper,
} from '@vue/test-utils'
// eslint-disable-next-line max-statements
describe('VDialog.ts', () => {
type Instance = InstanceType<typeof VDialog>
let mountFunction: (options?: object) => Wrapper<Instance>
let el
beforeEach(() => {
el = document.createElement('div')
el.setAttribute('data-app', 'true')
document.body.appendChild(el)
mountFunction = (options = {}) => {
return mount(VDialog, {
mocks: {
$vuetify: {
theme: {},
breakpoint: {},
},
},
...options,
})
}
})
afterEach(() => {
document.body.removeChild(el)
})
it('should render component and match snapshot', () => {
const wrapper = mountFunction()
expect(wrapper.html()).toMatchSnapshot()
})
it('should render a disabled component and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
disabled: true,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render a persistent component and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
persistent: true,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render a fullscreen component and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
fullscreen: true,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render a eager component and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
eager: true,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render a scrollable component and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
scrollable: true,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render component with custom origin and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
origin: 'top right',
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render component with custom width (max-width) and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
maxWidth: 100,
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render component with custom width and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
width: '50%',
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should render component with custom transition and match snapshot', () => {
const wrapper = mountFunction({
propsData: {
transition: 'fade-transition',
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should open dialog on activator click', async () => {
const input = jest.fn()
const wrapper = mountFunction({
scopedSlots: {
activator ({ on }) {
return this.$createElement('div', {
staticClass: 'activator',
on,
})
},
},
})
wrapper.vm.$on('input', input)
expect(wrapper.vm.isActive).toBe(false)
wrapper.find('div.activator').trigger('click')
expect(wrapper.vm.isActive).toBe(true)
await wrapper.vm.$nextTick()
expect(input).toHaveBeenCalledWith(true)
})
it('not should open disabed dialog on activator click', async () => {
const input = jest.fn()
const wrapper = mountFunction({
propsData: {
disabled: true,
},
scopedSlots: {
// eslint-disable-next-line sonarjs/no-identical-functions
activator ({ on }) {
return this.$createElement('div', {
staticClass: 'activator',
on,
})
},
},
})
wrapper.vm.$on('input', input)
expect(wrapper.vm.isActive).toBe(false)
wrapper.find('div.activator').trigger('click')
expect(wrapper.vm.isActive).toBe(false)
await wrapper.vm.$nextTick()
expect(input).not.toHaveBeenCalled()
})
it('not change state on v-model update', async () => {
const wrapper = mountFunction({
propsData: {
value: false,
},
scopedSlots: {
activator: '<span>activator</span>',
},
})
expect(wrapper.vm.isActive).toBe(false)
wrapper.setProps({
value: true,
})
await wrapper.vm.$nextTick()
expect(wrapper.vm.isActive).toBe(true)
wrapper.setProps({
value: false,
})
await wrapper.vm.$nextTick()
expect(wrapper.vm.isActive).toBe(false)
})
it('should emit keydown event', async () => {
const keydown = jest.fn()
const wrapper = mountFunction({
propsData: { value: true },
})
wrapper.vm.$on('keydown', keydown)
await wrapper.vm.$nextTick()
wrapper.vm.$refs.content.dispatchEvent(new Event('keydown'))
expect(keydown).toHaveBeenCalled()
})
// https://github.com/vuetifyjs/vuetify/issues/3101
it('should always remove scrollbar when fullscreen', async () => {
const wrapper = mountFunction()
wrapper.setProps({ value: true })
await wrapper.vm.$nextTick()
expect(document.documentElement.className).not.toContain('overflow-y-hidden')
wrapper.setProps({ fullscreen: true })
await wrapper.vm.$nextTick()
expect(document.documentElement.className).toContain('overflow-y-hidden')
})
it('should not respond to events if disabled', async () => {
const wrapper = mountFunction({
propsData: {
disabled: true,
},
scopedSlots: {
// eslint-disable-next-line sonarjs/no-identical-functions
activator ({ on }) {
return this.$createElement('div', {
staticClass: 'activator',
on,
})
},
},
})
const activator = wrapper.find('div.activator')
activator.trigger('click')
expect(wrapper.vm.isActive).toBe(false)
})
// https://github.com/vuetifyjs/vuetify/issues/5533
it('should emit click:outside', async () => {
const input = jest.fn()
const clickOutside = jest.fn()
const wrapper = mountFunction({
scopedSlots: {
// eslint-disable-next-line sonarjs/no-identical-functions
activator ({ on }) {
return this.$createElement('div', {
staticClass: 'activator',
on,
})
},
},
})
wrapper.vm.$on('input', input)
wrapper.vm.$on('click:outside', clickOutside)
expect(wrapper.vm.isActive).toBe(false)
wrapper.find('div.activator').trigger('click')
expect(wrapper.vm.isActive).toBe(true)
await wrapper.vm.$nextTick()
expect(input).toHaveBeenCalledWith(true)
wrapper.vm.onClickOutside(new Event('click'))
expect(clickOutside).toHaveBeenCalled()
})
// Ensure dialog opens up when provided a default value
it('should set model active before mounted', () => {
const wrapper = mountFunction({
propsData: { value: true },
})
expect(wrapper.vm.isActive).toBe(true)
})
it('should close dialog on escape keydown', () => {
const wrapper = mountFunction({
propsData: { value: true },
})
expect(wrapper.vm.isActive).toBe(true)
const content = wrapper.find('.v-dialog__content')
content.trigger('keydown.esc')
expect(wrapper.vm.isActive).toBe(false)
})
it('should only set tabindex if active', () => {
const wrapper = mountFunction({
propsData: { eager: true },
})
const dialog = wrapper.find('.v-dialog')
expect(dialog.html()).toMatchSnapshot()
expect(dialog.element.tabIndex).toBe(-1)
wrapper.setData({ isActive: true })
expect(dialog.element.tabIndex).toBe(0)
expect(dialog.html()).toMatchSnapshot()
})
// https://github.com/vuetifyjs/vuetify/issues/8697
it('should not close if persistent and hide-overly when click outside', async () => {
const input = jest.fn()
const clickOutside = jest.fn()
const wrapper = mountFunction({
propsData: {
persistent: true,
hideOverlay: true,
},
scopedSlots: {
// eslint-disable-next-line sonarjs/no-identical-functions
activator ({ on }) {
return this.$createElement('div', {
staticClass: 'activator',
on,
})
},
},
})
wrapper.vm.$on('input', input)
wrapper.vm.$on('click:outside', clickOutside)
expect(wrapper.vm.isActive).toBe(false)
wrapper.find('div.activator').trigger('click')
expect(wrapper.vm.isActive).toBe(true)
await wrapper.vm.$nextTick()
expect(input).toHaveBeenCalledWith(true)
wrapper.vm.onClickOutside(new Event('click'))
expect(clickOutside).toHaveBeenCalled()
expect(wrapper.vm.isActive).toBe(true)
})
})