tooltip.spec.js 30.6 KB
Newer Older
1
2
import Tooltip from '../../src/tooltip'
import EventHandler from '../../src/dom/event-handler'
3
import { noop } from '../../src/util/index'
Johann-S's avatar
Johann-S committed
4
5

/** Test helpers */
6
import { getFixture, clearFixture, jQueryMock, createEvent } from '../helpers/fixture'
Johann-S's avatar
Johann-S committed
7
8
9
10
11
12
13
14
15
16
17

describe('Tooltip', () => {
  let fixtureEl

  beforeAll(() => {
    fixtureEl = getFixture()
  })

  afterEach(() => {
    clearFixture()

18
    document.querySelectorAll('.tooltip').forEach(tooltipEl => {
Johann-S's avatar
Johann-S committed
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
      document.body.removeChild(tooltipEl)
    })
  })

  describe('VERSION', () => {
    it('should return plugin version', () => {
      expect(Tooltip.VERSION).toEqual(jasmine.any(String))
    })
  })

  describe('Default', () => {
    it('should return plugin default config', () => {
      expect(Tooltip.Default).toEqual(jasmine.any(Object))
    })
  })

  describe('NAME', () => {
    it('should return plugin name', () => {
      expect(Tooltip.NAME).toEqual(jasmine.any(String))
    })
  })

  describe('DATA_KEY', () => {
    it('should return plugin data key', () => {
      expect(Tooltip.DATA_KEY).toEqual('bs.tooltip')
    })
  })

  describe('Event', () => {
    it('should return plugin events', () => {
      expect(Tooltip.Event).toEqual(jasmine.any(Object))
    })
  })

  describe('EVENT_KEY', () => {
    it('should return plugin event key', () => {
      expect(Tooltip.EVENT_KEY).toEqual('.bs.tooltip')
    })
  })

  describe('DefaultType', () => {
    it('should return plugin default type', () => {
      expect(Tooltip.DefaultType).toEqual(jasmine.any(Object))
    })
  })

  describe('constructor', () => {
    it('should not take care of disallowed data attributes', () => {
67
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" data-sanitize="false" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
68
69
70
71
72
73
74
75

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      expect(tooltip.config.sanitize).toEqual(true)
    })

    it('should convert title and content to string if numbers', () => {
76
      fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
Johann-S's avatar
Johann-S committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        title: 1,
        content: 7
      })

      expect(tooltip.config.title).toEqual('1')
      expect(tooltip.config.content).toEqual('7')
    })

    it('should enable selector delegation', done => {
      fixtureEl.innerHTML = '<div></div>'

      const containerEl = fixtureEl.querySelector('div')
      const tooltipContainer = new Tooltip(containerEl, {
        selector: 'a[rel="tooltip"]',
        trigger: 'click'
      })

97
      containerEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
98
99
100
101
102
103
104
105
106
107
108

      const tooltipInContainerEl = containerEl.querySelector('a')

      tooltipInContainerEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).not.toBeNull()
        tooltipContainer.dispose()
        done()
      })

      tooltipInContainerEl.click()
    })
109

XhmikosR's avatar
XhmikosR committed
110
    it('should allow to pass config to popper.js with `popperConfig`', () => {
111
      fixtureEl.innerHTML = '<a href="#" rel="tooltip">'
112
113
114
115
116
117
118
119
120
121
122
123

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        popperConfig: {
          placement: 'left'
        }
      })

      const popperConfig = tooltip._getPopperConfig('top')

      expect(popperConfig.placement).toEqual('left')
    })
