-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtooltip.spec.ts
More file actions
937 lines (775 loc) · 28.3 KB
/
tooltip.spec.ts
File metadata and controls
937 lines (775 loc) · 28.3 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
import {
elementUpdated,
expect,
fixture,
html,
nextFrame,
} from '@open-wc/testing';
import { type SinonFakeTimers, spy, useFakeTimers } from 'sinon';
import { defineComponents } from '../common/definitions/defineComponents.js';
import {
finishAnimationsFor,
simulateBlur,
simulateClick,
simulateFocus,
simulatePointerEnter,
simulatePointerLeave,
} from '../common/utils.spec.js';
import IgcTooltipComponent from './tooltip.js';
describe('Tooltip', () => {
let anchor: HTMLButtonElement;
let tooltip: IgcTooltipComponent;
let clock: SinonFakeTimers;
const DIFF_OPTIONS = {
ignoreAttributes: ['anchor'],
};
const endTick = (tick: number) => tick + 180;
const DEFAULT_SHOW_DELAY = 200;
const DEFAULT_HIDE_DELAY = 300;
before(() => {
defineComponents(IgcTooltipComponent);
});
async function showComplete(instance: IgcTooltipComponent = tooltip) {
finishAnimationsFor(instance.shadowRoot!);
await elementUpdated(instance);
await nextFrame();
}
async function hideComplete(instance: IgcTooltipComponent = tooltip) {
await elementUpdated(instance);
finishAnimationsFor(instance.shadowRoot!);
await nextFrame();
await nextFrame();
}
describe('Initialization Tests', () => {
beforeEach(async () => {
const container = await fixture(createDefaultTooltip());
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
});
it('is defined', () => {
expect(tooltip).to.exist;
});
it('is accessible', async () => {
await expect(tooltip).to.be.accessible();
await expect(tooltip).shadowDom.to.be.accessible();
});
it('is accessible in sticky mode', async () => {
tooltip.sticky = true;
tooltip.open = true;
await elementUpdated(tooltip);
await expect(tooltip).to.be.accessible();
await expect(tooltip).shadowDom.to.be.accessible();
});
it('is correctly initialized with its default component state', () => {
expect(tooltip.dir).to.be.empty;
expect(tooltip.open).to.be.false;
expect(tooltip.withArrow).to.be.false;
expect(tooltip.offset).to.equal(6);
expect(tooltip.placement).to.equal('bottom');
expect(tooltip.anchor).to.be.undefined;
expect(tooltip.showTriggers).to.equal('pointerenter');
expect(tooltip.hideTriggers).to.equal('pointerleave,click');
expect(tooltip.showDelay).to.equal(200);
expect(tooltip.hideDelay).to.equal(300);
expect(tooltip.message).to.equal('');
});
it('is correctly rendered both in shown/hidden states', async () => {
expect(tooltip.open).to.be.false;
expect(tooltip).dom.to.equal('<igc-tooltip>It works!</igc-tooltip>');
expect(tooltip).shadowDom.to.equal(
`<igc-popover
inert
flip
shift
>
<div part="base">
<slot></slot>
</div>
</igc-popover>`
);
tooltip.open = true;
await elementUpdated(tooltip);
expect(tooltip).dom.to.equal('<igc-tooltip open>It works!</igc-tooltip>');
expect(tooltip).shadowDom.to.equal(
`<igc-popover
flip
shift
open
>
<div part="base">
<slot></slot>
</div>
</igc-popover>`
);
});
it('is initially open on first render', async () => {
const container = await fixture(createDefaultTooltip(true));
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
expect(tooltip).to.exist;
expect(tooltip.open).to.be.true;
});
it('is initially open on first render with target', async () => {
const container = await fixture(createTooltipWithTarget(true));
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
expect(tooltip).to.exist;
expect(tooltip.open).to.be.true;
});
});
describe('Properties Tests', () => {
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setTimeout'] });
const container = await fixture(createTooltipWithTarget());
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
anchor = container.querySelector('button')!;
});
afterEach(() => {
clock.restore();
});
it('should set target via `anchor` property', async () => {
const template = html`
<div>
<button id="first">I will have a tooltip</button>
<button id="second">I will have a tooltip too</button>
<button id="third">Just happy to be here</button>
<igc-tooltip>I am a tooltip</igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
const first = document.querySelector('#first') as HTMLButtonElement;
const second = document.querySelector('#second') as HTMLButtonElement;
const third = document.querySelector('#third') as HTMLButtonElement;
// If no anchor is provided.
// Considers the first preceding sibling that is an element as the target.
simulatePointerEnter(third);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.false;
simulatePointerLeave(third);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
// By providing an IDREF
tooltip.anchor = first.id;
await elementUpdated(tooltip);
simulatePointerEnter(first);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulatePointerLeave(first);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
// By providing an Element
simulatePointerEnter(second);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.false;
tooltip.anchor = second;
await elementUpdated(tooltip);
simulatePointerEnter(second);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
});
it('should show/hide the arrow via the `withArrow` property', async () => {
expect(tooltip.withArrow).to.be.false;
expect(tooltip.renderRoot.querySelector('#arrow')).to.be.null;
tooltip.withArrow = true;
await elementUpdated(tooltip);
expect(tooltip.withArrow).to.be.true;
expect(tooltip.renderRoot.querySelector('#arrow')).not.to.be.null;
});
it('should show/hide the arrow via the `with-arrow` attribute', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip with-arrow>I am a tooltip</igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
expect(tooltip.withArrow).to.be.true;
expect(tooltip.renderRoot.querySelector('#arrow')).not.to.be.null;
});
it('should provide content via the `message` property', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip message="Hello"></igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
const defaultSlot =
tooltip.renderRoot.querySelector<HTMLSlotElement>('slot:not([name])');
const content1 = defaultSlot
?.assignedNodes({ flatten: true })
.map((x) => x.textContent);
expect(content1).to.include('Hello');
const message = 'New Message!';
tooltip.message = message;
await elementUpdated(tooltip);
const content2 = defaultSlot
?.assignedNodes({ flatten: true })
.map((x) => x.textContent);
expect(content2).to.include(message);
});
it('slotted content takes priority over the `message` property', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip message="Hello">
<button>Close</button>
</igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
const defaultSlot =
tooltip.renderRoot.querySelector<HTMLSlotElement>('slot:not([name])');
expect(defaultSlot).not.to.be.null;
expect(defaultSlot?.assignedElements()[0].matches('button')).to.be.true;
});
it('should apply simple-text class when using message property only', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip message="Simple text tooltip"></igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
await elementUpdated(tooltip);
const baseElement = tooltip.renderRoot.querySelector('[part~="base"]');
expect(baseElement).not.to.be.null;
expect(baseElement?.part.contains('simple-text')).to.be.true;
});
it('should not apply simple-text class when using custom content', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip>
<div>Custom content with complex structure</div>
</igc-tooltip>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
await elementUpdated(tooltip);
const baseElement = tooltip.renderRoot.querySelector('[part~="base"]');
expect(baseElement).not.to.be.null;
expect(baseElement?.part.contains('simple-text')).to.be.false;
});
it('should render a default close button when in `sticky` mode', async () => {
tooltip.sticky = true;
await elementUpdated(tooltip);
expect(tooltip).dom.to.equal(
'<igc-tooltip sticky>It works!</igc-tooltip>',
DIFF_OPTIONS
);
expect(tooltip).shadowDom.to.equal(
`<igc-popover
inert
flip
shift
>
<div part="base">
<slot></slot>
<slot name="close-button">
<igc-icon
aria-hidden="true"
collection="default"
name="input_clear"
></igc-icon>
</slot>
</div>
</igc-popover>`
);
});
it('should render custom content for close button when in `sticky` mode', async () => {
const template = html`
<div>
<button>Hover</button>
<igc-tooltip message="Hello"
><button slot="close-button">Close</button></igc-tooltip
>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
tooltip.sticky = true;
tooltip.open = true;
await elementUpdated(tooltip);
const defaultSlot =
tooltip.renderRoot.querySelector<HTMLSlotElement>('slot:not([name])');
const closeButtonSlot = tooltip.renderRoot.querySelector<HTMLSlotElement>(
'slot[name="close-button"]'
);
const defaultSlotContent = defaultSlot
?.assignedNodes({ flatten: true })
.map((x) => x.textContent);
const closeButtonContent = closeButtonSlot
?.assignedNodes({ flatten: true })
.map((x) => x.textContent);
expect(defaultSlotContent).to.include('Hello');
expect(closeButtonContent).to.include('Close');
expect(closeButtonSlot?.assignedElements()[0].matches('button')).to.be
.true;
});
it('hide triggers should not close the tooltip when in `sticky` mode', async () => {
tooltip.sticky = true;
await elementUpdated(tooltip);
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.true;
});
it('should close the tooltip when in `sticky` mode by clicking the default close button', async () => {
tooltip.sticky = true;
tooltip.open = true;
await elementUpdated(tooltip);
expect(tooltip.open).to.be.true;
const closeIcon = tooltip.shadowRoot!.querySelector('igc-icon')!;
simulateClick(closeIcon);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
it('should close the tooltip when in `sticky` mode by pressing the `Esc` key', async () => {
tooltip.sticky = true;
tooltip.open = true;
await elementUpdated(tooltip);
expect(tooltip.open).to.be.true;
document.documentElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
composed: true,
})
);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
});
describe('Methods` Tests', () => {
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setTimeout'] });
const container = await fixture(createTooltipWithTarget());
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
anchor = container.querySelector('button')!;
});
afterEach(() => {
clock.restore();
});
it('calls `show` and `hide` methods successfully and returns proper values', async () => {
expect(tooltip.open).to.be.false;
// hide tooltip when already hidden
let animation = await tooltip.hide();
expect(animation).to.be.false;
expect(tooltip.open).to.be.false;
expect(tooltip).dom.to.equal(
'<igc-tooltip>It works!</igc-tooltip>',
DIFF_OPTIONS
);
// show tooltip when hidden
animation = await tooltip.show();
expect(animation).to.be.true;
expect(tooltip.open).to.be.true;
expect(tooltip).dom.to.equal(
'<igc-tooltip open>It works!</igc-tooltip>',
DIFF_OPTIONS
);
// show tooltip when already shown
animation = await tooltip.show();
expect(animation).to.be.false;
expect(tooltip.open).to.be.true;
expect(tooltip).dom.to.equal(
'<igc-tooltip open>It works!</igc-tooltip>',
DIFF_OPTIONS
);
// hide tooltip when shown
animation = await tooltip.hide();
expect(animation).to.be.true;
expect(tooltip.open).to.be.false;
expect(tooltip).dom.to.equal(
'<igc-tooltip>It works!</igc-tooltip>',
DIFF_OPTIONS
);
});
it('calls `toggle` method successfully and returns proper values', async () => {
expect(tooltip.open).to.be.false;
let animation = await tooltip.toggle();
expect(animation).to.be.true;
expect(tooltip.open).to.be.true;
expect(tooltip).dom.to.equal(
'<igc-tooltip open>It works!</igc-tooltip>',
DIFF_OPTIONS
);
animation = await tooltip.toggle();
expect(animation).to.be.true;
expect(tooltip.open).to.be.false;
expect(tooltip).dom.to.equal(
'<igc-tooltip>It works!</igc-tooltip>',
DIFF_OPTIONS
);
});
it('calls `show` with a new target, switches anchor, and resets anchor on hide', async () => {
const buttons = Array.from(
tooltip.parentElement!.querySelectorAll('button')
);
const [defaultAnchor, transientAnchor] = buttons;
let result = await tooltip.show(defaultAnchor);
expect(result).to.be.true;
expect(tooltip.open).to.be.true;
result = await tooltip.show(transientAnchor);
expect(result).to.be.true;
expect(tooltip.open).to.be.true;
result = await tooltip.hide();
expect(result).to.be.true;
expect(tooltip.open).to.be.false;
// the transient anchor should not reopen the tooltip once its hidden
simulatePointerEnter(transientAnchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.false;
simulatePointerEnter(defaultAnchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
});
it('should be able to pass and IDREF to `show` method', async () => {
const eventSpy = spy(tooltip, 'emitEvent');
const [_, transientAnchor] = Array.from(
tooltip.parentElement!.querySelectorAll('button')
);
transientAnchor.id = 'custom-target';
const result = await tooltip.show('custom-target');
expect(result).to.be.true;
expect(tooltip.open).to.be.true;
expect(eventSpy.callCount).to.equal(0);
});
it('should correctly handle open state and events between default and transient anchors', async () => {
const eventSpy = spy(tooltip, 'emitEvent');
const [defaultAnchor, transientAnchor] = Array.from(
tooltip.parentElement!.querySelectorAll('button')
);
const result = await tooltip.show(transientAnchor);
expect(result).to.be.true;
expect(tooltip.open).to.be.true;
expect(eventSpy.callCount).to.equal(0);
simulatePointerEnter(defaultAnchor);
// Trigger on the initial default anchor. Tooltip must be hidden.
expect(tooltip.open).to.be.false;
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
expect(eventSpy).calledWith('igcOpening', { cancelable: true });
expect(eventSpy).calledWith('igcOpened', { cancelable: false });
});
});
describe('Behaviors', () => {
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
const container = await fixture(createTooltipWithTarget());
anchor = container.querySelector('button')!;
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
});
afterEach(() => {
clock.restore();
});
it('default triggers', async () => {
expect(tooltip.showTriggers).to.equal('pointerenter');
expect(tooltip.hideTriggers).to.equal('pointerleave,click');
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
it('custom triggers via property', async () => {
tooltip.showTriggers = 'focus, pointerenter';
tooltip.hideTriggers = 'blur, click';
simulateFocus(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulateBlur(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulateClick(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
it('custom triggers via attribute', async () => {
const template = html`
<div>
<button id="anchor1">Hover over me</button>
<igc-tooltip
anchor="anchor1"
show-triggers="focus,click"
hide-triggers="blur"
>I am a tooltip</igc-tooltip
>
</div>
`;
const container = await fixture(template);
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
anchor = container.querySelector('button')!;
simulateFocus(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulateBlur(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
simulateClick(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
simulateBlur(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
it('pointerenter over tooltip prevents hiding and pointerleave triggers hiding', async () => {
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete();
expect(tooltip.open).to.be.true;
// Move cursor from anchor to tooltip
simulatePointerLeave(anchor);
await nextFrame();
simulatePointerEnter(tooltip);
await nextFrame();
expect(tooltip.open).to.be.true;
// Move cursor outside the tooltip
simulatePointerLeave(tooltip);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete();
expect(tooltip.open).to.be.false;
});
it('should show/hide the tooltip based on `showDelay` and `hideDelay`', async () => {
tooltip.showDelay = tooltip.hideDelay = 400;
simulatePointerEnter(anchor);
await clock.tickAsync(399);
expect(tooltip.open).to.be.false;
await clock.tickAsync(1);
await showComplete(tooltip);
expect(tooltip.open).to.be.true;
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(399));
expect(tooltip.open).to.be.true;
await clock.tickAsync(1);
await hideComplete(tooltip);
expect(tooltip.open).to.be.false;
});
it('prevents tooltip from showing when clicking the target - #1828', async () => {
const eventSpy = spy(tooltip, 'emitEvent');
tooltip.showTriggers = 'pointerenter';
tooltip.hideTriggers = 'pointerleave';
simulatePointerEnter(anchor);
await clock.tickAsync(199);
expect(tooltip.open).to.be.false;
// Click on the target before the tooltip is shown
simulateClick(anchor);
await clock.tickAsync(1);
await showComplete(tooltip);
expect(tooltip.open).to.be.false;
expect(eventSpy.callCount).to.equal(1);
eventSpy.resetHistory();
// Does not prevent showing when showTriggers includes 'click'
tooltip.showTriggers = 'click';
simulateClick(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete(tooltip);
expect(tooltip.open).to.be.true;
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(tooltip);
expect(tooltip.open).to.be.false;
expect(eventSpy.callCount).to.equal(4);
});
});
describe('Events', () => {
let eventSpy: ReturnType<typeof spy>;
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setTimeout'] });
const container = await fixture(createTooltipWithTarget());
tooltip = container.querySelector(IgcTooltipComponent.tagName)!;
anchor = container.querySelector('button')!;
eventSpy = spy(tooltip, 'emitEvent');
});
afterEach(() => {
clock.restore();
});
const verifyStateAndEventSequence = (
state: { open: boolean } = { open: false }
) => {
expect(tooltip.open).to.equal(state.open);
expect(eventSpy.callCount).to.equal(2);
expect(eventSpy.firstCall).calledWith(
state.open ? 'igcOpening' : 'igcClosing',
{ cancelable: true }
);
expect(eventSpy.secondCall).calledWith(
state.open ? 'igcOpened' : 'igcClosed',
{ cancelable: false }
);
};
it('events are correctly emitted on user interaction', async () => {
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete(tooltip);
verifyStateAndEventSequence({ open: true });
eventSpy.resetHistory();
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(tooltip);
verifyStateAndEventSequence({ open: false });
});
it('can cancel -ing events', async () => {
tooltip.addEventListener('igcOpening', (e) => e.preventDefault(), {
once: true,
});
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete(tooltip);
expect(tooltip.open).to.be.false;
expect(eventSpy).calledOnceWith('igcOpening', { cancelable: true });
eventSpy.resetHistory();
tooltip.open = true;
await elementUpdated(tooltip);
tooltip.addEventListener('igcClosing', (e) => e.preventDefault(), {
once: true,
});
simulatePointerLeave(anchor);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(tooltip);
expect(tooltip.open).to.be.true;
expect(eventSpy).calledOnceWith('igcClosing', { cancelable: true });
});
it('fires `igcClosed` when tooltip is hidden via Escape key', async () => {
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete(tooltip);
eventSpy.resetHistory();
document.documentElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
composed: true,
})
);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(tooltip);
expect(tooltip.open).to.be.false;
expect(eventSpy.callCount).to.equal(1);
expect(eventSpy.firstCall).calledWith('igcClosed', { cancelable: false });
});
});
describe('Keyboard interactions', () => {
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setTimeout'] });
});
afterEach(() => {
clock.restore();
});
it('pressing Escape in an active page hides the tooltip', async () => {
const container = await fixture(createTooltips());
const [anchor, _] = container.querySelectorAll('button');
const [tooltip, __] = container.querySelectorAll(
IgcTooltipComponent.tagName
);
simulatePointerEnter(anchor);
await clock.tickAsync(DEFAULT_SHOW_DELAY);
await showComplete(tooltip);
expect(tooltip.open).to.be.true;
document.documentElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
composed: true,
})
);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(tooltip);
expect(tooltip.open).to.be.false;
});
it('pressing Escape in an active page with multiple opened tooltips hides the last shown', async () => {
const container = await fixture(createTooltips());
const [first, last] = container.querySelectorAll(
IgcTooltipComponent.tagName
);
first.show();
last.show();
expect(first.open).to.be.true;
expect(last.open).to.be.true;
document.documentElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
composed: true,
})
);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(first);
await hideComplete(last);
expect(last.open).to.be.false;
expect(first.open).to.be.true;
document.documentElement.dispatchEvent(
new KeyboardEvent('keydown', {
key: 'Escape',
bubbles: true,
composed: true,
})
);
await clock.tickAsync(endTick(DEFAULT_HIDE_DELAY));
await hideComplete(first);
await hideComplete(last);
expect(last.open).to.be.false;
expect(first.open).to.be.false;
});
});
});
function createDefaultTooltip(isOpen = false) {
return html`
<div>
<button>Hover over me</button>
<igc-tooltip ?open=${isOpen}>It works!</igc-tooltip>
</div>
`;
}
function createTooltipWithTarget(isOpen = false) {
return html`
<div>
<button id="target">I have a tooltip</button>
<button>I don't have a tooltip</button>
<igc-tooltip anchor="target" ?open=${isOpen}>It works!</igc-tooltip>
</div>
`;
}
function createTooltips() {
return html`
<div>
<button id="firstTarget">Target 1</button>
<igc-tooltip anchor="firstTarget">First</igc-tooltip>
<button id="secondTarget">Target 2</button>
<igc-tooltip anchor="secondTarget">Second</igc-tooltip>
</div>
`;
}