Testing
Test doubles for the parts of a resource that reach for real browser APIs: a deterministic cache, controllable network and visibility sensors, and an in-memory mutation stash. HTTP itself stays on Angular's own testing backend.
@mmstack/resourcenpm
#A deterministic cache
provideMockQueryCache() is a real in-memory cache built for specs. It never touches IndexedDB or BroadcastChannel and disables the cleanup sweep, so it is safe under fake timers and leaves nothing pinned between tests. Because it is a real cache and not a stub, cache hits behave as they do in production and you can assert against them.
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import {
createCacheInterceptor,
createDedupeRequestsInterceptor,
provideMockQueryCache,
} from '@mmstack/resource';
TestBed.configureTestingModule({
providers: [
provideMockQueryCache(), // timer-free, no IndexedDB, no BroadcastChannel
provideHttpClient(
withInterceptors([
createCacheInterceptor(),
createDedupeRequestsInterceptor(),
]),
),
],
}); HTTP responses go through Angular's own provideHttpClientTesting() and HttpTestingController. A cache miss flows through the interceptor chain to the testing backend like any other request, so you flush responses the usual way.
#Network and visibility
Resources pause when the network drops or the page hides. To drive that in a test, provideMockResourceSensors() swaps the real navigator.onLine and document.visibilityState for writable signals you control. Pass your own to toggle state mid-test, or omit them for a static online and visible environment.
import { signal } from '@angular/core';
import { provideMockResourceSensors } from '@mmstack/resource';
const online = signal(true);
TestBed.configureTestingModule({
providers: [provideMockResourceSensors({ networkStatus: online })],
});
// later, simulate going offline:
online.set(false); // the resource sees the network drop and disables#Offline mutation persistence
For mutations that persist across an app close, provideMockMutationPersistence() is an in-memory stand-in for the IndexedDB stash. Seed it with rows to simulate mutations a previous session left behind, then instantiate the mutationResource with the matching persist.key and assert that they replay.
import { provideMockMutationPersistence } from '@mmstack/resource';
TestBed.configureTestingModule({
providers: [
provideMockMutationPersistence({
// seed a mutation as if a prior session left it unsettled
rows: [{ key: 'save-post', raw: { id: 1, title: 'Draft' } }],
}),
],
});