44 NgZone ,
55 OnInit ,
66 ViewChild ,
7- ElementRef
7+ ElementRef ,
8+ OnDestroy ,
89} from '@angular/core' ;
910import { TestBed , ComponentFixture , fakeAsync , tick , waitForAsync } from '@angular/core/testing' ;
1011import { IgxScrollInertiaDirective } from './scroll_inertia.directive' ;
@@ -15,21 +16,28 @@ import { wait } from '../../test-utils/ui-interactions.spec';
1516describe ( '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}
0 commit comments