refactor(books): update both book caches from socket events (#2209)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user