Skip to content

Commit 84b5c3b

Browse files
trshafertinayuangao
authored andcommitted
fix(menu,tooltip): Ensure subscription exists before unsubscribing. (#3078)
1 parent 5d6920d commit 84b5c3b

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

src/lib/menu/menu-directive.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ export class MdMenu implements AfterContentInit, MdMenuPanel, OnDestroy {
7676
}
7777

7878
ngOnDestroy() {
79-
this._tabSubscription.unsubscribe();
79+
if (this._tabSubscription) {
80+
this._tabSubscription.unsubscribe();
81+
}
8082
}
8183

8284
/**

src/lib/menu/menu.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,12 @@ describe('MdMenu', () => {
384384

385385
});
386386

387+
describe('destroy', () => {
388+
it('does not throw an error on destroy', () => {
389+
const fixture = TestBed.createComponent(SimpleMenu);
390+
expect(fixture.destroy.bind(fixture)).not.toThrow();
391+
});
392+
});
387393
});
388394

389395
@Component({

src/lib/tooltip/tooltip.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ describe('MdTooltip', () => {
402402
expect(tooltipDirective._tooltipInstance).toBeNull();
403403
}));
404404
});
405+
406+
describe('destroy', () => {
407+
it('does not throw an error on destroy', () => {
408+
const fixture = TestBed.createComponent(BasicTooltipDemo);
409+
fixture.detectChanges();
410+
delete fixture.componentInstance.tooltip.scrollSubscription;
411+
expect(fixture.destroy.bind(fixture)).not.toThrow();
412+
});
413+
});
405414
});
406415

407416
@Component({
@@ -417,6 +426,7 @@ class BasicTooltipDemo {
417426
position: string = 'below';
418427
message: string = initialTooltipMessage;
419428
showButton: boolean = true;
429+
@ViewChild(MdTooltip) tooltip: MdTooltip;
420430
}
421431

422432
@Component({

src/lib/tooltip/tooltip.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ export class MdTooltip implements OnInit, OnDestroy {
161161
this._disposeTooltip();
162162
}
163163

164-
this.scrollSubscription.unsubscribe();
164+
if (this.scrollSubscription) {
165+
this.scrollSubscription.unsubscribe();
166+
}
165167
}
166168

167169
/** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */

0 commit comments

Comments
 (0)