Skip to content

Commit f86d75d

Browse files
guguclaude
andcommitted
app.component.spec: isolate session-restoration setTimeout from cross-test pollution
AppComponent never unsubscribes from authCast, so prior tests' component instances re-fire on cast.next and queue their own setTimeouts, overwriting the captured callback. Gate the capture behind a flag set by THIS app's mocked initializeUserSession so we only grab the timeout from this instance's restoration branch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent e4d2057 commit f86d75d

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

frontend/src/app/app.component.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,17 @@ describe('AppComponent', () => {
288288
});
289289

290290
it('should restore session and log out after token expiration', async () => {
291+
// Lingering subscriptions from previous tests (AppComponent never unsubscribes from
292+
// authCast) also schedule setTimeouts when cast.next fires. Capture only the timeout
293+
// queued immediately after THIS app's mocked initializeUserSession, which is the one
294+
// scheduled by the session-restoration branch for this component instance.
291295
let capturedTimeoutCallback: Function | null = null;
296+
let captureNextTimeout = false;
292297
const setTimeoutSpy = vi.spyOn(window, 'setTimeout').mockImplementation((callback: Function) => {
293-
capturedTimeoutCallback = callback;
298+
if (captureNextTimeout) {
299+
capturedTimeoutCallback = callback;
300+
captureNextTimeout = false;
301+
}
294302
return 1 as unknown as ReturnType<typeof setTimeout>;
295303
});
296304

@@ -299,6 +307,7 @@ describe('AppComponent', () => {
299307

300308
vi.spyOn(app, 'initializeUserSession').mockImplementation(() => {
301309
app.userLoggedIn = true;
310+
captureNextTimeout = true;
302311
});
303312

304313
app.ngOnInit();
@@ -307,11 +316,9 @@ describe('AppComponent', () => {
307316
await fixture.whenStable();
308317

309318
expect(app.initializeUserSession).toHaveBeenCalled();
319+
expect(capturedTimeoutCallback).not.toBeNull();
310320

311-
// Execute the timeout callback that was captured
312-
if (capturedTimeoutCallback) {
313-
capturedTimeoutCallback();
314-
}
321+
capturedTimeoutCallback!();
315322

316323
expect(app.logOut).toHaveBeenCalledWith(true);
317324
expect(app.router.navigate).toHaveBeenCalledWith(['/login']);

0 commit comments

Comments
 (0)