loro/loro-js/tests/undo.test.ts
Zixuan Chen 321e0e72a4
Undo (#361)
https://github.com/loro-dev/loro/pull/361

---------

Co-authored-by: Leon Zhao <leeeon233@gmail.com>
2024-05-21 06:14:49 +08:00

81 lines
2.3 KiB
TypeScript

import { Loro, UndoManager } from "../src";
import { describe, expect, test } from "vitest";
describe("undo", () => {
test("basic text undo", () => {
const doc = new Loro();
doc.setPeerId(1);
const undo = new UndoManager(doc, { maxUndoSteps: 100, mergeInterval: 0 });
expect(undo.canRedo()).toBeFalsy();
expect(undo.canUndo()).toBeFalsy();
doc.getText("text").insert(0, "hello");
doc.commit();
doc.getText("text").insert(5, " world!");
doc.commit();
expect(undo.canRedo()).toBeFalsy();
expect(undo.canUndo()).toBeTruthy();
undo.undo();
expect(undo.canRedo()).toBeTruthy();
expect(undo.canUndo()).toBeTruthy();
expect(doc.toJSON()).toStrictEqual({
text: "hello",
});
undo.undo();
expect(undo.canRedo()).toBeTruthy();
expect(undo.canUndo()).toBeFalsy();
expect(doc.toJSON()).toStrictEqual({
text: "",
});
undo.redo();
expect(undo.canRedo()).toBeTruthy();
expect(undo.canUndo()).toBeTruthy();
expect(doc.toJSON()).toStrictEqual({
text: "hello",
});
undo.redo();
expect(undo.canRedo()).toBeFalsy();
expect(undo.canUndo()).toBeTruthy();
expect(doc.toJSON()).toStrictEqual({
text: "hello world!",
});
});
test("merge", async () => {
const doc = new Loro();
const undo = new UndoManager(doc, { maxUndoSteps: 100, mergeInterval: 5 });
for (let i = 0; i < 10; i++) {
doc.getText("text").insert(i, i.toString());
doc.commit();
}
await new Promise((resolve) => setTimeout(resolve, 10));
for (let i = 0; i < 10; i++) {
doc.getText("text").insert(i, i.toString());
doc.commit();
}
expect(doc.toJSON()).toStrictEqual({
text: "01234567890123456789",
});
undo.undo();
expect(doc.toJSON()).toStrictEqual({
text: "0123456789",
});
undo.undo();
expect(doc.toJSON()).toStrictEqual({
text: "",
});
});
test("max undo steps", () => {
const doc = new Loro();
const undo = new UndoManager(doc, { maxUndoSteps: 100, mergeInterval: 0 });
for (let i = 0; i < 200; i++) {
doc.getText("text").insert(0, "0");
doc.commit();
}
expect(doc.getText("text").length).toBe(200);
while (undo.canUndo()) {
undo.undo();
}
expect(doc.getText("text").length).toBe(100);
});
});