From c555afe9db1fc4cd2b2f838e564651b88d18f2b8 Mon Sep 17 00:00:00 2001 From: crisbeto Date: Sat, 12 Nov 2016 13:44:40 +0100 Subject: [PATCH] fix(md-spinner): animation not being cleaned up when used with AoT Fixes the `md-spinner` animation interval not being cleaned up when the app has been compiled through the AoT. This is due to the the fact that the `ngOnDestroy` handler is in the base `MdProgressCircle` class. Fixes #1283. --- .../progress-circle/progress-circle.spec.ts | 18 ++++++++++++++++++ src/lib/progress-circle/progress-circle.ts | 8 +++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/lib/progress-circle/progress-circle.spec.ts b/src/lib/progress-circle/progress-circle.spec.ts index 94ffc9ab0fec..897679539543 100644 --- a/src/lib/progress-circle/progress-circle.spec.ts +++ b/src/lib/progress-circle/progress-circle.spec.ts @@ -14,6 +14,7 @@ describe('MdProgressCircular', () => { IndeterminateProgressSpinner, ProgressSpinnerWithValueAndBoundMode, IndeterminateProgressSpinnerWithNgIf, + SpinnerWithNgIf, ], }); @@ -90,6 +91,20 @@ describe('MdProgressCircular', () => { fixture.detectChanges(); expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy(); }); + + it('should clean up the animation when a spinner is destroyed', () => { + let fixture = TestBed.createComponent(SpinnerWithNgIf); + fixture.detectChanges(); + + let progressElement = fixture.debugElement.query(By.css('md-spinner')); + + expect(progressElement.componentInstance.interdeterminateInterval).toBeTruthy(); + + fixture.debugElement.componentInstance.isHidden = true; + fixture.detectChanges(); + + expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy(); + }); }); @@ -105,3 +120,6 @@ class ProgressSpinnerWithValueAndBoundMode { } @Component({template: ` `}) class IndeterminateProgressSpinnerWithNgIf { } + +@Component({template: ``}) +class SpinnerWithNgIf { } diff --git a/src/lib/progress-circle/progress-circle.ts b/src/lib/progress-circle/progress-circle.ts index 76c03d8bfab4..048cf4c5ea77 100644 --- a/src/lib/progress-circle/progress-circle.ts +++ b/src/lib/progress-circle/progress-circle.ts @@ -244,11 +244,17 @@ export class MdProgressCircle implements OnDestroy { templateUrl: 'progress-circle.html', styleUrls: ['progress-circle.css'], }) -export class MdSpinner extends MdProgressCircle { +export class MdSpinner extends MdProgressCircle implements OnDestroy { constructor(changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef, ngZone: NgZone) { super(changeDetectorRef, ngZone, elementRef); this.mode = 'indeterminate'; } + + ngOnDestroy() { + // The `ngOnDestroy` from `MdProgressCircle` should be called explicitly, because + // in certain cases Angular won't call it (e.g. when using AoT and in unit tests). + super.ngOnDestroy(); + } }