Skip to content

Testing

DataCache and cachedResource require an Angular injection context. Use TestBed.

order-list.store.spec.ts
describe('OrderListStore', () => {
let store: OrderListStore
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideZiflux(),
provideHttpClient(),
provideHttpClientTesting(),
OrderApi,
OrderListStore,
],
})
store = TestBed.inject(OrderListStore)
})
it('loads orders', async () => {
const httpTesting = TestBed.inject(HttpTestingController)
// Flush the HTTP request
httpTesting.expectOne('/orders').flush([{ id: '1', status: 'pending' }])
await new Promise(r => setTimeout(r, 0)); TestBed.tick()
expect(store.orders.value()).toHaveLength(1)
})
})

Use runInInjectionContext when you need a bare cache without the full store setup.

data-cache.spec.ts
let cache: DataCache
beforeEach(() => {
TestBed.configureTestingModule({})
cache = TestBed.runInInjectionContext(() => new DataCache())
})
it('stores and retrieves data', () => {
cache.set(['key'], 'value')
expect(cache.get<string>(['key'])?.data).toBe('value')
})