forked from angular/components
/
tooltip.spec.ts
1687 lines (1384 loc) · 58.6 KB
/
tooltip.spec.ts
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {FocusMonitor} from '@angular/cdk/a11y';
import {Direction, Directionality} from '@angular/cdk/bidi';
import {ESCAPE} from '@angular/cdk/keycodes';
import {CdkScrollable, OverlayContainer, OverlayModule} from '@angular/cdk/overlay';
import {Platform} from '@angular/cdk/platform';
import {
createFakeEvent,
createKeyboardEvent,
createMouseEvent,
dispatchEvent,
dispatchFakeEvent,
dispatchKeyboardEvent,
dispatchMouseEvent,
patchElementFocus,
} from '../../cdk/testing/private';
import {
ChangeDetectionStrategy,
Component,
DebugElement,
ElementRef,
NgZone,
ViewChild,
} from '@angular/core';
import {
ComponentFixture,
fakeAsync,
flush,
inject,
TestBed,
tick,
waitForAsync,
} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {Subject} from 'rxjs';
import {
MAT_TOOLTIP_DEFAULT_OPTIONS,
MatTooltip,
MatTooltipModule,
SCROLL_THROTTLE_MS,
TooltipPosition,
TooltipTouchGestures,
} from './index';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
const initialTooltipMessage = 'initial tooltip message';
describe('MDC-based MatTooltip', () => {
let overlayContainerElement: HTMLElement;
let dir: {value: Direction; change: Subject<Direction>};
let platform: Platform;
let focusMonitor: FocusMonitor;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [
BasicTooltipDemo,
ScrollableTooltipDemo,
OnPushTooltipDemo,
DynamicTooltipsDemo,
TooltipOnTextFields,
TooltipOnDraggableElement,
DataBoundAriaLabelTooltip,
],
providers: [
{
provide: Directionality,
useFactory: () => {
return (dir = {value: 'ltr', change: new Subject()});
},
},
],
});
TestBed.compileComponents();
inject(
[OverlayContainer, FocusMonitor, Platform],
(oc: OverlayContainer, fm: FocusMonitor, pl: Platform) => {
overlayContainerElement = oc.getContainerElement();
focusMonitor = fm;
platform = pl;
},
)();
}));
describe('basic usage', () => {
let fixture: ComponentFixture<BasicTooltipDemo>;
let buttonDebugElement: DebugElement;
let buttonElement: HTMLButtonElement;
let tooltipDirective: MatTooltip;
beforeEach(() => {
fixture = TestBed.createComponent(BasicTooltipDemo);
fixture.detectChanges();
buttonDebugElement = fixture.debugElement.query(By.css('button'))!;
buttonElement = <HTMLButtonElement>buttonDebugElement.nativeElement;
tooltipDirective = buttonDebugElement.injector.get<MatTooltip>(MatTooltip);
});
it('should show and hide the tooltip', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show();
tick(0); // Tick for the show delay (default is 0)
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
// Wait until animation has finished
finishCurrentTooltipAnimation(overlayContainerElement, true);
// Make sure tooltip is shown to the user and animation has finished.
const tooltipElement = overlayContainerElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;
expect(tooltipElement instanceof HTMLElement).toBe(true);
expect(tooltipElement.classList).toContain('mat-mdc-tooltip-show');
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
// After hide is called, a timeout delay is created that will to hide the tooltip.
const tooltipDelay = 1000;
tooltipDirective.hide(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
// After the tooltip delay elapses, expect that the tooltip is not visible.
tick(tooltipDelay);
fixture.detectChanges();
expect(tooltipDirective._isTooltipVisible()).toBe(false);
// On animation complete, should expect that the tooltip has been detached.
finishCurrentTooltipAnimation(overlayContainerElement, false);
assertTooltipInstance(tooltipDirective, false);
}));
it('should be able to re-open a tooltip if it was closed by detaching the overlay', fakeAsync(() => {
tooltipDirective.show();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
tooltipDirective._overlayRef!.detach();
tick(0);
fixture.detectChanges();
expect(tooltipDirective._isTooltipVisible()).toBe(false);
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show();
tick(0);
finishCurrentTooltipAnimation(overlayContainerElement, true);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));
it('should show with delay', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
const tooltipDelay = 1000;
tooltipDirective.show(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('');
tick(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
}));
it('should be able to override the default show and hide delays', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [BasicTooltipDemo],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {showDelay: 1337, hideDelay: 7331},
},
],
})
.compileComponents();
fixture = TestBed.createComponent(BasicTooltipDemo);
fixture.detectChanges();
tooltipDirective = fixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
tooltipDirective.show();
fixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(false);
tick(1337);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
tooltipDirective.hide();
fixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);
tick(7331);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));
it('should be able to override the default position', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [TooltipDemoWithoutPositionBinding],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {position: 'right'},
},
],
})
.compileComponents();
const newFixture = TestBed.createComponent(TooltipDemoWithoutPositionBinding);
newFixture.detectChanges();
tooltipDirective = newFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
tooltipDirective.show();
newFixture.detectChanges();
tick();
expect(tooltipDirective.position).toBe('right');
expect(tooltipDirective._getOverlayPosition().main.overlayX).toBe('start');
expect(tooltipDirective._getOverlayPosition().fallback.overlayX).toBe('end');
}));
it('should position on the bottom-left by default', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
})
.compileComponents();
const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();
dispatchMouseEvent(button, 'mouseenter', triggerRect.right - 100, triggerRect.top + 100);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBeLessThan(
triggerRect.right - 250,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBeGreaterThanOrEqual(
triggerRect.bottom,
);
}));
it('should be able to override the default positionAtOrigin', fakeAsync(() => {
// We don't bind mouse events on mobile devices.
if (platform.IOS || platform.ANDROID) {
return;
}
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule],
declarations: [WideTooltipDemo],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {positionAtOrigin: true},
},
],
})
.compileComponents();
const wideFixture = TestBed.createComponent(WideTooltipDemo);
wideFixture.detectChanges();
tooltipDirective = wideFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
const button: HTMLButtonElement = wideFixture.nativeElement.querySelector('button');
const triggerRect = button.getBoundingClientRect();
dispatchMouseEvent(button, 'mouseenter', triggerRect.right - 100, triggerRect.top + 100);
wideFixture.detectChanges();
tick();
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(tooltipDirective._overlayRef!.overlayElement.offsetLeft).toBe(
triggerRect.right - 100 - 20,
);
expect(tooltipDirective._overlayRef!.overlayElement.offsetTop).toBe(triggerRect.top + 100);
}));
it('should be able to disable tooltip interactivity', fakeAsync(() => {
TestBed.resetTestingModule()
.configureTestingModule({
imports: [MatTooltipModule, OverlayModule, NoopAnimationsModule],
declarations: [TooltipDemoWithoutPositionBinding],
providers: [
{
provide: MAT_TOOLTIP_DEFAULT_OPTIONS,
useValue: {disableTooltipInteractivity: true},
},
],
})
.compileComponents();
const newFixture = TestBed.createComponent(TooltipDemoWithoutPositionBinding);
newFixture.detectChanges();
tooltipDirective = newFixture.debugElement
.query(By.css('button'))!
.injector.get<MatTooltip>(MatTooltip);
tooltipDirective.show();
newFixture.detectChanges();
tick();
expect(tooltipDirective._overlayRef?.overlayElement.classList).toContain(
'mat-mdc-tooltip-panel-non-interactive',
);
}));
it('should set a css class on the overlay panel element', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
tick(0);
const overlayRef = tooltipDirective._overlayRef;
expect(!!overlayRef).toBeTruthy();
expect(overlayRef!.overlayElement.classList)
.withContext('Expected the overlay panel element to have the tooltip panel class set.')
.toContain('mat-mdc-tooltip-panel');
}));
it('should not show if disabled', fakeAsync(() => {
// Test that disabling the tooltip will not set the tooltip visible
tooltipDirective.disabled = true;
tooltipDirective.show();
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
// Test to make sure setting disabled to false will show the tooltip
// Sanity check to make sure everything was correct before (detectChanges, tick)
tooltipDirective.disabled = false;
tooltipDirective.show();
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));
it('should hide if disabled while visible', fakeAsync(() => {
// Display the tooltip with a timeout before hiding.
tooltipDirective.hideDelay = 1000;
tooltipDirective.show();
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
// Set tooltip to be disabled and verify that the tooltip hides.
tooltipDirective.disabled = true;
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));
it('should hide if the message is cleared while the tooltip is open', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.componentInstance.message = '';
fixture.detectChanges();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));
it('should not show if hide is called before delay finishes', waitForAsync(() => {
assertTooltipInstance(tooltipDirective, false);
const tooltipDelay = 1000;
tooltipDirective.show(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain('');
tooltipDirective.hide();
fixture.whenStable().then(() => {
expect(tooltipDirective._isTooltipVisible()).toBe(false);
});
}));
it('should not show tooltip if message is not present or empty', () => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.message = undefined!;
fixture.detectChanges();
tooltipDirective.show();
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.message = null!;
fixture.detectChanges();
tooltipDirective.show();
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.message = '';
fixture.detectChanges();
tooltipDirective.show();
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.message = ' ';
fixture.detectChanges();
tooltipDirective.show();
assertTooltipInstance(tooltipDirective, false);
});
it('should not follow through with hide if show is called after', fakeAsync(() => {
tooltipDirective.show();
tick(0); // Tick for the show delay (default is 0)
expect(tooltipDirective._isTooltipVisible()).toBe(true);
// After hide called, a timeout delay is created that will to hide the tooltip.
const tooltipDelay = 1000;
tooltipDirective.hide(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
// Before delay time has passed, call show which should cancel intent to hide tooltip.
tooltipDirective.show();
tick(tooltipDelay);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
}));
it('should be able to update the tooltip position while open', fakeAsync(() => {
tooltipDirective.position = 'below';
tooltipDirective.show();
tick();
assertTooltipInstance(tooltipDirective, true);
tooltipDirective.position = 'above';
spyOn(tooltipDirective._overlayRef!, 'updatePosition').and.callThrough();
fixture.detectChanges();
tick();
assertTooltipInstance(tooltipDirective, true);
expect(tooltipDirective._overlayRef!.updatePosition).toHaveBeenCalled();
}));
it('should update the tooltip position when the directionality changes', fakeAsync(() => {
tooltipDirective.position = 'right';
tooltipDirective.show();
tick();
assertTooltipInstance(tooltipDirective, true);
const spy = spyOn(tooltipDirective as any, '_updatePosition').and.callThrough();
dir.change.next('rtl');
assertTooltipInstance(tooltipDirective, true);
expect(spy).toHaveBeenCalled();
}));
it('should not throw when updating the position for a closed tooltip', fakeAsync(() => {
tooltipDirective.position = 'left';
tooltipDirective.show(0);
fixture.detectChanges();
tick();
tooltipDirective.hide(0);
fixture.detectChanges();
tick();
finishCurrentTooltipAnimation(overlayContainerElement, false);
expect(() => {
tooltipDirective.position = 'right';
fixture.detectChanges();
tick();
}).not.toThrow();
}));
it('should be able to modify the tooltip message', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show();
tick(0); // Tick for the show delay (default is 0)
expect(tooltipDirective._tooltipInstance!.isVisible()).toBe(true);
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
const newMessage = 'new tooltip message';
tooltipDirective.message = newMessage;
fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain(newMessage);
}));
it('should allow extra classes to be set on the tooltip', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show();
tick(0); // Tick for the show delay (default is 0)
fixture.detectChanges();
// Make sure classes aren't prematurely added
let tooltipElement = overlayContainerElement.querySelector('.mat-mdc-tooltip') as HTMLElement;
expect(tooltipElement.classList).not.toContain(
'custom-one',
'Expected to not have the class before enabling matTooltipClass',
);
expect(tooltipElement.classList).not.toContain(
'custom-two',
'Expected to not have the class before enabling matTooltipClass',
);
// Enable the classes via ngClass syntax
fixture.componentInstance.showTooltipClass = true;
fixture.detectChanges();
// Make sure classes are correctly added
tooltipElement = overlayContainerElement.querySelector('.mat-mdc-tooltip') as HTMLElement;
expect(tooltipElement.classList)
.withContext('Expected to have the class after enabling matTooltipClass')
.toContain('custom-one');
expect(tooltipElement.classList)
.withContext('Expected to have the class after enabling matTooltipClass')
.toContain('custom-two');
}));
it('should be removed after parent destroyed', fakeAsync(() => {
tooltipDirective.show();
tick(0); // Tick for the show delay (default is 0)
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.destroy();
expect(overlayContainerElement.childNodes.length).toBe(0);
expect(overlayContainerElement.textContent).toBe('');
}));
it('should have an aria-described element with the tooltip message', fakeAsync(() => {
const dynamicTooltipsDemoFixture = TestBed.createComponent(DynamicTooltipsDemo);
const dynamicTooltipsComponent = dynamicTooltipsDemoFixture.componentInstance;
dynamicTooltipsComponent.tooltips = ['Tooltip One', 'Tooltip Two'];
dynamicTooltipsDemoFixture.detectChanges();
tick();
const buttons = dynamicTooltipsDemoFixture.nativeElement.querySelectorAll('button');
const firstButtonAria = buttons[0].getAttribute('aria-describedby');
expect(document.querySelector(`#${firstButtonAria}`)!.textContent).toBe('Tooltip One');
const secondButtonAria = buttons[1].getAttribute('aria-describedby');
expect(document.querySelector(`#${secondButtonAria}`)!.textContent).toBe('Tooltip Two');
}));
it(
'should not add an ARIA description for elements that have the same text as a' +
'data-bound aria-label',
fakeAsync(() => {
const ariaLabelFixture = TestBed.createComponent(DataBoundAriaLabelTooltip);
ariaLabelFixture.detectChanges();
tick();
const button = ariaLabelFixture.nativeElement.querySelector('button');
expect(button.getAttribute('aria-describedby')).toBeFalsy();
}),
);
it('should not try to dispose the tooltip when destroyed and done hiding', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
const tooltipDelay = 1000;
tooltipDirective.hide();
tick(tooltipDelay); // Change the tooltip state to hidden and trigger animation start
fixture.componentInstance.showButton = false;
fixture.detectChanges();
}));
it('should complete the afterHidden stream when tooltip is destroyed', fakeAsync(() => {
tooltipDirective.show();
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
const spy = jasmine.createSpy('complete spy');
const subscription = tooltipDirective
._tooltipInstance!.afterHidden()
.subscribe({complete: spy});
tooltipDirective.hide(0);
tick(0);
fixture.detectChanges();
expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
}));
it('should consistently position before and after overlay origin in ltr and rtl dir', () => {
tooltipDirective.position = 'left';
const leftOrigin = tooltipDirective._getOrigin().main;
tooltipDirective.position = 'right';
const rightOrigin = tooltipDirective._getOrigin().main;
// Test expectations in LTR
tooltipDirective.position = 'before';
expect(tooltipDirective._getOrigin().main).toEqual(leftOrigin);
tooltipDirective.position = 'after';
expect(tooltipDirective._getOrigin().main).toEqual(rightOrigin);
// Test expectations in RTL
dir.value = 'rtl';
tooltipDirective.position = 'before';
expect(tooltipDirective._getOrigin().main).toEqual(leftOrigin);
tooltipDirective.position = 'after';
expect(tooltipDirective._getOrigin().main).toEqual(rightOrigin);
});
it('should consistently position before and after overlay position in ltr and rtl dir', () => {
tooltipDirective.position = 'left';
const leftOverlayPosition = tooltipDirective._getOverlayPosition().main;
tooltipDirective.position = 'right';
const rightOverlayPosition = tooltipDirective._getOverlayPosition().main;
// Test expectations in LTR
tooltipDirective.position = 'before';
expect(tooltipDirective._getOverlayPosition().main).toEqual(leftOverlayPosition);
tooltipDirective.position = 'after';
expect(tooltipDirective._getOverlayPosition().main).toEqual(rightOverlayPosition);
// Test expectations in RTL
dir.value = 'rtl';
tooltipDirective.position = 'before';
expect(tooltipDirective._getOverlayPosition().main).toEqual(leftOverlayPosition);
tooltipDirective.position = 'after';
expect(tooltipDirective._getOverlayPosition().main).toEqual(rightOverlayPosition);
});
it('should throw when trying to assign an invalid position', () => {
expect(() => {
fixture.componentInstance.position = 'everywhere';
fixture.detectChanges();
tooltipDirective.show();
}).toThrowError('Tooltip position "everywhere" is invalid.');
});
it('should pass the layout direction to the tooltip', fakeAsync(() => {
dir.value = 'rtl';
tooltipDirective.show();
tick(0);
fixture.detectChanges();
const tooltipWrapper = overlayContainerElement.querySelector(
'.cdk-overlay-connected-position-bounding-box',
)!;
expect(tooltipWrapper).withContext('Expected tooltip to be shown.').toBeTruthy();
expect(tooltipWrapper.getAttribute('dir'))
.withContext('Expected tooltip to be in RTL mode.')
.toBe('rtl');
}));
it('should keep the overlay direction in sync with the trigger direction', fakeAsync(() => {
dir.value = 'rtl';
tooltipDirective.show();
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
let tooltipWrapper = overlayContainerElement.querySelector(
'.cdk-overlay-connected-position-bounding-box',
)!;
expect(tooltipWrapper.getAttribute('dir'))
.withContext('Expected tooltip to be in RTL.')
.toBe('rtl');
tooltipDirective.hide(0);
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, false);
dir.value = 'ltr';
tooltipDirective.show();
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
tooltipWrapper = overlayContainerElement.querySelector(
'.cdk-overlay-connected-position-bounding-box',
)!;
expect(tooltipWrapper.getAttribute('dir'))
.withContext('Expected tooltip to be in LTR.')
.toBe('ltr');
}));
it('should be able to set the tooltip message as a number', fakeAsync(() => {
fixture.componentInstance.message = 100;
fixture.detectChanges();
expect(tooltipDirective.message).toBe('100');
}));
it('should hide when clicking away', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
document.body.click();
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, false);
fixture.detectChanges();
expect(tooltipDirective._isTooltipVisible()).toBe(false);
expect(overlayContainerElement.textContent).toBe('');
}));
it('should hide when clicking away with an auxilliary button', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
dispatchFakeEvent(document.body, 'auxclick');
tick(0);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, false);
fixture.detectChanges();
expect(tooltipDirective._isTooltipVisible()).toBe(false);
expect(overlayContainerElement.textContent).toBe('');
}));
it('should not hide immediately if a click fires while animating', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
document.body.click();
fixture.detectChanges();
tick(500);
finishCurrentTooltipAnimation(overlayContainerElement, true);
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
}));
it('should hide when pressing escape', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
tick(500);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
tick(0);
fixture.detectChanges();
tick(500);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, false);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
expect(overlayContainerElement.textContent).toBe('');
}));
it('should not throw when pressing ESCAPE', fakeAsync(() => {
expect(() => {
dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
}).not.toThrow();
// Flush due to the additional tick that is necessary for the FocusMonitor.
flush();
}));
it('should preventDefault when pressing ESCAPE', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
const event = dispatchKeyboardEvent(document.body, 'keydown', ESCAPE);
fixture.detectChanges();
flush();
expect(event.defaultPrevented).toBe(true);
}));
it('should not preventDefault when pressing ESCAPE with a modifier', fakeAsync(() => {
tooltipDirective.show();
tick(0);
fixture.detectChanges();
const event = createKeyboardEvent('keydown', ESCAPE, undefined, {alt: true});
dispatchEvent(document.body, event);
fixture.detectChanges();
flush();
expect(event.defaultPrevented).toBe(false);
}));
it('should not show the tooltip on programmatic focus', fakeAsync(() => {
patchElementFocus(buttonElement);
assertTooltipInstance(tooltipDirective, false);
focusMonitor.focusVia(buttonElement, 'program');
tick(0);
fixture.detectChanges();
tick(500);
expect(overlayContainerElement.querySelector('.mat-mdc-tooltip')).toBeNull();
}));
it('should not show the tooltip on mouse focus', fakeAsync(() => {
patchElementFocus(buttonElement);
assertTooltipInstance(tooltipDirective, false);
focusMonitor.focusVia(buttonElement, 'mouse');
tick(0);
fixture.detectChanges();
tick(500);
expect(overlayContainerElement.querySelector('.mat-mdc-tooltip')).toBeNull();
}));
it('should not show the tooltip on touch focus', fakeAsync(() => {
patchElementFocus(buttonElement);
assertTooltipInstance(tooltipDirective, false);
focusMonitor.focusVia(buttonElement, 'touch');
tick(0);
fixture.detectChanges();
tick(500);
expect(overlayContainerElement.querySelector('.mat-mdc-tooltip')).toBeNull();
}));
it('should not hide the tooltip when calling `show` twice in a row', fakeAsync(() => {
tooltipDirective.show();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
const overlayRef = tooltipDirective._overlayRef!;
spyOn(overlayRef, 'detach').and.callThrough();
tooltipDirective.show();
tick(0);
expect(tooltipDirective._isTooltipVisible()).toBe(true);
fixture.detectChanges();
finishCurrentTooltipAnimation(overlayContainerElement, true);
expect(overlayRef.detach).not.toHaveBeenCalled();
}));
it('should set a class on the overlay panel that reflects the position', fakeAsync(() => {
// Move the element so that the primary position is always used.
buttonElement.style.position = 'fixed';
buttonElement.style.top = buttonElement.style.left = '200px';
fixture.componentInstance.message = 'hi';
fixture.detectChanges();
setPositionAndShow('below');
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
expect(classList).toContain('mat-mdc-tooltip-panel-below');
setPositionAndShow('above');
expect(classList).not.toContain('mat-mdc-tooltip-panel-below');
expect(classList).toContain('mat-mdc-tooltip-panel-above');
setPositionAndShow('left');
expect(classList).not.toContain('mat-mdc-tooltip-panel-above');
expect(classList).toContain('mat-mdc-tooltip-panel-left');
setPositionAndShow('right');
expect(classList).not.toContain('mat-mdc-tooltip-panel-left');
expect(classList).toContain('mat-mdc-tooltip-panel-right');
function setPositionAndShow(position: TooltipPosition) {
tooltipDirective.hide(0);
fixture.detectChanges();
tick(0);
tooltipDirective.position = position;
tooltipDirective.show(0);
fixture.detectChanges();
tick(0);
fixture.detectChanges();
tick(500);
}
}));
it('should account for RTL when setting the tooltip position class', fakeAsync(() => {
// Move the element so that the primary position is always used.
buttonElement.style.position = 'fixed';
buttonElement.style.top = buttonElement.style.left = '200px';
fixture.componentInstance.message = 'hi';
fixture.detectChanges();
dir.value = 'ltr';
tooltipDirective.position = 'after';
tooltipDirective.show(0);
fixture.detectChanges();
tick(0);
fixture.detectChanges();
tick(500);
const classList = tooltipDirective._overlayRef!.overlayElement.classList;
expect(classList).not.toContain('mat-mdc-tooltip-panel-after');
expect(classList).not.toContain('mat-mdc-tooltip-panel-before');
expect(classList).not.toContain('mat-mdc-tooltip-panel-left');
expect(classList).toContain('mat-mdc-tooltip-panel-right');
tooltipDirective.hide(0);
fixture.detectChanges();
tick(0);
dir.value = 'rtl';
tooltipDirective.show(0);
fixture.detectChanges();
tick(0);
fixture.detectChanges();
tick(500);
expect(classList).not.toContain('mat-mdc-tooltip-panel-after');
expect(classList).not.toContain('mat-mdc-tooltip-panel-before');
expect(classList).not.toContain('mat-mdc-tooltip-panel-right');
expect(classList).toContain('mat-mdc-tooltip-panel-left');
}));
it('should clear the show timeout on destroy', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show(1000);
fixture.detectChanges();
// Note that we aren't asserting anything, but `fakeAsync` will
// throw if we have any timers by the end of the test.
fixture.destroy();
}));
it('should clear the hide timeout on destroy', fakeAsync(() => {
assertTooltipInstance(tooltipDirective, false);
tooltipDirective.show();
tick(0);
fixture.detectChanges();
tick(500);
tooltipDirective.hide(1000);
fixture.detectChanges();
// Note that we aren't asserting anything, but `fakeAsync` will
// throw if we have any timers by the end of the test.
fixture.destroy();
}));
it('should set the multiline class on tooltips with messages that overflow', fakeAsync(() => {
fixture.componentInstance.message =
'This is a very long message that should cause the' +
'tooltip message body to overflow onto a new line.';
tooltipDirective.show();
fixture.detectChanges();
tick();
// Need to detect changes again to wait for the multiline class to be applied.
fixture.detectChanges();
const tooltipElement = overlayContainerElement.querySelector(
'.mat-mdc-tooltip',
) as HTMLElement;
expect(tooltipElement.classList).toContain('mdc-tooltip--multiline');