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.

refactor: refresh copy paste tests (#4782)

Bogdan Chadkinc562e1c6a75d
diff --git a/apps/builder/app/builder/shared/commands.test.tsx b/apps/builder/app/builder/shared/commands.test.tsx
index d87b0668e2..dee00a876f 100644
--- a/apps/builder/app/builder/shared/commands.test.tsx
+++ b/apps/builder/app/builder/shared/commands.test.tsx
@@ -1,7 +1,7 @@
import { describe, expect, test } from "vitest";
import * as baseMetas from "@webstudio-is/sdk-components-react/metas";
import { createDefaultPages } from "@webstudio-is/project-build";
-import { renderJsx, $ } from "@webstudio-is/template";
+import { $, renderData } from "@webstudio-is/template";
import {
$instances,
$pages,
@@ -21,7 +21,7 @@ $awareness.set({ pageId: "" });
describe("deleteInstance", () => {
test("delete selected instance and select next one", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id="parent">
<$.Box ws:id="child1"></$.Box>
@@ -42,7 +42,7 @@ describe("deleteInstance", () => {
test("delete selected instance and select previous one", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id="parent">
<$.Box ws:id="child1"></$.Box>
@@ -63,7 +63,7 @@ describe("deleteInstance", () => {
test("delete selected instance and select parent one", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id="parent">
<$.Box ws:id="child1"></$.Box>
@@ -80,7 +80,7 @@ describe("deleteInstance", () => {
describe("wrap in", () => {
test("wrap instance in link", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id="box"></$.Box>
</$.Body>
@@ -89,7 +89,7 @@ describe("wrap in", () => {
selectInstance(["box", "body"]);
wrapIn("Link");
expect($instances.get()).toEqual(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Link ws:id={expect.any(String)}>
<$.Box ws:id="box"></$.Box>
@@ -101,7 +101,7 @@ describe("wrap in", () => {
test("wrap image in box", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Image ws:id="image" />
</$.Body>
@@ -110,7 +110,7 @@ describe("wrap in", () => {
selectInstance(["image", "body"]);
wrapIn("Box");
expect($instances.get()).toEqual(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id={expect.any(String)}>
<$.Image ws:id="image" />
@@ -121,7 +121,7 @@ describe("wrap in", () => {
});
test("avoid wrapping text with link in link", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Text ws:id="text">
<$.RichTextLink ws:id="richtextlink"></$.RichTextLink>
@@ -136,7 +136,7 @@ describe("wrap in", () => {
});
test("avoid wrapping textual content", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Text ws:id="text">
<$.Bold ws:id="bold"></$.Bold>
@@ -154,7 +154,7 @@ describe("wrap in", () => {
describe("unwrap", () => {
test("unwrap instance", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Image ws:id="image" />
@@ -166,7 +166,7 @@ describe("unwrap", () => {
selectInstance(["box", "body"]);
unwrap();
expect($instances.get()).toEqual(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Image ws:id="image" />
<$.Text ws:id="text">text</$.Text>
@@ -176,7 +176,7 @@ describe("unwrap", () => {
});
test("avoid unwrapping textual instance", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Text ws:id="text1">text</$.Text>
<$.Text ws:id="text2">
@@ -193,7 +193,7 @@ describe("unwrap", () => {
});
test("avoid unwrapping constrained instances", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
diff --git a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
index e486ff84ac..d2e058cdd6 100644
--- a/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
+++ b/apps/builder/app/canvas/features/text-editor/text-editor.stories.tsx
@@ -6,6 +6,7 @@ import { action } from "@storybook/addon-actions";
import { Box, Button, Flex } from "@webstudio-is/design-system";
import { theme } from "@webstudio-is/design-system";
import type { Instance, Instances } from "@webstudio-is/sdk";
+import { $, renderData } from "@webstudio-is/template";
import {
$instances,
$pages,
@@ -15,7 +16,6 @@ import {
} from "~/shared/nano-states";
import { TextEditor } from "./text-editor";
import { emitCommand, subscribeCommands } from "~/canvas/shared/commands";
-import { $, renderJsx } from "@webstudio-is/template";
import { $awareness } from "~/shared/awareness";
export default {
@@ -259,7 +259,7 @@ export const CursorPositioningUpDown: StoryFn<typeof TextEditor> = () => {
])
);
- return renderJsx(
+ return renderData(
<$.Body ws:id="bodyId">
<$.Box ws:id="boxAId">
Hello world <$.Bold ws:id="boldA">Hello world</$.Bold> Hello world
diff --git a/apps/builder/app/shared/copy-paste.test.tsx b/apps/builder/app/shared/copy-paste.test.tsx
new file mode 100644
index 0000000000..2d23b1da2b
--- /dev/null
+++ b/apps/builder/app/shared/copy-paste.test.tsx
@@ -0,0 +1,156 @@
+import { expect, test } from "vitest";
+import stripIndent from "strip-indent";
+import { createRegularStyleSheet } from "@webstudio-is/css-engine";
+import { createDefaultPages } from "@webstudio-is/project-build";
+import { ROOT_INSTANCE_ID, type WebstudioData } from "@webstudio-is/sdk";
+import { $, ws, css, renderData } from "@webstudio-is/template";
+import type { Project } from "@webstudio-is/project";
+import {
+ extractWebstudioFragment,
+ insertWebstudioFragmentCopy,
+} from "./instance-utils";
+import { $project } from "./nano-states";
+
+$project.set({ id: "current_project" } as Project);
+
+const createStub = (element: JSX.Element) => {
+ const project = {
+ pages: createDefaultPages({ rootInstanceId: "", systemDataSourceId: "" }),
+ ...renderData(element),
+ };
+ // global root instance is never stored in data
+ project.instances.delete(ROOT_INSTANCE_ID);
+ return project;
+};
+
+const toCss = (data: WebstudioData) => {
+ const sheet = createRegularStyleSheet();
+ sheet.addMediaRule("base");
+ for (const { instanceId, values } of data.styleSourceSelections.values()) {
+ for (const styleSourceId of values) {
+ const styleSource = data.styleSources.get(styleSourceId);
+ let name;
+ if (styleSource?.type === "local") {
+ name = `${instanceId}:local`;
+ }
+ if (styleSource?.type === "token") {
+ name = `${instanceId}:token(${styleSource.name})`;
+ }
+ if (name) {
+ const rule = sheet.addNestingRule(name);
+ for (const styleDecl of data.styles.values()) {
+ rule.setDeclaration({
+ breakpoint: styleDecl.breakpointId,
+ selector: styleDecl.state ?? "",
+ property: styleDecl.property,
+ value: styleDecl.value,
+ });
+ }
+ }
+ }
+ }
+ return sheet.cssText;
+};
+
+test("should add :root local styles", () => {
+ const oldProject = createStub(
+ <ws.root
+ ws:id={ROOT_INSTANCE_ID}
+ ws:style={css`
+ color: red;
+ `}
+ >
+ <$.Body></$.Body>
+ </ws.root>
+ );
+ const newProject = createStub(<$.Body></$.Body>);
+ const fragment = extractWebstudioFragment(oldProject, ROOT_INSTANCE_ID);
+ insertWebstudioFragmentCopy({
+ data: newProject,
+ fragment,
+ availableDataSources: new Set(),
+ });
+ expect(toCss(newProject)).toEqual(
+ stripIndent(`
+ @media all {
+ :root:local {
+ color: red
+ }
+ }
+ `).trim()
+ );
+});
+
+test("should merge :root local styles", () => {
+ const oldProject = createStub(
+ <ws.root
+ ws:id={ROOT_INSTANCE_ID}
+ ws:style={css`
+ color: red;
+ `}
+ >
+ <$.Body></$.Body>
+ </ws.root>
+ );
+ const newProject = createStub(
+ <ws.root
+ ws:id={ROOT_INSTANCE_ID}
+ ws:style={css`
+ font-size: medium;
+ `}
+ >
+ <$.Body></$.Body>
+ </ws.root>
+ );
+ const fragment = extractWebstudioFragment(oldProject, ROOT_INSTANCE_ID);
+ insertWebstudioFragmentCopy({
+ data: newProject,
+ fragment,
+ availableDataSources: new Set(),
+ });
+ expect(toCss(newProject)).toEqual(
+ stripIndent(`
+ @media all {
+ :root:local {
+ font-size: medium;
+ color: red
+ }
+ }
+ `).trim()
+ );
+});
+
+test("should copy local styles of duplicated instance", () => {
+ const project = createStub(
+ <$.Body>
+ <$.Box
+ ws:id="boxId"
+ ws:style={css`
+ color: red;
+ `}
+ ></$.Box>
+ </$.Body>
+ );
+ const fragment = extractWebstudioFragment(project, "boxId");
+ expect(Array.from(project.styles.values())).toEqual([
+ expect.objectContaining({
+ property: "color",
+ value: { type: "keyword", value: "red" },
+ }),
+ ]);
+ insertWebstudioFragmentCopy({
+ data: project,
+ fragment,
+ availableDataSources: new Set(),
+ });
+ expect(Array.from(project.styles.values())).toEqual([
+ expect.objectContaining({
+ property: "color",
+ value: { type: "keyword", value: "red" },
+ }),
+ expect.objectContaining({
+ property: "color",
+ value: { type: "keyword", value: "red" },
+ }),
+ ]);
+});
diff --git a/apps/builder/app/shared/copy-paste/asset-upload.test.tsx b/apps/builder/app/shared/copy-paste/asset-upload.test.tsx
index 0daa6ce0e2..db5717e0b3 100644
--- a/apps/builder/app/shared/copy-paste/asset-upload.test.tsx
+++ b/apps/builder/app/shared/copy-paste/asset-upload.test.tsx
@@ -1,10 +1,10 @@
import { expect, test } from "vitest";
-import { $, renderJsx, AssetValue } from "@webstudio-is/template";
-import { denormalizeSrcProps } from "./asset-upload";
+import { $, AssetValue, renderTemplate } from "@webstudio-is/template";
import type { StyleDecl, WebstudioFragment } from "@webstudio-is/sdk";
+import { denormalizeSrcProps } from "./asset-upload";
test("extractSrcProps works well", async () => {
- const inputData = renderJsx(
+ const data = renderTemplate(
<$.Body ws:id="boxA">
<$.Image ws:id="imageA" src="https://src-a/"></$.Image>
<$.Box ws:id="boxB">
@@ -13,19 +13,6 @@ test("extractSrcProps works well", async () => {
</$.Body>
);
- const data: WebstudioFragment = {
- assets: [],
- breakpoints: [],
- children: [],
- dataSources: [],
- instances: [...inputData.instances.values()],
- props: [...inputData.props.values()],
- resources: [],
- styles: [],
- styleSources: [],
- styleSourceSelections: [],
- };
-
const src2AssetId = (src: string) => `${src}asset-id`;
const uploadImages = async (srcs: string[]) => {
@@ -41,7 +28,7 @@ test("extractSrcProps works well", async () => {
const assetA = new AssetValue(src2AssetId("https://src-a/"));
const assetB = new AssetValue(src2AssetId("https://src-b/"));
- const desiredOutcome = renderJsx(
+ const desiredOutcome = renderTemplate(
<$.Body ws:id="boxA">
<$.Image
ws:id="imageA"
@@ -60,11 +47,9 @@ test("extractSrcProps works well", async () => {
</$.Body>
);
- expect(denormalizedData.instances).toEqual([
- ...desiredOutcome.instances.values(),
- ]);
+ expect(denormalizedData.instances).toEqual(desiredOutcome.instances);
- expect(denormalizedData.props).toEqual([...desiredOutcome.props.values()]);
+ expect(denormalizedData.props).toEqual(desiredOutcome.props);
});
test("it works well with no background-images", async () => {
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
index 812ce65c5c..c830c3e951 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
@@ -10,7 +10,7 @@ import {
type WebstudioFragment,
type Instance,
} from "@webstudio-is/sdk";
-import { $, renderJsx } from "@webstudio-is/template";
+import { $, renderData } from "@webstudio-is/template";
import * as defaultMetas from "@webstudio-is/sdk-components-react/metas";
import { __testing__ } from "./plugin-webflow";
import {
@@ -41,7 +41,7 @@ const equalFragment = (fragment: WebstudioFragment, jsx: JSX.Element) => {
});
});
- const expected = renderJsx(jsx);
+ const expected = renderData(jsx);
const expectedInstances = new Map();
for (const instance of expected.instances.values()) {
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index acce118cba..5bed715343 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -5,11 +5,10 @@ import { createDefaultPages } from "@webstudio-is/project-build";
import {
$,
ws,
- renderJsx,
ExpressionValue,
renderTemplate,
+ renderData,
} from "@webstudio-is/template";
-import { parseCss } from "@webstudio-is/css-data";
import * as defaultMetas from "@webstudio-is/sdk-components-react/metas";
import * as radixMetas from "@webstudio-is/sdk-components-react-radix/metas";
import type {
@@ -22,17 +21,13 @@ import type {
Resource,
StyleDecl,
StyleDeclKey,
- Styles,
StyleSource,
- StyleSources,
- StyleSourceSelections,
WebstudioData,
WebstudioFragment,
WsComponentMeta,
} from "@webstudio-is/sdk";
import {
encodeDataSourceVariable,
- getStyleDeclKey,
coreMetas,
portalComponent,
collectionComponent,
@@ -64,7 +59,6 @@ import {
$resources,
} from "./nano-states";
import { registerContainers } from "./sync";
-import { mapGroupBy } from "./shim";
import { $awareness, selectInstance } from "./awareness";
enableMapSet();
@@ -445,7 +439,7 @@ describe("insert instance children", () => {
describe("insert webstudio fragment at", () => {
test("insert multiple instances", () => {
- $instances.set(renderJsx(<$.Body ws:id="bodyId"></$.Body>).instances);
+ $instances.set(renderData(<$.Body ws:id="bodyId"></$.Body>).instances);
$styleSourceSelections.set(new Map());
$styleSources.set(new Map());
$breakpoints.set(new Map());
@@ -467,7 +461,7 @@ describe("insert webstudio fragment at", () => {
}
);
expect($instances.get()).toEqual(
- renderJsx(
+ renderData(
<$.Body ws:id="bodyId">
<$.Heading ws:id={expect.any(String)}></$.Heading>
<$.Paragraph ws:id={expect.any(String)}></$.Paragraph>
@@ -705,7 +699,7 @@ describe("reparent instance", () => {
test("reparent required child", () => {
$instances.set(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Tooltip ws:id="tooltip">
<$.TooltipTrigger ws:id="trigger"></$.TooltipTrigger>
@@ -722,7 +716,7 @@ describe("reparent instance", () => {
position: "end",
});
expect($instances.get()).toEqual(
- renderJsx(
+ renderData(
<$.Body ws:id="body">
<$.Tooltip ws:id="tooltip">
<$.TooltipContent ws:id="content"></$.TooltipContent>
@@ -2714,142 +2708,6 @@ describe("insert webstudio fragment copy", () => {
});
});
-describe("copy paste", () => {
- const css = (strings: TemplateStringsArray, ...values: string[]) => {
- let cssString = "";
- strings.forEach((string, index) => {
- cssString += string + (values[index] ?? "");
- });
- const styleSourceSelections: StyleSourceSelections = new Map();
- const styleSources: StyleSources = new Map();
- const styles: Styles = new Map();
- const parsed = mapGroupBy(
- parseCss(cssString).map((styleDecl) => ({
- ...styleDecl,
- selector: styleDecl.selector.startsWith("__")
- ? styleDecl.selector.slice(2)
- : styleDecl.selector,
- })),
- (parsedStyleDecl) => parsedStyleDecl.selector
- );
- for (const [selector, parsedStyles] of parsed) {
- const [instanceId, styleSourceId] = selector.split("__");
- styleSourceSelections.set(instanceId, {
- instanceId: instanceId,
- values: [styleSourceId],
- });
- styleSources.set(styleSourceId, { id: styleSourceId, type: "local" });
- for (const { property, value } of parsedStyles) {
- const styleDecl: StyleDecl = {
- breakpointId: "baseId",
- styleSourceId,
- property,
- value,
- };
- styles.set(getStyleDeclKey(styleDecl), styleDecl);
- }
- }
- return { styleSourceSelections, styleSources, styles };
- };
-
- test("should add :root local styles", () => {
- const oldProject = getWebstudioDataStub({
- ...renderJsx(<$.Body ws:id="oldProjectBodyId"></$.Body>),
- ...css`
- :root__oldprojectlocalid {
- color: red;
- }
- `,
- });
- const newProject = getWebstudioDataStub();
- const fragment = extractWebstudioFragment(oldProject, ":root");
- insertWebstudioFragmentCopy({
- data: newProject,
- fragment,
- availableDataSources: new Set(),
- });
- const [newStyleSourceId] = newProject.styleSources.keys();
- expect(newProject).toEqual(
- expect.objectContaining(css`
- :root__${newStyleSourceId} {
- color: red;
- }
- `)
- );
- });
-
- test("should merge :root local styles", () => {
- const oldProject = getWebstudioDataStub({
- ...renderJsx(<$.Body ws:id="oldProjectBodyId"></$.Body>),
- ...css`
- :root__oldprojectlocalid {
- color: red;
- }
- `,
- });
- const newProject = getWebstudioDataStub({
- ...renderJsx(<$.Body ws:id="oldProjectBodyId"></$.Body>),
- ...css`
- :root__newprojectlocalid {
- font-size: medium;
- }
- `,
- });
- const fragment = extractWebstudioFragment(oldProject, ":root");
- insertWebstudioFragmentCopy({
- data: newProject,
- fragment,
- availableDataSources: new Set(),
- });
- expect({
- styleSourceSelections: newProject.styleSourceSelections,
- styleSources: newProject.styleSources,
- styles: newProject.styles,
- }).toEqual(css`
- :root__newprojectlocalid {
- font-size: medium;
- color: red;
- }
- `);
- });
-
- test("should copy local styles of duplicated instance", () => {
- const project = getWebstudioDataStub({
- ...renderJsx(
- <$.Body ws:id="bodyId">
- <$.Box ws:id="boxId"></$.Box>
- </$.Body>
- ),
- ...css`
- boxId__boxlocalid {
- color: red;
- }
- `,
- });
- const fragment = extractWebstudioFragment(project, "boxId");
- insertWebstudioFragmentCopy({
- data: project,
- fragment,
- availableDataSources: new Set(),
- });
- const [_bodyId, _boxId, newBoxId] = project.instances.keys();
- const [_boxLocalId, newBoxLocalId] = project.styleSources.keys();
- expect({
- styleSourceSelections: project.styleSourceSelections,
- styleSources: project.styleSources,
- styles: project.styles,
- }).toEqual(css`
- boxId__boxlocalid {
- color: red;
- }
- /* escape potential leading digit */
- __${newBoxId}__${newBoxLocalId} {
- color: red;
- }
- `);
- });
-});
-
describe("find closest insertable", () => {
const newBoxFragment = createFragment({
children: [{ type: "id", value: "newBoxId" }],
@@ -2874,7 +2732,7 @@ describe("find closest insertable", () => {
});
test("puts in the end if closest instance is container", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="bodyId">
<$.Box ws:id="boxId">
<$.Paragraph ws:id="paragraphId">
@@ -2892,7 +2750,7 @@ describe("find closest insertable", () => {
});
test("puts in the end of root instance", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="bodyId">
<$.Paragraph ws:id="paragraphId"></$.Paragraph>
</$.Body>
@@ -2906,7 +2764,7 @@ describe("find closest insertable", () => {
});
test("puts in the end of root instance when page root only has text", () => {
- const { instances } = renderJsx(<$.Body ws:id="bodyId">text</$.Body>);
+ const { instances } = renderData(<$.Body ws:id="bodyId">text</$.Body>);
$instances.set(instances);
selectInstance(["bodyId"]);
expect(findClosestInsertable(newBoxFragment)).toEqual({
@@ -2916,7 +2774,7 @@ describe("find closest insertable", () => {
});
test("finds closest container and puts after its child within selection", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="bodyId">
<$.Paragraph ws:id="paragraphId">
<$.Bold ws:id="boldId"></$.Bold>
@@ -2932,7 +2790,7 @@ describe("find closest insertable", () => {
});
test("finds closest container that doesn't have an expression as a child", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="bodyId">
<$.Box ws:id="box1Id"></$.Box>
<$.Paragraph ws:id="paragraphId">
@@ -2950,14 +2808,14 @@ describe("find closest insertable", () => {
});
test("forbids inserting into :root", () => {
- const { instances } = renderJsx(<$.Body ws:id="bodyId"></$.Body>);
+ const { instances } = renderData(<$.Body ws:id="bodyId"></$.Body>);
$instances.set(instances);
selectInstance([":root"]);
expect(findClosestInsertable(newBoxFragment)).toEqual(undefined);
});
test("allow inserting into collection item", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="bodyId">
<ws.collection ws:id="collectionId">
<$.Box ws:id="boxId"></$.Box>
@@ -2973,7 +2831,7 @@ describe("find closest insertable", () => {
});
test("forbid inserting list item in body", () => {
- const { instances } = renderJsx(<$.Body ws:id="bodyId"></$.Body>);
+ const { instances } = renderData(<$.Body ws:id="bodyId"></$.Body>);
$instances.set(instances);
selectInstance(["bodyId"]);
const newListItemFragment = createFragment({
diff --git a/apps/builder/app/shared/matcher.test.tsx b/apps/builder/app/shared/matcher.test.tsx
index 8fd56595bd..c84c5f4dd5 100644
--- a/apps/builder/app/shared/matcher.test.tsx
+++ b/apps/builder/app/shared/matcher.test.tsx
@@ -1,9 +1,9 @@
import { describe, expect, test, vi } from "vitest";
import {
- renderJsx,
$,
ExpressionValue,
renderTemplate,
+ renderData,
} from "@webstudio-is/template";
import { coreMetas } from "@webstudio-is/sdk";
import * as baseMetas from "@webstudio-is/sdk-components-react/metas";
@@ -31,7 +31,7 @@ describe("is instance matching", () => {
test("matches self with self matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -47,7 +47,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -66,7 +66,7 @@ describe("is instance matching", () => {
test("matches self with negated self matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -82,7 +82,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -101,7 +101,7 @@ describe("is instance matching", () => {
test("matches parent with parent matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -120,7 +120,7 @@ describe("is instance matching", () => {
test("not matches ancestor with parent matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box">
@@ -141,7 +141,7 @@ describe("is instance matching", () => {
test("not matches another parent with parent matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
</$.Body>
@@ -158,7 +158,7 @@ describe("is instance matching", () => {
test("matches parent with negated parent matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
</$.Body>
@@ -175,7 +175,7 @@ describe("is instance matching", () => {
test("not matches parent with negated parent matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -194,7 +194,7 @@ describe("is instance matching", () => {
test("matches parent with ancestor matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -213,7 +213,7 @@ describe("is instance matching", () => {
test("matches parent with ancestor matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box">
@@ -234,7 +234,7 @@ describe("is instance matching", () => {
test("not matches another ancestor with ancestor matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -253,7 +253,7 @@ describe("is instance matching", () => {
test("matches ancestor with negated ancestor matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -272,7 +272,7 @@ describe("is instance matching", () => {
test("not matches ancestor with negated ancestor matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -291,7 +291,7 @@ describe("is instance matching", () => {
test("combines self, parent and ancestor matchers", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -317,7 +317,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -346,7 +346,7 @@ describe("is instance matching", () => {
test("negated ancestor matcher should not interfere with self relation", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list"></$.List>
</$.Body>
@@ -373,7 +373,7 @@ describe("is instance matching", () => {
];
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.List ws:id="list"></$.List>
@@ -386,7 +386,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list"></$.List>
</$.Body>
@@ -400,7 +400,7 @@ describe("is instance matching", () => {
test("matches a child with child matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -419,7 +419,7 @@ describe("is instance matching", () => {
test("matches a child with negated child matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list"></$.List>
</$.Body>
@@ -433,7 +433,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box"></$.Box>
@@ -449,7 +449,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -468,7 +468,7 @@ describe("is instance matching", () => {
test("not matches a parent without a child with child matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list"></$.List>
</$.Body>
@@ -485,7 +485,7 @@ describe("is instance matching", () => {
test("not matches a parent with different child with child matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box"></$.Box>
@@ -504,7 +504,7 @@ describe("is instance matching", () => {
test("matches a child with descendant matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -523,7 +523,7 @@ describe("is instance matching", () => {
test("matches a descendant with descendant matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box">
@@ -544,7 +544,7 @@ describe("is instance matching", () => {
test("matches a descendant with negated descendant matcher", () => {
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box"></$.Box>
@@ -560,7 +560,7 @@ describe("is instance matching", () => {
).toBeTruthy();
expect(
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.Box ws:id="box">
@@ -581,7 +581,7 @@ describe("is instance matching", () => {
test("provide error message when negated matcher is failed", () => {
const onError = vi.fn();
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box"></$.Box>
</$.Body>
@@ -595,7 +595,7 @@ describe("is instance matching", () => {
});
expect(onError).toHaveBeenLastCalledWith("Box or Text is not acceptable");
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -615,7 +615,7 @@ describe("is instance matching", () => {
test("provide error message when positive matcher is failed", () => {
const onError = vi.fn();
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
</$.Body>
@@ -629,7 +629,7 @@ describe("is instance matching", () => {
});
expect(onError).toHaveBeenLastCalledWith("Box or Text is missing");
isInstanceMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
</$.Body>
@@ -674,7 +674,7 @@ describe("is tree matching", () => {
test("match selected instance", () => {
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -687,7 +687,7 @@ describe("is tree matching", () => {
).toBeTruthy();
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
</$.Body>
@@ -701,7 +701,7 @@ describe("is tree matching", () => {
test("match all descendants", () => {
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -714,7 +714,7 @@ describe("is tree matching", () => {
).toBeTruthy();
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.ListItem ws:id="listitem"></$.ListItem>
<$.Box ws:id="box"></$.Box>
@@ -729,7 +729,7 @@ describe("is tree matching", () => {
test("match ancestors", () => {
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Tabs ws:id="tabs">
<$.Box ws:id="box">
@@ -744,7 +744,7 @@ describe("is tree matching", () => {
).toBeTruthy();
expect(
isTreeMatching({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Tabs ws:id="tabs">
<$.Box ws:id="box"></$.Box>
@@ -764,7 +764,7 @@ describe("is instance detachable", () => {
test("allow deleting one of matching instances", () => {
expect(
isInstanceDetachable({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Tabs ws:id="tabs">
<$.TabsList ws:id="list">
@@ -785,7 +785,7 @@ describe("is instance detachable", () => {
test("prevent deleting last matching instance", () => {
expect(
isInstanceDetachable({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Tabs ws:id="tabs">
<$.TabsList ws:id="list">
@@ -804,7 +804,7 @@ describe("is instance detachable", () => {
test("allow deleting when siblings not matching", () => {
expect(
isInstanceDetachable({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Tabs ws:id="tabs"></$.Tabs>
<$.Box ws:id="box"></$.Box>
@@ -819,7 +819,7 @@ describe("is instance detachable", () => {
describe("find closest instance matching fragment", () => {
test("finds closest list with list item fragment", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.List ws:id="list">
<$.ListItem ws:id="listitem"></$.ListItem>
@@ -855,7 +855,7 @@ describe("find closest instance matching fragment", () => {
});
test("finds button parent with button fragment", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Button ws:id="button"></$.Button>
</$.Body>
@@ -872,7 +872,7 @@ describe("find closest instance matching fragment", () => {
});
test("finds button parent with button+span fragment", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Button ws:id="button"></$.Button>
</$.Body>
@@ -895,7 +895,7 @@ describe("find closest instance matching fragment", () => {
test("report first error", () => {
const onError = vi.fn();
- const { instances } = renderJsx(<$.Body ws:id="body"></$.Body>);
+ const { instances } = renderData(<$.Body ws:id="body"></$.Body>);
const fragment = renderTemplate(<$.ListItem ws:id="listitem"></$.ListItem>);
findClosestInstanceMatchingFragment({
metas,
@@ -912,7 +912,7 @@ describe("find closest container", () => {
test("skips non-container instances", () => {
expect(
findClosestContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Image ws:id="image" />
@@ -928,7 +928,7 @@ describe("find closest container", () => {
test("allow containers with text", () => {
expect(
findClosestContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Box ws:id="box-with-text">text</$.Box>
@@ -944,7 +944,7 @@ describe("find closest container", () => {
test("allow containers with expression", () => {
expect(
findClosestContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Box ws:id="box-with-expr">
@@ -962,7 +962,7 @@ describe("find closest container", () => {
test("allow root with text", () => {
expect(
findClosestContainer({
- ...renderJsx(<$.Body ws:id="body">text</$.Body>),
+ ...renderData(<$.Body ws:id="body">text</$.Body>),
metas,
instanceSelector: ["body"],
})
@@ -974,7 +974,7 @@ describe("find closest non textual container", () => {
test("skips non-container instances", () => {
expect(
findClosestNonTextualContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Image ws:id="image" />
@@ -990,7 +990,7 @@ describe("find closest non textual container", () => {
test("skips containers with text", () => {
expect(
findClosestNonTextualContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Box ws:id="box-with-text">text</$.Box>
@@ -1006,7 +1006,7 @@ describe("find closest non textual container", () => {
test("skips containers with expression", () => {
expect(
findClosestNonTextualContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Box ws:id="box-with-expr">
@@ -1024,7 +1024,7 @@ describe("find closest non textual container", () => {
test("skips containers with rich text children", () => {
expect(
findClosestNonTextualContainer({
- ...renderJsx(
+ ...renderData(
<$.Body ws:id="body">
<$.Box ws:id="box">
<$.Box ws:id="box-with-bold">
@@ -1042,7 +1042,7 @@ describe("find closest non textual container", () => {
test("allow root with text", () => {
expect(
findClosestNonTextualContainer({
- ...renderJsx(<$.Body ws:id="body">text</$.Body>),
+ ...renderData(<$.Body ws:id="body">text</$.Body>),
metas,
instanceSelector: ["body"],
})
diff --git a/apps/builder/app/shared/style-object-model.test.tsx b/apps/builder/app/shared/style-object-model.test.tsx
index ab947602eb..108a24f27b 100644
--- a/apps/builder/app/shared/style-object-model.test.tsx
+++ b/apps/builder/app/shared/style-object-model.test.tsx
@@ -9,7 +9,7 @@ import {
type StyleDecl,
getStyleDeclKey,
} from "@webstudio-is/sdk";
-import { $, renderJsx } from "@webstudio-is/template";
+import { $, renderData } from "@webstudio-is/template";
import { parseCss } from "@webstudio-is/css-data";
import type { StyleValue } from "@webstudio-is/css-engine";
import {
@@ -59,7 +59,7 @@ const createModel = ({
};
styles.set(getStyleDeclKey(styleDecl), styleDecl);
}
- const { instances, props } = renderJsx(jsx);
+ const { instances, props } = renderData(jsx);
const styleSourceSelections: StyleSourceSelections = new Map();
for (const prop of props.values()) {
if (prop.name === "class" && prop.type === "string") {
diff --git a/packages/sdk/src/instances-utils.test.tsx b/packages/sdk/src/instances-utils.test.tsx
index a540a81034..c93d961775 100644
--- a/packages/sdk/src/instances-utils.test.tsx
+++ b/packages/sdk/src/instances-utils.test.tsx
@@ -1,5 +1,5 @@
import { expect, test } from "vitest";
-import { $, renderJsx } from "@webstudio-is/template";
+import { $, renderData } from "@webstudio-is/template";
import {
findTreeInstanceIds,
findTreeInstanceIdsExcludingSlotDescendants,
@@ -7,7 +7,7 @@ import {
} from "./instances-utils";
test("find all tree instances", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="1">
<$.Box ws:id="2"></$.Box>
<$.Box ws:id="3">
@@ -20,7 +20,7 @@ test("find all tree instances", () => {
});
test("find all tree instances excluding slot descendants", () => {
- const { instances } = renderJsx(
+ const { instances } = renderData(
<$.Body ws:id="body">
<$.Box ws:id="box1">
<$.Slot ws:id="slot">
@@ -38,7 +38,7 @@ test("find all tree instances excluding slot descendants", () => {
});
test("include not existing/virtual instance", () => {
- const { instances } = renderJsx(<$.Body ws:id="1"></$.Body>);
+ const { instances } = renderData(<$.Body ws:id="1"></$.Body>);
expect(findTreeInstanceIds(instances, ":root")).toEqual(new Set([":root"]));
expect(
findTreeInstanceIdsExcludingSlotDescendants(instances, ":root")
diff --git a/packages/sdk/src/resources-generator.test.tsx b/packages/sdk/src/resources-generator.test.tsx
index 4ba97b1ff4..7b3a18654a 100644
--- a/packages/sdk/src/resources-generator.test.tsx
+++ b/packages/sdk/src/resources-generator.test.tsx
@@ -1,12 +1,11 @@
import { expect, test } from "vitest";
-import { renderJsx, $ } from "@webstudio-is/template";
+import { renderData, $ } from "@webstudio-is/template";
import type { Page } from "./schema/pages";
import { createScope } from "./scope";
import {
generateResources,
replaceFormActionsWithResources,
} from "./resources-generator";
-import type { Resource } from "./schema/resources";
const toMap = <T extends { id: string }>(list: T[]) =>
new Map(list.map((item) => [item.id, item] as const));
@@ -322,10 +321,9 @@ test("generate action resource", () => {
});
test("replace form action with resource", () => {
- const data = {
- resources: toMap<Resource>([]),
- ...renderJsx(<$.Form ws:id="formId" action="https://my-url.com"></$.Form>),
- };
+ const data = renderData(
+ <$.Form ws:id="formId" action="https://my-url.com"></$.Form>
+ );
replaceFormActionsWithResources(data);
expect(data.props).toEqual(
toMap([
@@ -352,10 +350,7 @@ test("replace form action with resource", () => {
});
test("ignore empty form action", () => {
- const data = {
- resources: toMap<Resource>([]),
- ...renderJsx(<$.Form ws:id="formId" action=""></$.Form>),
- };
+ const data = renderData(<$.Form ws:id="formId" action=""></$.Form>);
replaceFormActionsWithResources(data);
expect(data.props).toEqual(
toMap([
diff --git a/packages/template/src/jsx.ts b/packages/template/src/jsx.ts
index 4e30d82dff..90e9aee2ef 100644
--- a/packages/template/src/jsx.ts
+++ b/packages/template/src/jsx.ts
@@ -4,9 +4,7 @@ import type {
Breakpoint,
DataSource,
Instance,
- Instances,
Prop,
- Props,
StyleDecl,
StyleSource,
StyleSourceSelection,
@@ -383,21 +381,6 @@ export const renderData = (root: JSX.Element): Omit<WebstudioData, "pages"> => {
};
};
-export const renderJsx = (
- root: JSX.Element
-): {
- instances: Instances;
- props: Props;
-} => {
- const fragment = renderTemplate(root);
- return {
- instances: new Map(
- fragment.instances.map((instance) => [instance.id, instance])
- ),
- props: new Map(fragment.props.map((prop) => [prop.id, prop])),
- };
-};
-
type ComponentProps = Record<string, unknown> &
Record<`${string}:expression`, string> & {
"ws:id"?: string;