(".swap-root")!
.click(),
);
expect(div!.querySelector('section[data-id="a"]')).not.toBeNull();
await act(async () => {
mounted.transact(() => {
mounted.updateBlock("b", { props: { flavor: "warning" } });
mounted.removeBlocks(["a"]);
});
});
// `b` keeps its own frame: a , not `a`'s .
expect(div!.querySelector('div[data-id="b"]')).not.toBeNull();
expect(uncaught).toEqual([]);
});
it("finds a React layout block's own element, not React's wrappers", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{
id: "box-0",
type: "frameBox",
children: [{ id: "box-child", type: "paragraph", content: "Child" }],
},
]);
const { posBeforeNode } = mounted.transact(({ doc }) =>
getNodeById("box-0", doc),
)!;
// Exercise the real nodeDOM result: the frame must be found even when
// React supplies surrounding wrappers.
const element = getBlockElementForNode(
mounted.prosemirrorView,
posBeforeNode,
"box-0",
);
expect(element).toBe(div!.querySelector(".frame-box"));
expect(uncaught).toEqual([]);
});
it("draws a React-framed block again when its type changes", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{
id: "alert-0",
type: "frameAlert",
content: "Heads up",
children: [{ id: "alert-child", type: "paragraph", content: "Body" }],
},
]);
expect(div!.querySelector(".alert-frame")).not.toBeNull();
await act(async () => {
mounted.updateBlock("alert-0", { type: "paragraph" });
});
expect(div!.querySelector(".alert-frame")).toBeNull();
expect(div!.textContent).toContain("Heads up");
expect(div!.textContent).toContain("Body");
expect(uncaught).toEqual([]);
});
it("renders plain nesting when the frame mounts no slot", async ({
setupEditor,
}) => {
await mountEditor(setupEditor, [
{
id: "alert-0",
type: "frameAlert",
props: { framed: false },
content: "Heads up",
children: [{ id: "alert-child", type: "paragraph", content: "Body" }],
},
]);
expect(div!.querySelector(".alert-frame")).toBeNull();
expect(div!.textContent).toContain("Heads up");
expect(div!.textContent).toContain("Body");
expect(uncaught).toEqual([]);
});
it("keeps React state, context, and child DOM through frame updates", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{
id: "alert",
type: "frameAlert",
content: "Title",
children: [{ id: "child", type: "paragraph", content: "Body" }],
},
]);
const counter = () =>
div!.querySelector(".frame-counter")!;
expect(counter().textContent).toBe("inside: 0");
expect(div!.querySelector(".alert-slot")?.textContent).toBe("TitleBody");
const child = div!.querySelector('[data-id="child"]');
const title = div!.querySelector(".alert-title");
await act(async () => counter().click());
expect(counter().textContent).toBe("inside: 1");
await act(async () => {
div!.querySelector(".frame-flavor")!.click();
});
expect(
div!.querySelector(".alert-frame")?.getAttribute("data-flavor"),
).toBe("warning");
await act(async () => {
root!.render();
});
expect(counter().textContent).toBe("updated: 1");
expect(div!.querySelector('[data-id="child"]')).toBe(child);
expect(div!.querySelector(".alert-title")).toBe(title);
expect(mounted.getBlock("child")?.content).toEqual([
{ type: "text", text: "Body", styles: {} },
]);
expect(uncaught).toEqual([]);
});
it("switches framing off and on without losing title or child node views", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{
id: "alert",
type: "frameAlert",
content: "Title",
children: [{ id: "child", type: "paragraph", content: "Body" }],
},
]);
expect(div!.querySelector(".alert-slot")?.textContent).toBe("TitleBody");
const child = div!.querySelector('[data-id="child"]');
await act(async () => {
mounted.updateBlock("alert", { props: { framed: false } });
});
expect(div!.querySelector(".alert-frame")).toBeNull();
expect(div!.textContent).toContain("TitleBody");
expect(div!.querySelector('[data-id="child"]')).toBe(child);
await act(async () => {
mounted.updateBlock("alert", { props: { framed: true } });
});
expect(div!.querySelector(".alert-slot")?.textContent).toBe("TitleBody");
expect(div!.querySelector('[data-id="child"]')).toBe(child);
expect(uncaught).toEqual([]);
});
it("unmounts the frame component when its block is removed", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{ id: "alert", type: "frameAlert", content: "Title" },
{ id: "after", type: "paragraph", content: "After" },
]);
expect(activeFrames).toBe(1);
await act(async () => {
mounted.removeBlocks(["alert"]);
});
expect(activeFrames).toBe(0);
expect(div!.querySelector(".alert-frame")).toBeNull();
expect(uncaught).toEqual([]);
});
it("exports a titled React frame around its content and children", async ({
setupEditor,
}) => {
// Without an explicit `toExternalHTML`, external HTML keeps the frame,
// with the content and the children in its slot.
const { editor } = await setupEditor({ mountType: false, schema });
const html = editor.blocksToHTMLLossy([
{
type: "frameAlert",
content: "Title",
children: [{ type: "paragraph", content: "Body" }],
},
]);
const dom = document.createElement("div");
dom.innerHTML = html;
const slot = dom.querySelector(".alert-frame .alert-slot");
expect(slot?.querySelector(".alert-title")?.textContent).toBe("Title");
expect(slot?.textContent).toContain("Body");
expect(activeFrames).toBe(0);
});
it("exports a declined frame as plain content and disposes its effects", async ({
setupEditor,
}) => {
const { editor } = await setupEditor({ mountType: false, schema });
const html = editor.blocksToHTMLLossy([
{
type: "frameAlert",
props: { framed: false },
content: "Title",
children: [{ type: "paragraph", content: "Body" }],
},
]);
expect(html).not.toContain("alert-frame");
expect(html).toContain("Title");
expect(html).toContain("Body");
expect(activeFrames).toBe(0);
});
it("keeps ordinary Shift-Tab behavior inside a frame without declared children", async ({
setupEditor,
}) => {
const mounted = await mountEditor(setupEditor, [
{
id: "toggle",
type: "frameToggle",
content: "Title",
children: [{ id: "child", type: "paragraph", content: "Body" }],
},
]);
expect(div!.querySelector(".toggle-frame")?.textContent).toBe("TitleBody");
const event = new KeyboardEvent("keydown", {
key: "Tab",
code: "Tab",
keyCode: 9,
shiftKey: true,
bubbles: true,
});
await act(async () => {
mounted.setTextCursorPosition("child", "start");
mounted.domElement!.dispatchEvent(event);
});
expect(mounted.getParentBlock("child")).toBeUndefined();
expect(mounted.getBlock("toggle")?.children).toHaveLength(0);
expect(uncaught).toEqual([]);
});
});