Skip to content

Commit b0128f4

Browse files
committed
fix(igxScrollInertia): event listeners leak
1 parent 0489b04 commit b0128f4

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

projects/igniteui-angular/src/lib/directives/scroll-inertia/scroll_inertia.directive.spec.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
NgZone,
55
OnInit,
66
ViewChild,
7-
ElementRef
7+
ElementRef,
8+
OnDestroy,
89
} from '@angular/core';
910
import { TestBed, ComponentFixture, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
1011
import { IgxScrollInertiaDirective } from './scroll_inertia.directive';
@@ -15,21 +16,28 @@ import { wait } from '../../test-utils/ui-interactions.spec';
1516
describe('Scroll Inertia Directive - Rendering', () => {
1617
let fix: ComponentFixture<ScrollInertiaComponent>;
1718

18-
configureTestSuite();
19+
configureTestSuite({ checkLeaks: true });
1920
beforeAll(waitForAsync(() => {
2021
TestBed.configureTestingModule({
2122
imports: [
2223
IgxTestScrollInertiaDirective,
2324
ScrollInertiaComponent
2425
]
25-
}).compileComponents().then(() => {
26-
fix = TestBed.createComponent(ScrollInertiaComponent);
27-
fix.detectChanges();
28-
});
26+
}).compileComponents();
2927
}));
3028

31-
it('should initialize directive on non-scrollable container.', () => {
29+
beforeEach(() => {
30+
fix = TestBed.createComponent(ScrollInertiaComponent);
31+
fix.detectChanges();
32+
});
33+
34+
afterEach(() => {
35+
fix = null;
36+
});
37+
38+
it('should initialize directive on non-scrollable container.', async () => {
3239
expect(fix.componentInstance.scrInertiaDir).toBeDefined('scroll inertia initializing through markup failed');
40+
await fix.whenStable();
3341
});
3442

3543
// Unit tests for inertia function.
@@ -89,6 +97,10 @@ describe('Scroll Inertia Directive - Scrolling', () => {
8997
scrollInertiaDir.smoothingDuration = 0;
9098
});
9199

100+
afterEach(() => {
101+
scrollInertiaDir.ngOnDestroy();
102+
});
103+
92104
// Unit test for wheel - wheelDelataY/wheelDeltaX supported on Chrome, Safari, Opera.
93105
it('should change scroll top for related scrollbar if onWheel is executed with wheelDeltaY.', () => {
94106
scrollInertiaDir.IgxScrollInertiaDirection = 'vertical';
@@ -342,7 +354,7 @@ export class IgxTestScrollInertiaDirective extends IgxScrollInertiaDirective {
342354
`,
343355
imports: [IgxTestScrollInertiaDirective]
344356
})
345-
export class ScrollInertiaComponent implements OnInit {
357+
export class ScrollInertiaComponent implements OnInit, OnDestroy {
346358
@ViewChild('container', { static: true }) public container: ElementRef;
347359
@ViewChild('scrBar', { static: true }) public scrollContainer: ElementRef;
348360
@ViewChild('scrInertiaContainer', { read: IgxTestScrollInertiaDirective, static: true })
@@ -359,9 +371,8 @@ export class ScrollInertiaComponent implements OnInit {
359371
public ngOnInit() {
360372
this.scrInertiaDir.IgxScrollInertiaScrollContainer = this.scrollContainer.nativeElement;
361373

362-
this.scrollContainer.nativeElement.addEventListener('scroll', (evt) => {
363-
this.onScroll(evt);
364-
});
374+
this.onScroll = this.onScroll.bind(this);
375+
this.scrollContainer.nativeElement.addEventListener('scroll', this.onScroll);
365376
}
366377

367378
public onScroll(evt) {
@@ -375,4 +386,8 @@ export class ScrollInertiaComponent implements OnInit {
375386
this.scrTopStepArray.push(calcScrollStep);
376387
this.scrLeftStepArray.push(calcScrollLeftStep);
377388
}
389+
390+
public ngOnDestroy(): void {
391+
this.scrollContainer.nativeElement.removeEventListener('scroll', this.onScroll);
392+
}
378393
}

projects/igniteui-angular/src/lib/directives/scroll-inertia/scroll_inertia.directive.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ export class IgxScrollInertiaDirective implements OnInit, OnDestroy {
6969
return;
7070
}
7171
const targetElem = this.parentElement;
72-
targetElem.addEventListener('wheel', this.onWheel.bind(this), { passive: false });
73-
targetElem.addEventListener('touchstart', this.onTouchStart.bind(this), { passive: false });
74-
targetElem.addEventListener('touchmove', this.onTouchMove.bind(this), { passive: false });
75-
targetElem.addEventListener('touchend', this.onTouchEnd.bind(this), { passive: false });
72+
this.onWheel = this.onWheel.bind(this);
73+
this.onTouchStart = this.onTouchStart.bind(this);
74+
this.onTouchMove = this.onTouchMove.bind(this);
75+
this.onTouchEnd = this.onTouchEnd.bind(this);
76+
targetElem.addEventListener('wheel', this.onWheel, { passive: false });
77+
targetElem.addEventListener('touchstart', this.onTouchStart, { passive: false });
78+
targetElem.addEventListener('touchmove', this.onTouchMove, { passive: false });
79+
targetElem.addEventListener('touchend', this.onTouchEnd, { passive: false });
7680
});
7781
}
7882

0 commit comments

Comments
 (0)