Files
racket-wiki/test/cmap-history.test.mjs
T

49 lines
1.4 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { CmapHistory } from "../static/js/cmap/controller/cmap-history.js";
function history(initial = "one") {
let value = initial;
const changes = [];
const instance = new CmapHistory({
snapshot: () => value,
restore: (snapshot) => { value = snapshot; },
onChange: (change) => changes.push(change),
limit: 2
});
return {
instance,
changes,
get value() { return value; },
set value(next) { value = next; }
};
}
test("CmapHistory commits, undoes and redoes serialized state", () => {
const state = history();
state.instance.reset();
state.value = "two";
assert.equal(state.instance.commit(), true);
state.value = "three";
assert.equal(state.instance.commit(), true);
assert.equal(state.instance.undo(), true);
assert.equal(state.value, "two");
assert.equal(state.instance.redo(), true);
assert.equal(state.value, "three");
assert.equal(state.instance.canUndo(), true);
assert.equal(state.instance.canRedo(), false);
});
test("CmapHistory replaces a document as one undo operation", () => {
const state = history();
state.instance.reset();
state.instance.replace(() => { state.value = "replacement"; });
assert.equal(state.value, "replacement");
assert.equal(state.instance.undo(), true);
assert.equal(state.value, "one");
assert.ok(state.changes.length > 0);
});