Johann-S's avatar
Johann-S committed
124
125
126
127
  })

  describe('enable', () => {
    it('should enable a tooltip', done => {
128
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.enable()

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeDefined()
        done()
      })

      tooltip.show()
    })
  })

  describe('disable', () => {
    it('should disable tooltip', done => {
146
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.disable()

      tooltipEl.addEventListener('show.bs.tooltip', () => {
        throw new Error('should not show a disabled tooltip')
      })

      tooltip.show()

      setTimeout(() => {
        expect().nothing()
        done()
      }, 10)
    })
  })

  describe('toggleEnabled', () => {
    it('should toggle enabled', () => {
168
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      expect(tooltip._isEnabled).toEqual(true)

      tooltip.toggleEnabled()

      expect(tooltip._isEnabled).toEqual(false)
    })
  })

  describe('toggle', () => {
    it('should do nothing if disabled', done => {
183
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.disable()

      tooltipEl.addEventListener('show.bs.tooltip', () => {
        throw new Error('should not show a disabled tooltip')
      })

      tooltip.toggle()

      setTimeout(() => {
        expect().nothing()
        done()
      }, 10)
    })

    it('should show a tooltip', done => {
203
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
204
205
206
207
208
209
210
211
212
213
214
215
216

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeDefined()
        done()
      })

      tooltip.toggle()
    })

    it('should call toggle and show the tooltip when trigger is "click"', done => {
217
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        trigger: 'click'
      })

      spyOn(tooltip, 'toggle').and.callThrough()

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(tooltip.toggle).toHaveBeenCalled()
        done()
      })

      tooltipEl.click()
    })

    it('should hide a tooltip', done => {
235
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        tooltip.toggle()
      })

      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeNull()
        done()
      })

      tooltip.toggle()
    })

    it('should call toggle and hide the tooltip when trigger is "click"', done => {
253
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        trigger: 'click'
      })

      spyOn(tooltip, 'toggle').and.callThrough()

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        tooltipEl.click()
      })

      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        expect(tooltip.toggle).toHaveBeenCalled()
        done()
      })

      tooltipEl.click()
    })
  })

  describe('dispose', () => {
    it('should destroy a tooltip', () => {
277
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
278
279
280
281

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

282
      expect(Tooltip.getInstance(tooltipEl)).toEqual(tooltip)
Johann-S's avatar
Johann-S committed
283
284
285

      tooltip.dispose()

286
      expect(Tooltip.getInstance(tooltipEl)).toEqual(null)
Johann-S's avatar
Johann-S committed
287
288
289
    })

    it('should destroy a tooltip and remove it from the dom', done => {
290
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeDefined()

        tooltip.dispose()

        expect(document.querySelector('.tooltip')).toBeNull()
        done()
      })

      tooltip.show()
    })
  })

  describe('show', () => {
    it('should show a tooltip', done => {
310
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        const tooltipShown = document.querySelector('.tooltip')

        expect(tooltipShown).toBeDefined()
        expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
        expect(tooltipShown.getAttribute('id').indexOf('tooltip') !== -1).toEqual(true)
        done()
      })

      tooltip.show()
    })

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    it('should show a tooltip when hovering a children element', done => {
      fixtureEl.innerHTML =
        '<a href="#" rel="tooltip" title="Another tooltip">' +
          '<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">' +
            '<rect width="100%" fill="#563d7c"/>' +
            '<circle cx="50" cy="50" r="30" fill="#fff"/>' +
          '</svg>' +
        '</a>'

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      spyOn(tooltip, 'show')

      tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))

      setTimeout(() => {
        expect(tooltip.show).toHaveBeenCalled()
        done()
      }, 0)
    })

