Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

1.7 KB
Raw Blame
// Locks the public JSX component and intrinsic-element spellings at the
// server evaluator boundary.
import { expect, test } from "vitest";
import { evaluateWebstudioJsxFragment } from "./fragment.server";
import { webstudioJsxFragmentInputDescription } from "./bindings";

test("uses intrinsic tags and direct component identifiers", async () => {
  const fragment = await evaluateWebstudioJsxFragment(
    '<section><Heading tag="h2">Title</Heading></section>'
  );

  expect(fragment.instances).toMatchObject([
    { component: "ws:element", tag: "section" },
    { component: "Heading" },
  ]);
  expect(fragment.props).toContainEqual(
    expect.objectContaining({
      instanceId: fragment.instances[1]?.id,
      name: "tag",
      value: "h2",
    })
  );
});

test("uses a stable prefix for namespaced component-name collisions", async () => {
  const fragment = await evaluateWebstudioJsxFragment("<RadixCheckbox />");

  expect(fragment.instances).toMatchObject([
    {
      component: "@webstudio-is/sdk-components-react-radix:Checkbox",
    },
  ]);
});

test("rejects component member syntax", async () => {
  await expect(
    evaluateWebstudioJsxFragment("<Library.Card />")
  ).rejects.toThrow("JSX component namespaces are not supported");
});

test("keeps internal validation errors out of the public JSX environment", async () => {
  const fragment = await evaluateWebstudioJsxFragment(
    "<div title={typeof TemplateValidationError} />"
  );
  expect(fragment.props).toContainEqual(
    expect.objectContaining({
      name: "title",
      type: "string",
      value: "undefined",
    })
  );
  expect(webstudioJsxFragmentInputDescription).not.toContain(
    "TemplateValidationError"
  );
});