Files
webstudio/apps/builder/app/shared/copy-paste/plugin-jsx.test.ts
T
Oleg Isonen fc5b7e140e test: Cover Builder tRPC request workflows (#6720)
Builder features that use tRPC need request-level coverage beyond
publishing. This PR shares the publish request interceptor and uses it
to test uncovered UI flows across the HTTP boundary. It only changes
test support and tests; product behavior and API contracts stay
unchanged.

## Todos
- [x] Move the tRPC interceptor into shared Builder test support and
update its publish tests.
- [x] Replace the JSX paste test’s tRPC client mock with an intercepted
request.
- [x] Test workspace member invitations for success and server
rejection.
- [x] Test accepting and declining notifications.
- [x] Test backup loading and both successful and failed restores.
- [x] Make the shared interceptor reject invalid batch requests, enforce
each request's method, and reject unregistered routes.
- [x] Review Builder docs; the existing dashboard guide already
describes invitations, and these test-only changes need no doc update.
- [x] Run focused browser tests, Builder typecheck, lint, and formatting
checks.
- [x] Wait for CI on the latest commit.

## Verification
- Publish browser tests: 19 files, 199 tests passed.
- Shared tRPC, notifications, and backups: 3 files, 12 tests passed.
- JSX paste request tests: 1 file, 6 tests passed.
- Workspace member request tests: 1 file, 2 tests passed.
- New regression tests failed against the previous implementation and
passed with the fix.
- Builder typecheck, changed-file lint, formatting, diff checks, and CI
passed.

Ref #6719
2026-11-19 10:29:28 +01:00

153 lines
4.6 KiB
TypeScript

import {
afterAll,
beforeAll,
beforeEach,
describe,
expect,
test,
vi,
} from "vitest";
import { createEmptyWebstudioFragment } from "@webstudio-is/project-build/runtime";
import { inspectWebstudioJsxFragmentSyntax } from "@webstudio-is/project-build/transfer";
import { $project } from "../sync/data-stores";
import { updateCsrfToken } from "../csrf.client";
import { createTrpcInterceptor } from "../trpc/trpc-test-utils";
const mocks = vi.hoisted(() => ({
insertWebstudioFragmentAt: vi.fn(),
toastError: vi.fn(),
}));
vi.mock("../instance-utils/insert", async (importOriginal) => ({
...(await importOriginal()),
insertWebstudioFragmentAt: mocks.insertWebstudioFragmentAt,
}));
vi.mock("../builder-api", async (importOriginal) => ({
...(await importOriginal()),
builderApi: {
toast: {
error: mocks.toastError,
warn: vi.fn(),
},
},
}));
let requestInput: unknown;
let createFragmentResponse: unknown = createEmptyWebstudioFragment();
let jsx: typeof import("./plugin-jsx").jsx;
let isLikelyWebstudioJsx: typeof import("./plugin-jsx").isLikelyWebstudioJsx;
let pasteHandled: typeof import("./copy-paste").pasteHandled;
let pasteIgnored: typeof import("./copy-paste").pasteIgnored;
const interceptor = createTrpcInterceptor({
GET: {
"build.createJsxFragment": (input) => {
requestInput = input;
return createFragmentResponse;
},
},
});
beforeAll(async () => {
interceptor.start();
({ pasteHandled, pasteIgnored } = await import("./copy-paste"));
({ jsx, isLikelyWebstudioJsx } = await import("./plugin-jsx"));
});
afterAll(() => interceptor.stop());
describe("jsx paste plugin", () => {
beforeEach(() => {
vi.clearAllMocks();
requestInput = undefined;
createFragmentResponse = createEmptyWebstudioFragment();
updateCsrfToken("test-token");
$project.set({ id: "project-id" } as never);
});
test("detects only Webstudio JSX", () => {
expect(isLikelyWebstudioJsx("<Box />")).toBe(true);
expect(isLikelyWebstudioJsx('<ws.element ws:tag="section" />')).toBe(true);
expect(isLikelyWebstudioJsx("<Switch />")).toBe(true);
expect(isLikelyWebstudioJsx("<VideoAnimation />")).toBe(true);
expect(isLikelyWebstudioJsx("</Box>")).toBe(false);
expect(isLikelyWebstudioJsx("<section>HTML</section>")).toBe(false);
expect(isLikelyWebstudioJsx("## Markdown")).toBe(false);
});
test("can import JSX syntax inspection without the Node evaluator", () => {
expect(() => inspectWebstudioJsxFragmentSyntax("<Box />")).not.toThrow();
expect(() =>
inspectWebstudioJsxFragmentSyntax("<Box data-secret={process.env} />")
).toThrow('Do not access "process" in JSX fragments');
});
test("converts and inserts Webstudio JSX", async () => {
const fragment = createEmptyWebstudioFragment();
createFragmentResponse = fragment;
mocks.insertWebstudioFragmentAt.mockReturnValue(true);
const result = await jsx.onPaste?.("<Box />");
expect(requestInput).toEqual({
projectId: "project-id",
source: "<Box />",
});
expect(result).toEqual(pasteHandled);
expect(mocks.insertWebstudioFragmentAt).toHaveBeenCalledWith(
fragment,
undefined,
"theirs",
expect.objectContaining({
onBreakpointLimitMerge: expect.any(Function),
})
);
});
test("does not claim non-Webstudio JSX text", async () => {
await expect(jsx.onPaste?.("<section>HTML</section>")).resolves.toEqual(
pasteIgnored
);
expect(requestInput).toBeUndefined();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
});
test("does not claim JSX without a current project", async () => {
$project.set(undefined);
await expect(jsx.onPaste?.("<Box />")).resolves.toEqual(pasteIgnored);
expect(requestInput).toBeUndefined();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
});
test("reports conversion errors and stops paste fallback", async () => {
createFragmentResponse = new Response(
JSON.stringify([
{
error: {
message: "Invalid JSX",
code: -32600,
data: { code: "BAD_REQUEST", httpStatus: 400 },
},
},
]),
{
status: 400,
headers: { "Content-Type": "application/json" },
}
);
await expect(jsx.onPaste?.('<Box ws:id="0" />')).resolves.toEqual({
success: false,
error: "Invalid JSX",
});
expect(mocks.toastError).not.toHaveBeenCalled();
expect(mocks.insertWebstudioFragmentAt).not.toHaveBeenCalled();
expect(requestInput).toEqual({
projectId: "project-id",
source: '<Box ws:id="0" />',
});
});
});