Johann-S's avatar
Johann-S committed
349
    it('should show a tooltip on mobile', done => {
350
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)
      document.documentElement.ontouchstart = noop

      spyOn(EventHandler, 'on')

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).not.toBeNull()
        expect(EventHandler.on).toHaveBeenCalled()
        document.documentElement.ontouchstart = undefined
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip relative to placement option', done => {
369
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        placement: 'bottom'
      })

      tooltipEl.addEventListener('inserted.bs.tooltip', () => {
        expect(tooltip.getTipElement().classList.contains('bs-tooltip-bottom')).toEqual(true)
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        const tooltipShown = document.querySelector('.tooltip')

        expect(tooltipShown.classList.contains('bs-tooltip-bottom')).toEqual(true)
        done()
      })

      tooltip.show()
    })

    it('should not error when trying to show a tooltip that has been removed from the dom', done => {
391
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      const firstCallback = () => {
        tooltipEl.removeEventListener('shown.bs.tooltip', firstCallback)
        let tooltipShown = document.querySelector('.tooltip')

        tooltipShown.parentNode.removeChild(tooltipShown)

        tooltipEl.addEventListener('shown.bs.tooltip', () => {
          tooltipShown = document.querySelector('.tooltip')

          expect(tooltipShown).not.toBeNull()
          done()
        })

        tooltip.show()
      }

      tooltipEl.addEventListener('shown.bs.tooltip', firstCallback)

      tooltip.show()
    })

    it('should show a tooltip with a dom element container', done => {
418
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        container: fixtureEl
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip with a jquery element container', done => {
434
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        container: {
          0: fixtureEl,
          jquery: 'jQuery'
        }
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip with a selector in container', done => {
453
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        container: '#fixture'
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip with placement as a function', done => {
469
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

      const spy = jasmine.createSpy('placement').and.returnValue('top')
      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        placement: spy
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeDefined()
        expect(spy).toHaveBeenCalled()
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip with offset as a function', done => {
487
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

      const spy = jasmine.createSpy('offset').and.returnValue({})
      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        offset: spy
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeDefined()
        expect(spy).toHaveBeenCalled()
        done()
      })

      tooltip.show()
    })

    it('should show a tooltip without the animation', done => {
505
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        animation: false
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        const tip = document.querySelector('.tooltip')

        expect(tip).toBeDefined()
        expect(tip.classList.contains('fade')).toEqual(false)
        done()
      })

      tooltip.show()
    })

    it('should throw an error the element is not visible', () => {
524
      fixtureEl.innerHTML = '<a href="#" style="display: none" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
525
526
527
528
529
530
531
532
533
534
535
536

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      try {
        tooltip.show()
      } catch (error) {
        expect(error.message).toEqual('Please use show on visible elements')
      }
    })

    it('should not show a tooltip if show.bs.tooltip is prevented', done => {
537
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      const expectedDone = () => {
        setTimeout(() => {
          expect(document.querySelector('.tooltip')).toBeNull()
          done()
        }, 10)
      }

      tooltipEl.addEventListener('show.bs.tooltip', ev => {
        ev.preventDefault()
        expectedDone()
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        throw new Error('Tooltip should not be shown')
      })

      tooltip.show()
    })

    it('should show tooltip if leave event hasn\'t occurred before delay expires', done => {
562
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        delay: 150
      })

      spyOn(tooltip, 'show')

      setTimeout(() => {
        expect(tooltip.show).not.toHaveBeenCalled()
      }, 100)

      setTimeout(() => {
        expect(tooltip.show).toHaveBeenCalled()
        done()
      }, 200)

      tooltipEl.dispatchEvent(createEvent('mouseover'))
    })

    it('should not show tooltip if leave event occurs before delay expires', done => {
584
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        delay: 150
      })

      spyOn(tooltip, 'show')

      setTimeout(() => {
        expect(tooltip.show).not.toHaveBeenCalled()
        tooltipEl.dispatchEvent(createEvent('mouseover'))
      }, 100)

      setTimeout(() => {
        expect(tooltip.show).toHaveBeenCalled()
        expect(document.querySelectorAll('.tooltip').length).toEqual(0)
        done()
      }, 200)

      tooltipEl.dispatchEvent(createEvent('mouseover'))
    })

    it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', done => {
608
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        delay: {
          show: 0,
          hide: 150
        }
      })

      setTimeout(() => {
        expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
        tooltipEl.dispatchEvent(createEvent('mouseout'))

        setTimeout(() => {
          expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
          tooltipEl.dispatchEvent(createEvent('mouseover'))
        }, 100)

        setTimeout(() => {
          expect(tooltip.getTipElement().classList.contains('show')).toEqual(true)
          done()
        }, 200)
      }, 0)

      tooltipEl.dispatchEvent(createEvent('mouseover'))
    })
  })

  describe('hide', () => {
    it('should hide a tooltip', done => {
639
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeNull()
        expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
        done()
      })

      tooltip.show()
    })

    it('should hide a tooltip on mobile', done => {
655
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        document.documentElement.ontouchstart = noop
        spyOn(EventHandler, 'off')
        tooltip.hide()
      })

      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeNull()
        expect(EventHandler.off).toHaveBeenCalled()
        document.documentElement.ontouchstart = undefined
        done()
      })

      tooltip.show()
    })

    it('should hide a tooltip without animation', done => {
677
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        animation: false
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        expect(document.querySelector('.tooltip')).toBeNull()
        expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
        done()
      })

      tooltip.show()
    })

    it('should not hide a tooltip if hide event is prevented', done => {
695
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719

      const assertDone = () => {
        setTimeout(() => {
          expect(document.querySelector('.tooltip')).not.toBeNull()
          done()
        }, 20)
      }

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        animation: false
      })

      tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
      tooltipEl.addEventListener('hide.bs.tooltip', event => {
        event.preventDefault()
        assertDone()
      })
      tooltipEl.addEventListener('hidden.bs.tooltip', () => {
        throw new Error('should not trigger hidden event')
      })

      tooltip.show()
    })
