1
- import { fakeAsync , TestBed , tick , flush , waitForAsync } from '@angular/core/testing' ;
1
+ import { DebugElement } from '@angular/core' ;
2
+ import { fakeAsync , TestBed , tick , flush , waitForAsync , ComponentFixture } from '@angular/core/testing' ;
2
3
import { By } from '@angular/platform-browser' ;
3
4
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
4
5
import { IgxTooltipSingleTargetComponent , IgxTooltipMultipleTargetsComponent , IgxTooltipPlainStringComponent , IgxTooltipWithToggleActionComponent } from '../../test-utils/tooltip-components.spec' ;
@@ -11,10 +12,10 @@ const HIDDEN_TOOLTIP_CLASS = 'igx-tooltip--hidden';
11
12
const TOOLTIP_CLASS = 'igx-tooltip' ;
12
13
13
14
describe ( 'IgxTooltip' , ( ) => {
14
- let fix ;
15
- let tooltipNativeElement ;
15
+ let fix : ComponentFixture < any > ;
16
+ let tooltipNativeElement : HTMLElement ;
16
17
let tooltipTarget : IgxTooltipTargetDirective ;
17
- let button ;
18
+ let button : DebugElement ;
18
19
19
20
beforeEach ( waitForAsync ( ( ) => {
20
21
TestBed . configureTestingModule ( {
@@ -273,7 +274,7 @@ describe('IgxTooltip', () => {
273
274
flush ( ) ;
274
275
275
276
verifyTooltipVisibility ( tooltipNativeElement , tooltipTarget , true ) ;
276
-
277
+
277
278
fix . componentInstance . showButton = false ;
278
279
fix . detectChanges ( ) ;
279
280
flush ( ) ;
@@ -492,8 +493,8 @@ describe('IgxTooltip', () => {
492
493
fix = TestBed . createComponent ( IgxTooltipMultipleTargetsComponent ) ;
493
494
fix . detectChanges ( ) ;
494
495
tooltipNativeElement = fix . debugElement . query ( By . directive ( IgxTooltipDirective ) ) . nativeElement ;
495
- targetOne = fix . componentInstance . targetOne as IgxTooltipTargetDirective ;
496
- targetTwo = fix . componentInstance . targetTwo as IgxTooltipTargetDirective ;
496
+ targetOne = fix . componentInstance . targetOne ;
497
+ targetTwo = fix . componentInstance . targetTwo ;
497
498
buttonOne = fix . debugElement . query ( By . css ( '.buttonOne' ) ) ;
498
499
buttonTwo = fix . debugElement . query ( By . css ( '.buttonTwo' ) ) ;
499
500
} ) ) ;
@@ -560,6 +561,64 @@ describe('IgxTooltip', () => {
560
561
// Tooltip is NOT visible and positioned relative to buttonOne
561
562
verifyTooltipPosition ( tooltipNativeElement , buttonOne , false ) ;
562
563
} ) ) ;
564
+
565
+ it ( 'Should not call `hideTooltip` multiple times on document:touchstart' , fakeAsync ( ( ) => {
566
+ spyOn ( targetOne , 'hideTooltip' ) . and . callThrough ( ) ;
567
+ spyOn ( targetTwo , 'hideTooltip' ) . and . callThrough ( ) ;
568
+
569
+ touchElement ( buttonOne ) ;
570
+ tick ( 500 ) ;
571
+
572
+ const dummyDiv = fix . debugElement . query ( By . css ( '.dummyDiv' ) ) ;
573
+ touchElement ( dummyDiv ) ;
574
+ flush ( ) ;
575
+
576
+ expect ( targetOne . hideTooltip ) . toHaveBeenCalledTimes ( 1 ) ;
577
+ expect ( targetTwo . hideTooltip ) . not . toHaveBeenCalled ( ) ;
578
+ } ) ) ;
579
+
580
+ it ( 'should not emit tooltipHide event multiple times' , fakeAsync ( ( ) => {
581
+ spyOn ( targetOne . tooltipHide , 'emit' ) ;
582
+ spyOn ( targetTwo . tooltipHide , 'emit' ) ;
583
+
584
+ hoverElement ( buttonOne ) ;
585
+ flush ( ) ;
586
+
587
+ const tooltipHideArgsTargetOne = { target : targetOne , tooltip : fix . componentInstance . tooltip , cancel : false } ;
588
+ const tooltipHideArgsTargetTwo = { target : targetTwo , tooltip : fix . componentInstance . tooltip , cancel : false } ;
589
+
590
+ unhoverElement ( buttonOne ) ;
591
+ tick ( 500 ) ;
592
+ expect ( targetOne . tooltipHide . emit ) . toHaveBeenCalledOnceWith ( tooltipHideArgsTargetOne ) ;
593
+ expect ( targetTwo . tooltipHide . emit ) . not . toHaveBeenCalled ( ) ;
594
+ flush ( ) ;
595
+
596
+ hoverElement ( buttonTwo ) ;
597
+ flush ( ) ;
598
+
599
+ unhoverElement ( buttonTwo ) ;
600
+ tick ( 500 ) ;
601
+ expect ( targetOne . tooltipHide . emit ) . toHaveBeenCalledOnceWith ( tooltipHideArgsTargetOne ) ;
602
+ expect ( targetTwo . tooltipHide . emit ) . toHaveBeenCalledOnceWith ( tooltipHideArgsTargetTwo ) ;
603
+ flush ( ) ;
604
+ } ) ) ;
605
+
606
+
607
+ it ( 'IgxTooltip hides when touch one target, then another, then outside' , fakeAsync ( ( ) => {
608
+ touchElement ( targetOne ) ;
609
+ flush ( ) ;
610
+ verifyTooltipVisibility ( tooltipNativeElement , targetOne , true ) ;
611
+ verifyTooltipPosition ( tooltipNativeElement , targetOne , true ) ;
612
+
613
+ touchElement ( targetTwo ) ;
614
+ flush ( ) ;
615
+ verifyTooltipVisibility ( tooltipNativeElement , targetTwo , true ) ;
616
+ verifyTooltipPosition ( tooltipNativeElement , targetTwo , true ) ;
617
+
618
+ touchElement ( fix . debugElement ) ;
619
+ flush ( ) ;
620
+ verifyTooltipVisibility ( tooltipNativeElement , targetTwo , false ) ;
621
+ } ) ) ;
563
622
} ) ;
564
623
565
624
describe ( 'Tooltip integration' , ( ) => {
@@ -593,11 +652,15 @@ describe('IgxTooltip', () => {
593
652
} ) ;
594
653
} ) ;
595
654
596
- const hoverElement = ( element ) => element . nativeElement . dispatchEvent ( new MouseEvent ( 'mouseenter' ) ) ;
655
+ interface ElementRefLike {
656
+ nativeElement : HTMLElement
657
+ }
658
+
659
+ const hoverElement = ( element : ElementRefLike ) => element . nativeElement . dispatchEvent ( new MouseEvent ( 'mouseenter' ) ) ;
597
660
598
- const unhoverElement = ( element ) => element . nativeElement . dispatchEvent ( new MouseEvent ( 'mouseleave' ) ) ;
661
+ const unhoverElement = ( element : ElementRefLike ) => element . nativeElement . dispatchEvent ( new MouseEvent ( 'mouseleave' ) ) ;
599
662
600
- const touchElement = ( element ) => element . nativeElement . dispatchEvent ( new TouchEvent ( 'touchstart' , { bubbles : true } ) ) ;
663
+ const touchElement = ( element : ElementRefLike ) => element . nativeElement . dispatchEvent ( new TouchEvent ( 'touchstart' , { bubbles : true } ) ) ;
601
664
602
665
const verifyTooltipVisibility = ( tooltipNativeElement , tooltipTarget , shouldBeVisible : boolean ) => {
603
666
expect ( tooltipNativeElement . classList . contains ( TOOLTIP_CLASS ) ) . toBe ( shouldBeVisible ) ;
0 commit comments