Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Nov 20, 2024
1 parent adb9a72 commit aa0cf04
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/stash/src/actions/applyUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ export function applyUpdates({ stash, updates }: ApplyUpdatesArgs): void {
((storeUpdates.records[table.namespaceLabel] ??= {})[table.label] ??= {})[encodedKey] ??= update;
}

// queueMicrotask(() => {
notifySubscribers(stash);
// });
queueMicrotask(() => {
notifySubscribers(stash);
});
}

function notifySubscribers(stash: Stash) {
Expand Down
26 changes: 11 additions & 15 deletions packages/stash/src/actions/subscribeQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("defineQuery", () => {

beforeEach(() => {
stash = createStash(config);
vi.useFakeTimers({ toFake: ["queueMicrotask"] });

// Add some mock data
const items = ["0xgold", "0xsilver"] as const;
Expand All @@ -45,6 +46,8 @@ describe("defineQuery", () => {
setRecord({ stash, table: Inventory, key: { player: `0x${String(i)}`, item }, value: { amount: i } });
}
}

vi.advanceTimersToNextTimer();
});

it("should return the matching keys and keep it updated", () => {
Expand All @@ -55,23 +58,22 @@ describe("defineQuery", () => {
});

setRecord({ stash, table: Health, key: { player: `0x2` }, value: { health: 2 } });
vi.advanceTimersToNextTimer();

attest(result.keys).snap({
"0x3": { player: "0x3" },
"0x4": { player: "0x4" },
"0x2": { player: "0x2" },
});
attest(result.keys).snap({ "0x3": { player: "0x3" }, "0x4": { player: "0x4" }, "0x2": { player: "0x2" } });
});

it("should notify subscribers when a matching key is updated", () => {
vi.useFakeTimers({ toFake: ["queueMicrotask"] });

let lastUpdate: unknown;
const subscriber = vi.fn((update: QueryUpdate) => (lastUpdate = update));
const result = subscribeQuery({ stash, query: [Matches(Position, { x: 4 }), In(Health)] });
result.subscribe(subscriber);

const result = subscribeQuery({
stash,
query: [Matches(Position, { x: 4 }), In(Health)],
});
result.subscribe(subscriber);
vi.advanceTimersToNextTimer();

expect(subscriber).toBeCalledTimes(0);

setRecord({ stash, table: Position, key: { player: "0x4" }, value: { y: 2 } });
Expand All @@ -95,8 +97,6 @@ describe("defineQuery", () => {
});

it("should notify subscribers when a new key matches", () => {
vi.useFakeTimers({ toFake: ["queueMicrotask"] });

let lastUpdate: unknown;
const subscriber = vi.fn((update: QueryUpdate) => (lastUpdate = update));
const result = subscribeQuery({ stash, query: [In(Position), In(Health)] });
Expand Down Expand Up @@ -126,8 +126,6 @@ describe("defineQuery", () => {
});

it("should notify subscribers when a key doesn't match anymore", () => {
vi.useFakeTimers({ toFake: ["queueMicrotask"] });

let lastUpdate: unknown;
const subscriber = vi.fn((update: QueryUpdate) => (lastUpdate = update));
const result = subscribeQuery({ stash, query: [In(Position), In(Health)] });
Expand Down Expand Up @@ -157,8 +155,6 @@ describe("defineQuery", () => {
});

it("should notify initial subscribers with initial query result", () => {
vi.useFakeTimers({ toFake: ["queueMicrotask"] });

let lastUpdate: unknown;
const subscriber = vi.fn((update: QueryUpdate) => (lastUpdate = update));
subscribeQuery({ stash, query: [In(Position), In(Health)], options: { initialSubscribers: [subscriber] } });
Expand Down

0 comments on commit aa0cf04

Please sign in to comment.