refactor(books): update both book caches from socket events (#2209)

This commit is contained in:
Alex
2026-09-21 13:01:52 +01:00
committed by GitHub
parent ca6046c155
commit 2c9581f2c1
11 changed files with 703 additions and 134 deletions
+30 -1
View File
@@ -2,7 +2,7 @@ import {ApplicationRef, provideZonelessChangeDetection, signal, type Environment
import {provideHttpClient} from '@angular/common/http';
import {provideHttpClientTesting} from '@angular/common/http/testing';
import {TestBed} from '@angular/core/testing';
import {provideTanStackQuery, QueryClient} from '@tanstack/angular-query-experimental';
import {provideTanStackQuery, QueryClient, QueryObserver} from '@tanstack/angular-query-experimental';
interface AuthServiceStub {
token: WritableSignal<string | null>;
@@ -40,6 +40,35 @@ export function flushSignalAndQueryEffects(): void {
TestBed.flushEffects();
}
export function observeActiveQuery(queryClient: QueryClient, queryKey: readonly unknown[], data: unknown) {
let fetchCount = 0;
let abortCount = 0;
const pendingResolutions: (() => void)[] = [];
queryClient.setQueryData(queryKey, data);
const observer = new QueryObserver(queryClient, {
queryKey,
staleTime: Infinity,
queryFn: ({signal}) => new Promise(resolve => {
fetchCount += 1;
signal.addEventListener('abort', () => {
abortCount += 1;
});
pendingResolutions.push(() => resolve(data));
}),
});
const unsubscribe = observer.subscribe(() => undefined);
return {
fetchCount: () => fetchCount,
abortCount: () => abortCount,
finish: () => {
pendingResolutions.splice(0).forEach(resolve => resolve());
unsubscribe();
},
};
}
/**
* Asynchronously flushes Angular effects and query state across multiple rounds.
* Use this for tests involving async operations like HTTP requests or timers.