Skip to content

Commit 1f9da25

Browse files
paulirwinandrefarzat
authored andcommitted
Fix: #198 - Error thrown in ngOnDestroy if plot not yet initialized
This can occur in unit tests if you do not await the component being stable, so this resolves #198. However, this could also happen if ngOnDestroy is called very quickly after ngOnInit.
1 parent 0dcdd2f commit 1f9da25

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

projects/plotly/src/lib/plotly.component.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,9 @@ describe('PlotlyComponent', () => {
242242
expect(component.loadTheme).not.toHaveBeenCalled();
243243
expect(component.themeLoader.load).not.toHaveBeenCalledOnceWith('plotly_dark');
244244
});
245+
246+
it('should not cause errors if ngOnDestroy is called before plotly is initialized', () => {
247+
// note that this test intentionally does not call ngOnInit/whenStable
248+
expect(() => component.ngOnDestroy()).not.toThrow();
249+
});
245250
});

projects/plotly/src/lib/plotly.component.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ export class PlotlyComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
143143
this.resizeHandler = undefined;
144144
}
145145

146-
const figure = this.createFigure();
147-
this.purge.emit(figure);
148-
PlotlyService.remove(this.plotlyInstance!);
146+
if (this.plotlyInstance) {
147+
const figure = this.createFigure();
148+
this.purge.emit(figure);
149+
PlotlyService.remove(this.plotlyInstance);
150+
}
149151
}
150152

151153
ngOnChanges(changes: SimpleChanges): void {

0 commit comments

Comments
 (0)