tooltip.spec.js
5.96 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
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
import { mount } from '@vue/test-utils'
import { waitNT, waitRAF } from '../../../tests/utils'
import { VBTooltip } from './tooltip'
// Key which we use to store tooltip object on element
const BV_TOOLTIP = '__BV_Tooltip__'
describe('v-b-tooltip directive', () => {
const originalCreateRange = document.createRange
const origGetBCR = Element.prototype.getBoundingClientRect
beforeEach(() => {
// Hack to make Popper not bork out during tests
// Note: Popper still does not do any positioning calculation in JSDOM though
// So we cannot test actual positioning - just detect when it is open
// https://github.com/FezVrasta/popper.js/issues/478#issuecomment-407422016
document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: 'BODY',
ownerDocument: document
}
})
// Mock `getBoundingClientRect()` so that the `isVisible(el)` test returns `true`
// Needed for visibility checks of trigger element, etc.
Element.prototype.getBoundingClientRect = jest.fn(() => ({
width: 24,
height: 24,
top: 0,
left: 0,
bottom: 0,
right: 0
}))
})
afterEach(() => {
// Reset overrides
document.createRange = originalCreateRange
Element.prototype.getBoundingClientRect = origGetBCR
})
it('should have BVTooltip Vue class instance', async () => {
jest.useFakeTimers()
const App = {
directives: {
bTooltip: VBTooltip
},
template: '<button v-b-tooltip title="foobar">button</button>'
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('BUTTON')
const $button = wrapper.find('button')
// Should have instance of popover class on it
expect($button.element[BV_TOOLTIP]).toBeDefined()
expect($button.element[BV_TOOLTIP].$options.name).toBe('BVTooltip')
wrapper.destroy()
})
it('should work', async () => {
jest.useFakeTimers()
const App = {
directives: {
bTooltip: VBTooltip
},
template: '<button v-b-tooltip.click.html title="<b>foobar</b>">button</button>'
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('BUTTON')
const $button = wrapper.find('button')
// Should have instance of popover class on it
expect($button.element[BV_TOOLTIP]).toBeDefined()
expect($button.element[BV_TOOLTIP].$options.name).toBe('BVTooltip')
expect($button.attributes('aria-describedby')).toBeUndefined()
// Trigger click
await $button.trigger('click')
await waitRAF()
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect($button.attributes('aria-describedby')).toBeDefined()
const adb = $button.attributes('aria-describedby')
const tip = document.querySelector(`#${adb}`)
expect(tip).not.toBe(null)
expect(tip.classList.contains('tooltip')).toBe(true)
wrapper.destroy()
})
it('should not show tooltip when title is empty', async () => {
jest.useFakeTimers()
const App = {
directives: {
bTooltip: VBTooltip
},
template: '<button v-b-tooltip.click title="">button</button>'
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('BUTTON')
const $button = wrapper.find('button')
// Should have instance of popover class on it
expect($button.element[BV_TOOLTIP]).toBeDefined()
expect($button.element[BV_TOOLTIP].$options.name).toBe('BVTooltip')
expect($button.attributes('aria-describedby')).toBeUndefined()
// Trigger click
await $button.trigger('click')
await waitRAF()
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect($button.attributes('aria-describedby')).toBeUndefined()
wrapper.destroy()
})
it('variant and customClass should work', async () => {
jest.useFakeTimers()
const App = {
directives: {
bTooltip: VBTooltip
},
template: `<button v-b-tooltip.click.html.v-info="{ customClass: 'foobar'}" title="<b>foobar</b>">button</button>`
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper.vm).toBeDefined()
expect(wrapper.element.tagName).toBe('BUTTON')
const $button = wrapper.find('button')
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
await waitNT(wrapper.vm)
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
// Trigger click
await $button.trigger('click')
await waitRAF()
await waitRAF()
jest.runOnlyPendingTimers()
await waitNT(wrapper.vm)
await waitRAF()
expect($button.attributes('aria-describedby')).toBeDefined()
const adb = $button.attributes('aria-describedby')
const tip = document.querySelector(`#${adb}`)
expect(tip).not.toBe(null)
expect(tip.classList.contains('tooltip')).toBe(true)
expect(tip.classList.contains('b-tooltip-info')).toBe(true)
expect(tip.classList.contains('foobar')).toBe(true)
wrapper.destroy()
})
})