import { expect, it } from "bun:test"; import { Patches, apply } from "mutative"; import { HistoryEntries } from "../historyEntries"; it("compares paths", () => { expect(HistoryEntries.pathsEqual(["a", "b"], ["a", "b"])).toEqual(true); expect(HistoryEntries.pathsEqual(["a", "b"], ["a", "c"])).toEqual(false); }); it("consolidates patches", () => { const patches: Patches = [ { path: ["a", "b"], op: "replace", value: 1 }, { path: ["a", "b"], op: "replace", value: 2 }, ]; expect(HistoryEntries.compactPatches(patches)).toEqual([ { path: ["a", "b"], op: "replace", value: 2 }, ]); }); it("doesn't consolidate patches with different paths", () => { const patches: Patches = [ { path: ["a", "b"], op: "replace", value: 1 }, { path: ["a", "c"], op: "replace", value: 2 }, ]; expect(HistoryEntries.compactPatches(patches)).toEqual(patches); }); it("consolidates patches that are separated by unrelated patches", () => { const patches: Patches = [ { path: ["a", "b"], op: "replace", value: 1 }, { path: ["a", "c"], op: "replace", value: 1 }, { path: ["a", "b"], op: "replace", value: 2 }, ]; expect(HistoryEntries.compactPatches(patches)).toEqual([ { path: ["a", "c"], op: "replace", value: 1 }, { path: ["a", "b"], op: "replace", value: 2 }, ]); }); it("doesn't consolidate patches that are separated by move", () => { const patches: Patches = [ { path: ["a", "b"], op: "replace", value: 1 }, { from: ["a", "b"], path: ["a", "c"], op: "move" }, { path: ["a", "b"], op: "replace", value: 2 }, ]; expect(HistoryEntries.compactPatches(patches)).toEqual([ { path: ["a", "b"], op: "replace", value: 1 }, { from: ["a", "b"], path: ["a", "c"], op: "move" }, { path: ["a", "b"], op: "replace", value: 2 }, ]); }); it("doesn't consolidate patches that are separated by parent move", () => { const patches: Patches = [ { path: ["a", "b"], op: "replace", value: 1 }, { from: ["a"], path: ["c"], op: "move" }, { path: ["a"], op: "add", value: {} }, { path: ["a", "b"], op: "replace", value: 2 }, ]; expect(HistoryEntries.compactPatches(patches)).toEqual(patches); }); it("supports move patches", () => { const testState: { foo?: number; bar?: number } = { foo: 42 }; const result = apply(testState, [ { path: ["bar"], op: "move", from: ["foo"], value: 42 }, ]); expect(result).toEqual({ bar: 42 }); });