Files
co-lite/solvway-admin-portal/app/components/pages/CanvasFrame.test.tsx
T

29 lines
1.5 KiB
TypeScript

import { render, waitFor, within } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { CanvasFrame } from "./CanvasFrame";
// add-page-visual-editor: the canvas renders into its own blank document, with the editor's styles and theme.
const frame = () => document.querySelector("iframe") as HTMLIFrameElement;
describe("CanvasFrame", () => {
it("renders its children inside the frame's document, at the given size, carrying the editor's styles and theme", async () => {
const style = document.head.appendChild(document.createElement("style"));
style.textContent = ".x{color:red}";
document.documentElement.classList.add("dark");
try {
render(<CanvasFrame width={390} height={700} title="Canvas"><p>On the canvas</p></CanvasFrame>);
expect(frame()).toHaveStyle({ width: "390px", height: "700px" });
const inner = () => frame().contentDocument!;
await waitFor(() => expect(within(inner().body).getByText("On the canvas")).toBeInTheDocument());
expect(document.body).not.toHaveTextContent("On the canvas");
expect(inner().documentElement).toHaveClass("dark");
expect(inner().head.querySelector("style")?.textContent).toBe(".x{color:red}");
document.documentElement.classList.remove("dark");
await waitFor(() => expect(inner().documentElement).not.toHaveClass("dark"));
} finally {
style.remove();
document.documentElement.classList.remove("dark");
}
});
});