foundation: resolve platform in injection context for dot background teardown

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-25 17:12:10 +02:00
parent 42e9f01253
commit 9ea2885c7b
3 changed files with 28 additions and 1 deletions

View File

@@ -25,4 +25,29 @@ describe('DotBackground', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
it('does not throw when destroyed after browser initialization', async () => {
const gradient = { addColorStop: vi.fn() };
const context = {
clearRect: vi.fn(),
beginPath: vi.fn(),
arc: vi.fn(),
fill: vi.fn(),
createRadialGradient: vi.fn(() => gradient),
};
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(
context as unknown as CanvasRenderingContext2D,
);
const animationFrameSpy = vi.spyOn(window, 'requestAnimationFrame').mockReturnValue(0);
const initializedFixture = TestBed.createComponent(DotBackground);
initializedFixture.detectChanges();
await initializedFixture.whenStable();
expect(() => initializedFixture.destroy()).not.toThrow();
animationFrameSpy.mockRestore();
vi.restoreAllMocks();
});
});

View File

@@ -21,6 +21,7 @@ export class DotBackground implements OnDestroy {
private readonly ngZone = inject(NgZone);
private readonly coarsePointer = prefersCoarsePointer();
private readonly isBrowser = isBrowserPlatform();
private ctx: CanvasRenderingContext2D | undefined;
private dots: Dot[] = [];
@@ -50,7 +51,7 @@ export class DotBackground implements OnDestroy {
cancelAnimationFrame(this.animationId);
if (isBrowserPlatform()) {
if (this.isBrowser) {
window.removeEventListener('resize', this.resize);
window.removeEventListener('mousemove', this.onMouseMove);
window.removeEventListener('click', this.onMouseClick);