|
1 | 1 | /// <reference types="jasmine" /> |
2 | | -import { TestBed, fakeAsync, tick, flush } from '@angular/core/testing'; |
| 2 | +import { TestBed, fakeAsync, tick, flush, flushMicrotasks } from '@angular/core/testing'; |
3 | 3 | import { ChangeDetectionStrategy, Component, DebugElement } from '@angular/core'; |
4 | 4 | import { By } from '@angular/platform-browser'; |
5 | 5 | import { HighchartsChartDirective } from './highcharts-chart.directive'; |
@@ -91,21 +91,29 @@ describe('HighchartsChartDirective', () => { |
91 | 91 | }); |
92 | 92 |
|
93 | 93 | 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 | + |
94 | 97 | const multiFixture = TestBed.createComponent(MultiTestHostComponent); |
95 | 98 | multiFixture.detectChanges(); // Triggers initialization for 3 charts synchronously |
96 | 99 |
|
| 100 | + // Flush the initial effects so all timers start exactly at T=0 |
| 101 | + tick(0); |
97 | 102 | expect(chartSpy).not.toHaveBeenCalled(); |
98 | 103 |
|
99 | 104 | // First chart renders at baseTimeout (500ms) + 0ms stagger |
100 | 105 | tick(500); |
| 106 | + flushMicrotasks(); // CRUCIAL: Allow the 'await' to resume after the timer fires |
101 | 107 | expect(chartSpy).toHaveBeenCalledTimes(1); |
102 | 108 |
|
103 | 109 | // Second chart renders 16ms later |
104 | 110 | tick(16); |
| 111 | + flushMicrotasks(); // CRUCIAL |
105 | 112 | expect(chartSpy).toHaveBeenCalledTimes(2); |
106 | 113 |
|
107 | 114 | // Third chart renders another 16ms later |
108 | 115 | tick(16); |
| 116 | + flushMicrotasks(); // CRUCIAL |
109 | 117 | expect(chartSpy).toHaveBeenCalledTimes(3); |
110 | 118 |
|
111 | 119 | flush(); // Clear out any remaining tasks |
|
0 commit comments