Skip to content

Commit c985eb4

Browse files
Merge pull request #16103 from IgniteUI/rivanova/fix-16100-master
fix(tooltip): prevent adding multiple document touch event listeners - master
2 parents 135f3ab + 7ffa65c commit c985eb4

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

projects/igniteui-angular/src/lib/directives/tooltip/tooltip.directive.spec.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DebugElement } from '@angular/core';
22
import { fakeAsync, TestBed, tick, flush, waitForAsync, ComponentFixture } from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
5-
import { IgxTooltipSingleTargetComponent, IgxTooltipMultipleTargetsComponent, IgxTooltipPlainStringComponent, IgxTooltipWithToggleActionComponent } from '../../test-utils/tooltip-components.spec';
5+
import { IgxTooltipSingleTargetComponent, IgxTooltipMultipleTargetsComponent, IgxTooltipPlainStringComponent, IgxTooltipWithToggleActionComponent, IgxTooltipMultipleTooltipsComponent } from '../../test-utils/tooltip-components.spec';
66
import { UIInteractions } from '../../test-utils/ui-interactions.spec';
77
import { HorizontalAlignment, VerticalAlignment, AutoPositionStrategy } from '../../services/public_api';
88
import { IgxTooltipDirective } from './tooltip.directive';
@@ -621,6 +621,36 @@ describe('IgxTooltip', () => {
621621
}));
622622
});
623623

624+
describe('Multiple tooltips', () => {
625+
let targetOne: IgxTooltipTargetDirective;
626+
627+
let tooltipOne: IgxTooltipDirective;
628+
let tooltipTwo: IgxTooltipDirective;
629+
630+
beforeEach(waitForAsync(() => {
631+
fix = TestBed.createComponent(IgxTooltipMultipleTooltipsComponent);
632+
fix.detectChanges();
633+
targetOne = fix.componentInstance.targetOne;
634+
tooltipOne = fix.componentInstance.tooltipOne;
635+
tooltipTwo = fix.componentInstance.tooltipTwo;
636+
}));
637+
638+
it('should not add multiple document:touchstart event listeners when having multiple igxTooltip instances - #16100', fakeAsync(() => {
639+
spyOn<any>(tooltipOne, 'onDocumentTouchStart').and.callThrough();
640+
spyOn<any>(tooltipTwo, 'onDocumentTouchStart').and.callThrough();
641+
642+
touchElement(targetOne);
643+
tick(500);
644+
645+
const dummyDiv = fix.debugElement.query(By.css('.dummyDiv'));
646+
touchElement(dummyDiv);
647+
flush();
648+
649+
expect(tooltipOne['onDocumentTouchStart']).toHaveBeenCalledTimes(1);
650+
expect(tooltipTwo['onDocumentTouchStart']).not.toHaveBeenCalled();
651+
}));
652+
});
653+
624654
describe('Tooltip integration', () => {
625655
beforeEach(waitForAsync(() => {
626656
fix = TestBed.createComponent(IgxTooltipWithToggleActionComponent);

projects/igniteui-angular/src/lib/directives/tooltip/tooltip.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export class IgxTooltipDirective extends IgxToggleDirective implements OnDestroy
122122
super(elementRef, cdr, overlayService, navigationService);
123123

124124
this.onDocumentTouchStart = this.onDocumentTouchStart.bind(this);
125-
this.overlayService.opening.pipe(takeUntil(this._destroy$)).subscribe(() => {
125+
this.opening.pipe(takeUntil(this._destroy$)).subscribe(() => {
126126
this._document.addEventListener('touchstart', this.onDocumentTouchStart, { passive: true });
127127
});
128-
this.overlayService.closed.pipe(takeUntil(this._destroy$)).subscribe(() => {
128+
this.closed.pipe(takeUntil(this._destroy$)).subscribe(() => {
129129
this._document.removeEventListener('touchstart', this.onDocumentTouchStart);
130130
});
131131
}

projects/igniteui-angular/src/lib/test-utils/tooltip-components.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ export class IgxTooltipMultipleTargetsComponent {
6464
@ViewChild(IgxTooltipDirective, { static: true }) public tooltip: IgxTooltipDirective;
6565
}
6666

67+
@Component({
68+
template: `
69+
<div class="dummyDiv">dummy div for touch tests</div>
70+
71+
<button class="buttonOne" #targetOne="tooltipTarget" [igxTooltipTarget]="tooltipRef1" style="margin: 100px">
72+
Target One
73+
</button>
74+
75+
<button class="buttonTwo" #targetTwo="tooltipTarget" [igxTooltipTarget]="tooltipRef2" style="margin: 100px">
76+
Target Two
77+
</button>
78+
79+
<div igxTooltip #tooltipRef1="tooltip">
80+
Hello, I am tooltip 1!
81+
</div>
82+
<div igxTooltip #tooltipRef2="tooltip">
83+
Hello, I am tooltip 2!
84+
</div>
85+
`,
86+
imports: [IgxTooltipDirective, IgxTooltipTargetDirective]
87+
})
88+
export class IgxTooltipMultipleTooltipsComponent {
89+
@ViewChild('targetOne', { read: IgxTooltipTargetDirective, static: true }) public targetOne: IgxTooltipTargetDirective;
90+
@ViewChild('targetTwo', { read: IgxTooltipTargetDirective, static: true }) public targetTwo: IgxTooltipTargetDirective;
91+
@ViewChild('tooltipRef1', { read: IgxTooltipDirective, static: true }) public tooltipOne: IgxTooltipDirective;
92+
@ViewChild('tooltipRef2', { read: IgxTooltipDirective, static: true }) public tooltipTwo: IgxTooltipDirective;
93+
}
94+
6795

6896
@Component({
6997
template: `

0 commit comments

Comments
 (0)