Eric Bower
·
2025-06-06
batch.test.ts
1import { parallel } from "../index.js";
2import {
3 createBatchMdw,
4 createSchema,
5 createStore,
6 slice,
7} from "../store/index.js";
8import { expect, test } from "../test.js";
9
10test("should batch notify subscribers based on mdw", async () => {
11 const [schema, initialState] = createSchema({
12 cache: slice.table({ empty: {} }),
13 loaders: slice.loaders(),
14 });
15 const store = createStore({
16 initialState,
17 middleware: [createBatchMdw(queueMicrotask)],
18 });
19 let counter = 0;
20 store.subscribe(() => {
21 counter += 1;
22 });
23 await store.run(function* () {
24 const group: any = yield* parallel([
25 schema.update(schema.cache.add({ "1": "one" })),
26 schema.update(schema.cache.add({ "2": "two" })),
27 schema.update(schema.cache.add({ "3": "three" })),
28 schema.update(schema.cache.add({ "4": "four" })),
29 schema.update(schema.cache.add({ "5": "five" })),
30 schema.update(schema.cache.add({ "6": "six" })),
31 ]);
32 yield* group;
33 // make sure it will still notify subscribers after batched round
34 yield* schema.update(schema.cache.add({ "7": "seven" }));
35 });
36 expect(counter).toBe(2);
37});