table-tfoot-events.spec.js
4.76 KB
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
import { mount } from '@vue/test-utils'
import { BTable } from './table'
const testItems = [{ a: 1, b: 2, c: 3 }]
const testFields = [{ key: 'a', label: 'A' }, { key: 'b', label: 'B' }, { key: 'c', label: 'C' }]
describe('table > tfoot events', () => {
it('should emit head-clicked event when a head cell is clicked', async () => {
const wrapper = mount(BTable, {
propsData: {
fields: testFields,
items: testItems,
footClone: true
},
listeners: {
// Head-clicked will not be emitted unless there is a
// registered head-clicked listener
'head-clicked': () => {}
}
})
expect(wrapper).toBeDefined()
const $rows = wrapper.findAll('tfoot > tr')
expect($rows.length).toBe(1)
const $ths = wrapper.findAll('tfoot > tr > th')
expect($ths.length).toBe(testFields.length)
expect(wrapper.emitted('head-clicked')).toBeUndefined()
await $ths.at(0).trigger('click')
expect(wrapper.emitted('head-clicked')).toBeDefined()
expect(wrapper.emitted('head-clicked').length).toBe(1)
expect(wrapper.emitted('head-clicked')[0][0]).toEqual(testFields[0].key) // Field key
expect(wrapper.emitted('head-clicked')[0][1]).toEqual(testFields[0]) // Field definition
expect(wrapper.emitted('head-clicked')[0][2]).toBeInstanceOf(MouseEvent) // Event
expect(wrapper.emitted('head-clicked')[0][3]).toBe(true) // Is footer
await $ths.at(2).trigger('click')
expect(wrapper.emitted('head-clicked').length).toBe(2)
expect(wrapper.emitted('head-clicked')[1][0]).toEqual(testFields[2].key) // Field key
expect(wrapper.emitted('head-clicked')[1][1]).toEqual(testFields[2]) // Field definition
expect(wrapper.emitted('head-clicked')[1][2]).toBeInstanceOf(MouseEvent) // Event
expect(wrapper.emitted('head-clicked')[1][3]).toBe(true) // Is footer
wrapper.destroy()
})
it('should not emit head-clicked event when prop busy is set', async () => {
const wrapper = mount(BTable, {
propsData: {
fields: testFields,
items: testItems,
footClone: true,
busy: true
},
listeners: {
// Head-clicked will not be emitted unless there is a
// registered head-clicked listener
'head-clicked': () => {}
}
})
expect(wrapper).toBeDefined()
const $ths = wrapper.findAll('tfoot > tr > th')
expect($ths.length).toBe(testFields.length)
expect(wrapper.emitted('head-clicked')).toBeUndefined()
await $ths.at(0).trigger('click')
expect(wrapper.emitted('head-clicked')).toBeUndefined()
wrapper.destroy()
})
it('should not emit head-clicked event when vm.localBusy is true', async () => {
const wrapper = mount(BTable, {
propsData: {
fields: testFields,
items: testItems,
footClone: true
},
listeners: {
// Head-clicked will not be emitted unless there is a
// registered head-clicked listener
'head-clicked': () => {}
}
})
await wrapper.setData({
localBusy: true
})
expect(wrapper).toBeDefined()
const $ths = wrapper.findAll('tfoot > tr > th')
expect($ths.length).toBe(testFields.length)
expect(wrapper.emitted('head-clicked')).toBeUndefined()
await $ths.at(0).trigger('click')
expect(wrapper.emitted('head-clicked')).toBeUndefined()
wrapper.destroy()
})
it('should not emit head-clicked event when clicking on a button or other interactive element', async () => {
const wrapper = mount(BTable, {
propsData: {
fields: testFields,
items: testItems,
footClone: true
},
listeners: {
// Head-clicked will not be emitted unless there is a
// registered head-clicked listener
'head-clicked': () => {}
},
slots: {
// In Vue 2.6x, slots get translated into scopedSlots
'foot(a)': '<button id="a">button</button>',
'foot(b)': '<input id="b">',
// Will use `head` slot if foot slot not defined
'head(c)': '<a href="#" id="c">link</a>'
}
})
expect(wrapper).toBeDefined()
const $ths = wrapper.findAll('tfoot > tr > th')
expect($ths.length).toBe(testFields.length)
expect(wrapper.emitted('head-clicked')).toBeUndefined()
const $btn = wrapper.find('button[id="a"]')
expect($btn.exists()).toBe(true)
await $btn.trigger('click')
expect(wrapper.emitted('head-clicked')).toBeUndefined()
const $input = wrapper.find('input[id="b"]')
expect($input.exists()).toBe(true)
await $input.trigger('click')
expect(wrapper.emitted('head-clicked')).toBeUndefined()
const $link = wrapper.find('a[id="c"]')
expect($link.exists()).toBe(true)
await $link.trigger('click')
expect(wrapper.emitted('head-clicked')).toBeUndefined()
wrapper.destroy()
})
})