Skip to content

Commit 8d97c6a

Browse files
Test refactor.
1 parent 41b3bed commit 8d97c6a

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

highcharts-angular/src/lib/highcharts-chart.directive.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/// <reference types="jasmine" />
2-
import { TestBed, fakeAsync, tick, flush } from '@angular/core/testing';
2+
import { TestBed, fakeAsync, tick, flush, flushMicrotasks } from '@angular/core/testing';
33
import { ChangeDetectionStrategy, Component, DebugElement } from '@angular/core';
44
import { By } from '@angular/platform-browser';
55
import { HighchartsChartDirective } from './highcharts-chart.directive';
@@ -91,21 +91,29 @@ describe('HighchartsChartDirective', () => {
9191
});
9292

9393
it('should stagger multiple chart initializations to prevent main thread blocking', fakeAsync(() => {
94+
// 1. Reset the spy in case the component from beforeEach() fired and leaked a call
95+
chartSpy.calls.reset();
96+
9497
const multiFixture = TestBed.createComponent(MultiTestHostComponent);
9598
multiFixture.detectChanges(); // Triggers initialization for 3 charts synchronously
9699

100+
// Flush the initial effects so all timers start exactly at T=0
101+
tick(0);
97102
expect(chartSpy).not.toHaveBeenCalled();
98103

99104
// First chart renders at baseTimeout (500ms) + 0ms stagger
100105
tick(500);
106+
flushMicrotasks(); // CRUCIAL: Allow the 'await' to resume after the timer fires
101107
expect(chartSpy).toHaveBeenCalledTimes(1);
102108

103109
// Second chart renders 16ms later
104110
tick(16);
111+
flushMicrotasks(); // CRUCIAL
105112
expect(chartSpy).toHaveBeenCalledTimes(2);
106113

107114
// Third chart renders another 16ms later
108115
tick(16);
116+
flushMicrotasks(); // CRUCIAL
109117
expect(chartSpy).toHaveBeenCalledTimes(3);
110118

111119
flush(); // Clear out any remaining tasks

0 commit comments

Comments
 (0)