This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 529
/
createTippy.test.js
284 lines (252 loc) · 8.61 KB
/
createTippy.test.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
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
import { h, hasTippy, cleanDocumentBody, wait } from '../utils'
import { Defaults } from '../../src/js/defaults'
import { Selectors } from '../../src/js/selectors'
import createTippy from '../../src/js/createTippy'
import * as Utils from '../../src/js/utils'
afterEach(cleanDocumentBody)
describe('createTippy', () => {
it('returns `null` if the reference already has a tippy instance', () => {
const reference = h()
reference._tippy = true
expect(createTippy(reference, Defaults)).toBe(null)
})
it('returns the instance with expected properties', () => {
const tip = createTippy(h(), Defaults)
expect(tip.id).toBeDefined()
expect(tip.reference).toBeDefined()
expect(tip.popper).toBeDefined()
expect(tip.popperInstance).toBeDefined()
expect(tip.popperChildren).toBeDefined()
expect(tip.popperInstance).toBeDefined()
expect(tip.state).toBeDefined()
expect(tip.clearDelayTimeouts).toBeDefined()
expect(tip.set).toBeDefined()
expect(tip.setContent).toBeDefined()
expect(tip.show).toBeDefined()
expect(tip.hide).toBeDefined()
expect(tip.enable).toBeDefined()
expect(tip.disable).toBeDefined()
expect(tip.destroy).toBeDefined()
})
it('increments the `id` on each call with valid arguments', () => {
const tips = [
createTippy(h(), Defaults),
createTippy(h(), Defaults),
createTippy(h(), Defaults)
]
expect(tips[0].id).toBe(tips[1].id - 1)
expect(tips[1].id).toBe(tips[2].id - 1)
})
it('adds correct listeners to the reference element based on `trigger`', async done => {
// Note that hide delay is 20ms by default
const HIDE_DELAY = 21
const instance = createTippy(h(), {
...Defaults,
trigger: 'mouseenter focus click'
})
instance.reference.dispatchEvent(new Event('mouseenter'))
expect(instance.state.isVisible).toBe(true)
instance.reference.dispatchEvent(new Event('mouseleave'))
await wait(HIDE_DELAY)
expect(instance.state.isVisible).toBe(false)
instance.reference.dispatchEvent(new Event('focus'))
expect(instance.state.isVisible).toBe(true)
instance.reference.dispatchEvent(new Event('blur'))
await wait(HIDE_DELAY)
expect(instance.state.isVisible).toBe(false)
instance.reference.dispatchEvent(new Event('click'))
expect(instance.state.isVisible).toBe(true)
instance.reference.dispatchEvent(new Event('click'))
await wait(HIDE_DELAY)
expect(instance.state.isVisible).toBe(false)
done()
})
})
describe('instance.destroy', () => {
it('sets state.isDestroyed to `true`', () => {
const instance = createTippy(h(), Defaults)
instance.destroy()
expect(instance.state.isDestroyed).toBe(true)
})
it('deletes the `_tippy` property from the reference', () => {
const ref = h()
const instance = createTippy(ref, Defaults)
expect('_tippy' in ref).toBe(true)
instance.destroy()
expect('_tippy' in ref).toBe(false)
})
it('removes listeners from the reference', () => {
const ref = h()
const instance = createTippy(ref, { ...Defaults, trigger: 'mouseenter' })
instance.destroy()
ref.dispatchEvent(new Event('mouseenter'))
expect(instance.state.isVisible).toBe(false)
})
it('does nothing if the instance is already destroyed', () => {
const ref = h()
const instance = createTippy(ref, Defaults)
instance.state.isDestroyed = true
instance.destroy()
expect(ref._tippy).toBeDefined()
})
it('destroys target instances if `true` is passed as an argument', () => {
const ref = h()
const p = document.createElement('p')
ref.append(p)
const instance = createTippy(ref, { ...Defaults, target: 'p' })
p._tippy = createTippy(p, Defaults)
expect(p._tippy).toBeDefined()
ref._tippy.destroy(true)
expect(p._tippy).toBeUndefined()
})
})
describe('instance.show', () => {
it('changes state.isVisible to `true`', () => {
const instance = createTippy(h(), Defaults)
instance.show()
expect(instance.state.isVisible).toBe(true)
instance.destroy()
})
it('mounts the popper to the DOM', () => {
const instance = createTippy(h(), Defaults)
instance.show()
expect(document.querySelector(Selectors.POPPER)).toBe(instance.popper)
instance.destroy()
})
it('overrides instance.props.duration if supplied an argument', done => {
const instance = createTippy(h(), {
...Defaults,
duration: 100
})
instance.show(10)
setTimeout(() => {
expect(instance.popperChildren.tooltip.style.transitionDuration).toBe(
'10ms'
)
instance.destroy()
done()
}, 20)
})
it('adds .tippy-active class if interactive', done => {
const instance = createTippy(h(), {
...Defaults,
interactive: true
})
instance.show()
setTimeout(() => {
expect(instance.reference.classList.contains('tippy-active')).toBe(true)
done()
})
})
it('does not show tooltip if the reference has a `disabled` attribute', () => {
const ref = h()
ref.setAttribute('disabled', 'disabled')
const instance = createTippy(ref, Defaults)
instance.show()
expect(instance.state.isVisible).toBe(false)
})
})
describe('instance.hide', () => {
it('changes state.isVisible to false', () => {
const instance = createTippy(h(), Defaults)
instance.hide()
expect(instance.state.isVisible).toBe(false)
instance.destroy()
})
it('removes the popper element from the DOM after hiding', () => {
const instance = createTippy(h(), {
...Defaults
})
instance.show(0)
expect(document.querySelector(Selectors.POPPER)).toBe(instance.popper)
instance.hide(0)
setTimeout(() => {
expect(document.querySelector(Selectors.POPPER)).toBeNull()
instance.destroy()
done()
}, 20)
})
it('overrides instance.props.duration if supplied an argument', () => {
const instance = createTippy(h(), {
...Defaults,
duration: 100
})
instance.show(0)
instance.hide(9)
expect(instance.popperChildren.tooltip.style.transitionDuration).toBe('9ms')
instance.destroy()
})
})
describe('instance.enable', () => {
it('sets state.isEnabled to `true`', () => {
const instance = createTippy(h(), Defaults)
instance.enable()
expect(instance.state.isEnabled).toBe(true)
instance.destroy()
})
it('allows a tippy to be shown', () => {
const instance = createTippy(h(), Defaults)
instance.enable()
instance.show()
expect(instance.state.isVisible).toBe(true)
instance.destroy()
})
})
describe('instance.disable', () => {
it('sets state.isEnabled to `false`', () => {
const instance = createTippy(h(), Defaults)
instance.disable()
expect(instance.state.isEnabled).toBe(false)
instance.destroy()
})
it('disallows a tippy to be shown', () => {
const instance = createTippy(h(), Defaults)
instance.disable()
instance.show()
expect(instance.state.isVisible).toBe(false)
instance.destroy()
})
})
describe('instance.set', () => {
it('sets the new props by merging them with the current instance', () => {
const instance = createTippy(h(), Defaults)
expect(instance.props.arrow).toBe(Defaults.arrow)
expect(instance.props.duration).toBe(Defaults.duration)
instance.set({ arrow: !Defaults.arrow, duration: 82 })
expect(instance.props.arrow).toBe(!Defaults.arrow)
expect(instance.props.duration).toBe(82)
})
it('redraws the tooltip by creating a new popper element', () => {
const instance = createTippy(h(), Defaults)
expect(instance.popper.querySelector('.tippy-arrow')).toBeNull()
instance.set({ arrow: true })
expect(instance.popper.querySelector('.tippy-arrow')).not.toBeNull()
})
it('popperChildren property is updated to reflect the new popper element', () => {
const instance = createTippy(h(), Defaults)
expect(instance.popperChildren.arrow).toBeNull()
instance.set({ arrow: true })
expect(instance.popperChildren.arrow).not.toBeNull()
})
it('popperInstance popper is updated to the new popper', () => {
const instance = createTippy(h(), {
...Defaults,
lazy: false
})
instance.set({ arrow: true })
expect(instance.popperInstance.popper).toBe(instance.popper)
})
it('popper._tippy is defined with the correct instance', () => {
const instance = createTippy(h(), Defaults)
instance.set({ arrow: true })
expect(instance.popper._tippy).toBe(instance)
})
})
describe('instance.setContent', () => {
it('works like set({ content: newContent })', () => {
const instance = createTippy(h(), Defaults)
const content = 'Hello!'
instance.setContent(content)
expect(instance.props.content).toBe(content)
})
})