46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import type { Command } from "./command";
|
|
import { createInitialAppState } from "@editor/initial-state";
|
|
import { createAppStore } from "@editor/store";
|
|
import { commandIds } from "./ids";
|
|
import { createCommandRegistry } from "./registry";
|
|
import { viewportPanCommand } from "./viewport";
|
|
|
|
const noOpCommand: Command<unknown> = {
|
|
id: "test.noop",
|
|
name: "No-op",
|
|
execute: ({ state }) => state,
|
|
};
|
|
|
|
describe("command dispatcher", () => {
|
|
test("throws for unknown commands", () => {
|
|
const store = createAppStore(createInitialAppState("Test"), createCommandRegistry([]));
|
|
expect(() => store.dispatch("missing" as never, undefined as never)).toThrow("Unknown command: missing");
|
|
});
|
|
|
|
test("dispatch applies command result to store", () => {
|
|
const store = createAppStore(createInitialAppState("Test"), createCommandRegistry([viewportPanCommand]));
|
|
store.dispatch(commandIds.viewportPan, { delta: { x: 3, y: 7 } });
|
|
expect(store.getState().editor.viewport.center).toEqual({ x: 3, y: 7 });
|
|
});
|
|
|
|
test("repeated no-op commands do not emit or record history", () => {
|
|
const initialState = createInitialAppState("Test");
|
|
const store = createAppStore(initialState, createCommandRegistry([noOpCommand]));
|
|
let notificationCount = 0;
|
|
store.subscribe(() => {
|
|
notificationCount += 1;
|
|
});
|
|
|
|
const firstResult = store.dispatch("test.noop" as never, undefined as never);
|
|
const secondResult = store.dispatch("test.noop" as never, undefined as never);
|
|
|
|
expect(firstResult).toBe(initialState);
|
|
expect(secondResult).toBe(initialState);
|
|
expect(store.getState()).toBe(initialState);
|
|
expect(store.getState().history.past).toHaveLength(0);
|
|
expect(store.getState().history.future).toHaveLength(0);
|
|
expect(notificationCount).toBe(0);
|
|
});
|
|
});
|