test(web): 画布测试 - 修复源码断言与编辑器插槽的跨文件状态依赖
This commit is contained in:
1 parent
da0531f6eb
commit
c114865781
3 files changed
+32
-19
No files matched your search
@@ -8,14 +8,19 @@ import {
|
||||
|
||||
const render = () => null;
|
||||
|
||||
// 真实插件 editor-shell 会在被导入时占满全部 EditorSlotKind,而它是否已被导入取决于测试执行顺序。
|
||||
// 这里只断言本文件自己注册的 pluginId,使结果不再受其它测试文件的副作用影响。
|
||||
const ownSlots = (slot: Parameters<typeof getEditorSlots>[0]) =>
|
||||
getEditorSlots(slot)
|
||||
.filter((item) => ["editor-a", "a", "b", "c", "x", "p1", "p2"].includes(item.pluginId))
|
||||
.map((item) => item.pluginId);
|
||||
|
||||
describe("editor slot registry", () => {
|
||||
test("registers a slot and returns it", () => {
|
||||
const unregister = registerEditorSlot({ pluginId: "editor-a", slot: "timeline-panel", render });
|
||||
const slots = getEditorSlots("timeline-panel");
|
||||
expect(slots).toHaveLength(1);
|
||||
expect(slots[0].pluginId).toBe("editor-a");
|
||||
expect(ownSlots("timeline-panel")).toEqual(["editor-a"]);
|
||||
unregister();
|
||||
expect(getEditorSlots("timeline-panel")).toHaveLength(0);
|
||||
expect(ownSlots("timeline-panel")).toEqual([]);
|
||||
});
|
||||
|
||||
test("sorts by priority desc, then registration order asc", () => {
|
||||
@@ -24,22 +29,19 @@ describe("editor slot registry", () => {
|
||||
registerEditorSlot({ pluginId: "b", slot: "preview-renderer", render }),
|
||||
registerEditorSlot({ pluginId: "c", slot: "preview-renderer", priority: 5, render }),
|
||||
];
|
||||
const slots = getEditorSlots("preview-renderer");
|
||||
expect(slots.map((s) => s.pluginId)).toEqual(["c", "a", "b"]);
|
||||
expect(ownSlots("preview-renderer")).toEqual(["c", "a", "b"]);
|
||||
unregs.forEach((u) => u());
|
||||
});
|
||||
|
||||
test("re-registering same plugin+slot overrides (idempotent, HMR-safe)", () => {
|
||||
const unreg1 = registerEditorSlot({ pluginId: "x", slot: "inspector", render });
|
||||
const unreg2 = registerEditorSlot({ pluginId: "x", slot: "inspector", render });
|
||||
const slots = getEditorSlots("inspector");
|
||||
expect(slots).toHaveLength(1);
|
||||
expect(slots[0].pluginId).toBe("x");
|
||||
expect(ownSlots("inspector")).toEqual(["x"]);
|
||||
// 旧卸载函数不应误删新注册项
|
||||
unreg1();
|
||||
expect(getEditorSlots("inspector")).toHaveLength(1);
|
||||
expect(ownSlots("inspector")).toEqual(["x"]);
|
||||
unreg2();
|
||||
expect(getEditorSlots("inspector")).toHaveLength(0);
|
||||
expect(ownSlots("inspector")).toEqual([]);
|
||||
});
|
||||
|
||||
test("different plugins coexist in the same slot", () => {
|
||||
@@ -47,13 +49,14 @@ describe("editor slot registry", () => {
|
||||
registerEditorSlot({ pluginId: "p1", slot: "subtitle-tool", render }),
|
||||
registerEditorSlot({ pluginId: "p2", slot: "subtitle-tool", render }),
|
||||
];
|
||||
expect(getEditorSlots("subtitle-tool")).toHaveLength(2);
|
||||
expect(ownSlots("subtitle-tool")).toEqual(["p1", "p2"]);
|
||||
unregisterEditorSlot("p1", "subtitle-tool");
|
||||
expect(getEditorSlots("subtitle-tool").map((s) => s.pluginId)).toEqual(["p2"]);
|
||||
expect(ownSlots("subtitle-tool")).toEqual(["p2"]);
|
||||
unregs.forEach((u) => u());
|
||||
});
|
||||
|
||||
test("unknown slot returns empty", () => {
|
||||
expect(getEditorSlots("export-renderer")).toHaveLength(0);
|
||||
test("slots this file never registered into stay untouched", () => {
|
||||
// 真实插件会占用 export-renderer,因此断言「本文件没有向未注册的插槽留下贡献」。
|
||||
expect(ownSlots("export-renderer")).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user