Files
mnote/wolai-frontend/src/lib/file-tree/clipboard.test.ts
T

58 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-01-07 18:38:56 +08:00
import { describe, expect, it } from "vitest";
import {
clearFileTreeClipboardPayload,
2026-01-07 18:38:56 +08:00
decodeFileTreeClipboardPayload,
encodeFileTreeClipboardPayload,
inferPasteTargetDocId,
isTextInputTarget,
} from "./clipboard";
describe("file-tree clipboard payload", () => {
it("可编码/解码", () => {
2026-01-18 19:01:31 +08:00
const payload = { type: "mnote-file-tree" as const, version: 1 as const, action: "copy" as const, rowIds: ["doc:a", "asset:x"] };
2026-01-07 18:38:56 +08:00
const text = encodeFileTreeClipboardPayload(payload);
expect(decodeFileTreeClipboardPayload(text)).toEqual(payload);
const cutPayload = { ...payload, action: "cut" as const };
expect(decodeFileTreeClipboardPayload(encodeFileTreeClipboardPayload(cutPayload))).toEqual(cutPayload);
expect(
decodeFileTreeClipboardPayload(
encodeFileTreeClipboardPayload({ ...payload, action: "move" as any }),
),
).toBeNull();
2026-01-07 18:38:56 +08:00
expect(decodeFileTreeClipboardPayload("not-a-payload")).toBeNull();
});
it("可清空内存剪贴板", async () => {
await clearFileTreeClipboardPayload();
});
2026-01-07 18:38:56 +08:00
});
describe("inferPasteTargetDocId", () => {
it("focused 为 doc/index/asset 三分支覆盖", () => {
const rowById = new Map<string, any>();
rowById.set("asset:x", { kind: "asset", rowId: "asset:x", docId: "d1", asset: { id: "x" } });
expect(inferPasteTargetDocId({ focusedRowId: "doc:d2", rowById, activeDocId: "active" })).toBe("d2");
expect(inferPasteTargetDocId({ focusedRowId: "index:d3", rowById, activeDocId: "active" })).toBe("d3");
expect(inferPasteTargetDocId({ focusedRowId: "asset:x", rowById, activeDocId: "active" })).toBe("d1");
});
it("无 focused 时回退 activeDocId", () => {
const rowById = new Map<string, any>();
expect(inferPasteTargetDocId({ focusedRowId: null, rowById, activeDocId: "active" })).toBe("active");
expect(inferPasteTargetDocId({ focusedRowId: null, rowById, activeDocId: null })).toBeNull();
});
});
describe("isTextInputTarget", () => {
it("input/textarea/contenteditable 不拦截快捷键", () => {
const input = document.createElement("input");
const textarea = document.createElement("textarea");
const div = document.createElement("div");
div.setAttribute("contenteditable", "true");
expect(isTextInputTarget(input)).toBe(true);
expect(isTextInputTarget(textarea)).toBe(true);
expect(isTextInputTarget(div)).toBe(true);
expect(isTextInputTarget(document.createElement("button"))).toBe(false);
});
});