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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: use jsx to represent instances in tests (#3568)
Bogdan Chadkincf5c6b9f29be
diff --git a/packages/jest-config/esbuild-jest.js b/packages/jest-config/esbuild-jest.jsindex a1000e4c34..2909583371 100644--- a/packages/jest-config/esbuild-jest.js+++ b/packages/jest-config/esbuild-jest.js@@ -12,6 +12,7 @@ const createTransformer = () => ({const result = transformSync(content, {loader,+ jsx: "automatic",format: "esm",target: "es2022",// Sourcemaps are necessary for inline snapshots to workdiff --git a/packages/react-sdk/src/component-generator.test.ts b/packages/react-sdk/src/component-generator.test.tsxsimilarity index 100%rename from packages/react-sdk/src/component-generator.test.tsrename to packages/react-sdk/src/component-generator.test.tsxdiff --git a/packages/react-sdk/src/css/css.test.ts b/packages/react-sdk/src/css/css.test.tsxsimilarity index 100%rename from packages/react-sdk/src/css/css.test.tsrename to packages/react-sdk/src/css/css.test.tsxdiff --git a/packages/react-sdk/src/embed-template.test.ts b/packages/react-sdk/src/embed-template.test.tsxsimilarity index 100%rename from packages/react-sdk/src/embed-template.test.tsrename to packages/react-sdk/src/embed-template.test.tsxdiff --git a/packages/react-sdk/src/instance-utils.test.ts b/packages/react-sdk/src/instance-utils.test.tsxsimilarity index 100%rename from packages/react-sdk/src/instance-utils.test.tsrename to packages/react-sdk/src/instance-utils.test.tsxdiff --git a/packages/sdk/package.json b/packages/sdk/package.jsonindex 2ba11d5d62..64fbc5069a 100644--- a/packages/sdk/package.json+++ b/packages/sdk/package.json@@ -7,9 +7,14 @@"license": "AGPL-3.0-or-later","type": "module","exports": {- "webstudio": "./src/index.ts",- "types": "./lib/types/index.d.ts",- "import": "./lib/index.js"+ ".": {+ "webstudio": "./src/index.ts",+ "types": "./lib/types/index.d.ts",+ "import": "./lib/index.js"+ },+ "./testing": {+ "webstudio": "./src/testing.ts"+ }},"files": ["lib/*",diff --git a/packages/sdk/src/instances-utils.test.ts b/packages/sdk/src/instances-utils.test.tsdeleted file mode 100644index 2d4b173974..0000000000--- a/packages/sdk/src/instances-utils.test.ts+++ /dev/null@@ -1,76 +0,0 @@-import { expect, test } from "@jest/globals";-import type { Instance, Instances } from "./schema/instances";-import {- findTreeInstanceIds,- findTreeInstanceIdsExcludingSlotDescendants,- parseComponentName,-} from "./instances-utils";--const createInstance = (- id: Instance["id"],- component: string,- children: Instance["children"]-): Instance => {- return {- type: "instance",- id,- component,- children,- };-};--const createInstancePair = (- id: Instance["id"],- component: string,- children: Instance["children"]-) => {- return [id, createInstance(id, component, children)] as const;-};--test("find all tree instances", () => {- const instances: Instances = new Map([- createInstancePair("1", "Body", [{ type: "id", value: "3" }]),- // this is outside of subtree- createInstancePair("2", "Box", []),- // these should be matched- createInstancePair("3", "Box", [- { type: "id", value: "4" },- { type: "id", value: "5" },- ]),- createInstancePair("4", "Box", []),- createInstancePair("5", "Box", []),- // this one is from other tree- createInstancePair("6", "Box", []),- ]);- expect(findTreeInstanceIds(instances, "3")).toEqual(new Set(["3", "4", "5"]));-});--test("find all tree instances excluding slot descendants", () => {- const instances: Instances = new Map([- createInstancePair("root", "Body", [- { type: "id", value: "box1" },- { type: "id", value: "box2" },- ]),- // this is outside of subtree- createInstancePair("outside", "Box", []),- // these should be matched- createInstancePair("box1", "Box", [- { type: "id", value: "slot11" },- { type: "id", value: "box12" },- ]),- createInstancePair("slot11", "Slot", [- { type: "id", value: "box111" },- { type: "id", value: "box112" },- ]),- createInstancePair("box12", "Box", []),- createInstancePair("box2", "Box", []),- ]);- expect(- findTreeInstanceIdsExcludingSlotDescendants(instances, "box1")- ).toEqual(new Set(["box1", "box12", "slot11"]));-});--test("extract short name and namespace from component name", () => {- expect(parseComponentName("Box")).toEqual([undefined, "Box"]);- expect(parseComponentName("radix:Box")).toEqual(["radix", "Box"]);-});diff --git a/packages/sdk/src/instances-utils.test.tsx b/packages/sdk/src/instances-utils.test.tsxnew file mode 100644index 0000000000..87d4232036--- /dev/null+++ b/packages/sdk/src/instances-utils.test.tsx@@ -0,0 +1,43 @@+import { expect, test } from "@jest/globals";+import { $, renderJsx } from "./jsx";+import {+ findTreeInstanceIds,+ findTreeInstanceIdsExcludingSlotDescendants,+ parseComponentName,+} from "./instances-utils";++test("find all tree instances", () => {+ const { instances } = renderJsx(+ <$.Body ws:id="1">+ <$.Box ws:id="2"></$.Box>+ <$.Box ws:id="3">+ <$.Box ws:id="4"></$.Box>+ <$.Box ws:id="5"></$.Box>+ </$.Box>+ </$.Body>+ );+ expect(findTreeInstanceIds(instances, "3")).toEqual(new Set(["3", "4", "5"]));+});++test("find all tree instances excluding slot descendants", () => {+ const { instances } = renderJsx(+ <$.Body ws:id="body">+ <$.Box ws:id="box1">+ <$.Slot ws:id="slot">+ <$.Box ws:id="slotbox1"></$.Box>+ <$.Box ws:id="slotbox2"></$.Box>+ </$.Slot>+ <$.Box ws:id="box2"></$.Box>+ </$.Box>+ <$.Box ws:id="box3"></$.Box>+ </$.Body>+ );+ expect(+ findTreeInstanceIdsExcludingSlotDescendants(instances, "box1")+ ).toEqual(new Set(["box1", "slot", "box2"]));+});++test("extract short name and namespace from component name", () => {+ expect(parseComponentName("Box")).toEqual([undefined, "Box"]);+ expect(parseComponentName("radix:Box")).toEqual(["radix", "Box"]);+});diff --git a/packages/sdk/src/jsx.test.tsx b/packages/sdk/src/jsx.test.tsxnew file mode 100644index 0000000000..f6f0121652--- /dev/null+++ b/packages/sdk/src/jsx.test.tsx@@ -0,0 +1,86 @@+import { expect, test } from "@jest/globals";+import { $, renderJsx } from "./jsx";++test("render jsx into instances with generated id", () => {+ const { instances } = renderJsx(+ <$.Body>+ <$.Box></$.Box>+ <$.Box></$.Box>+ </$.Body>+ );+ expect(instances).toEqual(+ new Map([+ [+ "0",+ {+ type: "instance",+ id: "0",+ component: "Body",+ children: [+ { type: "id", value: "1" },+ { type: "id", value: "2" },+ ],+ },+ ],+ [+ "1",+ {+ type: "instance",+ id: "1",+ component: "Box",+ children: [],+ },+ ],+ [+ "2",+ {+ type: "instance",+ id: "2",+ component: "Box",+ children: [],+ },+ ],+ ])+ );+});++test("override generated ids with ws:id prop", () => {+ const { instances } = renderJsx(+ <$.Body ws:id="custom1">+ <$.Box ws:id="custom2">+ <$.Span ws:id="custom3"></$.Span>+ </$.Box>+ </$.Body>+ );+ expect(instances).toEqual(+ new Map([+ [+ "custom1",+ {+ type: "instance",+ id: "custom1",+ component: "Body",+ children: [{ type: "id", value: "custom2" }],+ },+ ],+ [+ "custom2",+ {+ type: "instance",+ id: "custom2",+ component: "Box",+ children: [{ type: "id", value: "custom3" }],+ },+ ],+ [+ "custom3",+ {+ type: "instance",+ id: "custom3",+ component: "Span",+ children: [],+ },+ ],+ ])+ );+});diff --git a/packages/sdk/src/jsx.ts b/packages/sdk/src/jsx.tsnew file mode 100644index 0000000000..0a4f72b257--- /dev/null+++ b/packages/sdk/src/jsx.ts@@ -0,0 +1,66 @@+import type { ReactNode } from "react";+import type { Instances } from "./schema/instances";++const traverseJsx = (+ element: JSX.Element,+ callback: (element: JSX.Element, children: JSX.Element[]) => void+) => {+ const children = Array.isArray(element.props?.children)+ ? element.props?.children+ : element.props?.children+ ? [element.props?.children]+ : [];+ callback(element, children);+ for (const child of children) {+ traverseJsx(child, callback);+ }+};++export const renderJsx = (root: JSX.Element) => {+ let lastId = -1;+ const instances: Instances = new Map();+ const ids = new Map<unknown, string>();+ const getId = (key: unknown) => {+ let id = ids.get(key);+ if (id === undefined) {+ lastId += 1;+ id = lastId.toString();+ ids.set(key, id);+ }+ return id;+ };+ traverseJsx(root, (element, children) => {+ const id = element.props?.["ws:id"] ?? getId(element);+ const component = element.type.displayName;+ instances.set(id, {+ type: "instance",+ id,+ component,+ children: children.map((child) => ({+ type: "id",+ value: child.props?.["ws:id"] ?? getId(child),+ })),+ });+ });+ return {+ instances,+ };+};++type Props = {+ "ws:id"?: string;+ children?: ReactNode;+};++type Component = { displayName: string } & ((props: Props) => ReactNode);++export const $: Record<string, Component> = new Proxy(+ {},+ {+ get(_target, prop) {+ const component: Component = () => undefined;+ component.displayName = prop as string;+ return component;+ },+ }+);diff --git a/packages/sdk/src/testing.ts b/packages/sdk/src/testing.tsnew file mode 100644index 0000000000..02c01f8332--- /dev/null+++ b/packages/sdk/src/testing.ts@@ -0,0 +1 @@+export * from "./jsx";diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 2971f5becf..d240f72e2f 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -18292,7 +18292,7 @@ snapshots:dependencies:- nanoid: 3.3.6+ nanoid: 3.3.7picocolors: 1.0.1source-map-js: 1.0.2