Skip to content

Commit

Permalink
refactor: refresh copy paste tests
Browse files Browse the repository at this point in the history
Here replaced renderJsx with renderData everywhere and refreshed
copy paste tests so they not break CI occasionally. Will help with
writing tests for global variables.
  • Loading branch information
TrySound committed Jan 24, 2025
1 parent 080168e commit d546673
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 283 deletions.
28 changes: 14 additions & 14 deletions apps/builder/app/builder/shared/commands.test.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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" />
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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" />
Expand All @@ -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>
Expand All @@ -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">
Expand All @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
156 changes: 156 additions & 0 deletions apps/builder/app/shared/copy-paste.test.tsx
Original file line number Diff line number Diff line change
@@ -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" },
}),
]);
});
27 changes: 6 additions & 21 deletions apps/builder/app/shared/copy-paste/asset-upload.test.tsx
Original file line number Diff line number Diff line change
@@ -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">
Expand All @@ -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[]) => {
Expand All @@ -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"
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Expand Down
Loading

0 comments on commit d546673

Please sign in to comment.