720
721
722
723
724
725
726
727
728
729
730
731
732
733

    it('should not throw error running hide if popper hasn\'t been shown', () => {
      fixtureEl.innerHTML = '<div></div>'

      const div = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(div)

      try {
        tooltip.hide()
        expect().nothing()
      } catch {
        throw new Error('should not throw error')
      }
    })
Johann-S's avatar
Johann-S committed
734
735
736
737
  })

  describe('update', () => {
    it('should call popper schedule update', done => {
738
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltipEl.addEventListener('shown.bs.tooltip', () => {
        spyOn(tooltip._popper, 'scheduleUpdate')

        tooltip.update()

        expect(tooltip._popper.scheduleUpdate).toHaveBeenCalled()
        done()
      })

      tooltip.show()
    })

    it('should do nothing if the tooltip is not shown', () => {
756
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
757
758
759
760
761
762
763
764
765
766
767

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.update()
      expect().nothing()
    })
  })

  describe('isWithContent', () => {
    it('should return true if there is content', () => {
768
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
769
770
771
772
773
774
775
776

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      expect(tooltip.isWithContent()).toEqual(true)
    })

    it('should return false if there is no content', () => {
777
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="">'
Johann-S's avatar
Johann-S committed
778
779
780
781
782
783
784
785
786
787

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      expect(tooltip.isWithContent()).toEqual(false)
    })
  })

  describe('getTipElement', () => {
    it('should create the tip element and return it', () => {
788
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
789
790
791
792
793
794
795
796
797
798
799

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      spyOn(document, 'createElement').and.callThrough()

      expect(tooltip.getTipElement()).toBeDefined()
      expect(document.createElement).toHaveBeenCalled()
    })

    it('should return the created tip element', () => {
800
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      const spy = spyOn(document, 'createElement').and.callThrough()

      expect(tooltip.getTipElement()).toBeDefined()
      expect(spy).toHaveBeenCalled()

      spy.calls.reset()

      expect(tooltip.getTipElement()).toBeDefined()
      expect(spy).not.toHaveBeenCalled()
    })
  })

  describe('setContent', () => {
    it('should set tip content', () => {
819
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.setContent()

      const tip = tooltip.getTipElement()

      expect(tip.classList.contains('show')).toEqual(false)
      expect(tip.classList.contains('fade')).toEqual(false)
      expect(tip.querySelector('.tooltip-inner').textContent).toEqual('Another tooltip')
    })
  })

  describe('setElementContent', () => {
    it('should do nothing if the element is null', () => {
836
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
837
838
839
840
841
842
843
844
845
846

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.setElementContent(null, null)
      expect().nothing()
    })

    it('should add the content as a child of the element', () => {
      fixtureEl.innerHTML = [
847
        '<a href="#" rel="tooltip" title="Another tooltip">',
Johann-S's avatar
Johann-S committed
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
        '<div id="childContent"></div>'
      ].join('')

      const tooltipEl = fixtureEl.querySelector('a')
      const childContent = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(tooltipEl, {
        html: true
      })

      tooltip.setElementContent(tooltip.getTipElement(), childContent)

      expect(childContent.parentNode).toEqual(tooltip.getTipElement())
    })

    it('should do nothing if the content is a child of the element', () => {
      fixtureEl.innerHTML = [
864
        '<a href="#" rel="tooltip" title="Another tooltip">',
Johann-S's avatar
Johann-S committed
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
        '<div id="childContent"></div>'
      ].join('')

      const tooltipEl = fixtureEl.querySelector('a')
      const childContent = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(tooltipEl, {
        html: true
      })

      tooltip.getTipElement().appendChild(childContent)
      tooltip.setElementContent(tooltip.getTipElement(), childContent)

      expect().nothing()
    })

    it('should add the content as a child of the element for jQuery elements', () => {
      fixtureEl.innerHTML = [
882
        '<a href="#" rel="tooltip" title="Another tooltip">',
Johann-S's avatar
Johann-S committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
        '<div id="childContent"></div>'
      ].join('')

      const tooltipEl = fixtureEl.querySelector('a')
      const childContent = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(tooltipEl, {
        html: true
      })

      tooltip.setElementContent(tooltip.getTipElement(), { 0: childContent, jquery: 'jQuery' })

      expect(childContent.parentNode).toEqual(tooltip.getTipElement())
    })

    it('should add the child text content in the element', () => {
      fixtureEl.innerHTML = [
899
        '<a href="#" rel="tooltip" title="Another tooltip">',
Johann-S's avatar
Johann-S committed
900
901
902
903
904
905
906
907
908
909
910
911
912
        '<div id="childContent">Tooltip</div>'
      ].join('')

      const tooltipEl = fixtureEl.querySelector('a')
      const childContent = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.setElementContent(tooltip.getTipElement(), childContent)

      expect(childContent.textContent).toEqual(tooltip.getTipElement().textContent)
    })

    it('should add html without sanitize it', () => {
913
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
914
915
916
917
918
919
920
921
922
923
924
925
926

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        sanitize: false,
        html: true
      })

      tooltip.setElementContent(tooltip.getTipElement(), '<div id="childContent">Tooltip</div>')

      expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
    })

    it('should add html sanitized', () => {
927
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        html: true
      })

      tooltip.setElementContent(tooltip.getTipElement(), [
        '<div id="childContent">',
        ' <button type="button">test btn</button>',
        '</div>'
      ].join(''))

      expect(tooltip.getTipElement().querySelector('div').id).toEqual('childContent')
      expect(tooltip.getTipElement().querySelector('button')).toEqual(null)
    })

    it('should add text content', () => {
945
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
946
947
948
949
950
951

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      tooltip.setElementContent(tooltip.getTipElement(), 'test')

952
      expect(tooltip.getTipElement().textContent).toEqual('test')
Johann-S's avatar
Johann-S committed
953
954
955
956
957
    })
  })

  describe('getTitle', () => {
    it('should return the title', () => {
958
      fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
Johann-S's avatar
Johann-S committed
959
960
961
962
963
964
965
966

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl)

      expect(tooltip.getTitle()).toEqual('Another tooltip')
    })

    it('should call title function', () => {
967
      fixtureEl.innerHTML = '<a href="#" rel="tooltip"></a>'
Johann-S's avatar
Johann-S committed
968
969
970
971
972
973
974
975
976
977

      const tooltipEl = fixtureEl.querySelector('a')
      const tooltip = new Tooltip(tooltipEl, {
        title: () => 'test'
      })

      expect(tooltip.getTitle()).toEqual('test')
    })
  })

978
  describe('jQueryInterface', () => {
Johann-S's avatar
Johann-S committed
979
980
981
982
983
    it('should create a tooltip', () => {
      fixtureEl.innerHTML = '<div></div>'

      const div = fixtureEl.querySelector('div')

984
      jQueryMock.fn.tooltip = Tooltip.jQueryInterface
Johann-S's avatar
Johann-S committed
985
986
987
988
      jQueryMock.elements = [div]

      jQueryMock.fn.tooltip.call(jQueryMock)

989
      expect(Tooltip.getInstance(div)).toBeDefined()
Johann-S's avatar
Johann-S committed
990
991
992
993
994
995
996
997
    })

    it('should not re create a tooltip', () => {
      fixtureEl.innerHTML = '<div></div>'

      const div = fixtureEl.querySelector('div')
      const tooltip = new Tooltip(div)

998
      jQueryMock.fn.tooltip = Tooltip.jQueryInterface
Johann-S's avatar
Johann-S committed
999
1000
      jQueryMock.elements = [div]

For faster browsing, not all history is shown. View entire blame