Skip to content

refactor: simplify template format #4607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 11 additions & 40 deletions apps/builder/app/shared/matcher.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { describe, expect, test, vi } from "vitest";
import type { JSX } from "react";
import { renderJsx, $, ExpressionValue } from "@webstudio-is/template";
import {
renderJsx,
$,
ExpressionValue,
renderTemplate,
} from "@webstudio-is/template";
import { coreMetas } from "@webstudio-is/react-sdk";
import * as baseMetas from "@webstudio-is/sdk-components-react/metas";
import type { WsComponentMeta } from "@webstudio-is/react-sdk";
import type { Matcher, WebstudioFragment } from "@webstudio-is/sdk";
import type { Matcher } from "@webstudio-is/sdk";
import {
findClosestNonTextualContainer,
findClosestInstanceMatchingFragment,
Expand Down Expand Up @@ -754,23 +758,6 @@ describe("is tree matching", () => {
});

describe("find closest instance matching fragment", () => {
const createFragment = (element: JSX.Element): WebstudioFragment => {
const { instances } = renderJsx(element);
const instancesArray = Array.from(instances.values());
return {
children: instancesArray[0].children,
instances: instancesArray,
styleSourceSelections: [],
styleSources: [],
breakpoints: [],
styles: [],
dataSources: [],
resources: [],
props: [],
assets: [],
};
};

test("finds closest list with list item fragment", () => {
const { instances } = renderJsx(
<$.Body ws:id="body">
Expand All @@ -779,12 +766,7 @@ describe("find closest instance matching fragment", () => {
</$.List>
</$.Body>
);
const fragment = createFragment(
// only children are tested
<>
<$.ListItem ws:id="new"></$.ListItem>
</>
);
const fragment = renderTemplate(<$.ListItem ws:id="new"></$.ListItem>);
expect(
findClosestInstanceMatchingFragment({
metas,
Expand Down Expand Up @@ -818,12 +800,7 @@ describe("find closest instance matching fragment", () => {
<$.Button ws:id="button"></$.Button>
</$.Body>
);
const fragment = createFragment(
// only children are tested
<>
<$.Button ws:id="new"></$.Button>
</>
);
const fragment = renderTemplate(<$.Button ws:id="new"></$.Button>);
expect(
findClosestInstanceMatchingFragment({
metas,
Expand All @@ -840,8 +817,7 @@ describe("find closest instance matching fragment", () => {
<$.Button ws:id="button"></$.Button>
</$.Body>
);
const fragment = createFragment(
// only children are tested
const fragment = renderTemplate(
<>
<$.Button ws:id="new-button"></$.Button>
<$.Text ws:id="new-text"></$.Text>
Expand All @@ -860,12 +836,7 @@ describe("find closest instance matching fragment", () => {
test("report first error", () => {
const onError = vi.fn();
const { instances } = renderJsx(<$.Body ws:id="body"></$.Body>);
const fragment = createFragment(
// only children are tested
<>
<$.ListItem ws:id="listitem"></$.ListItem>
</>
);
const fragment = renderTemplate(<$.ListItem ws:id="listitem"></$.ListItem>);
findClosestInstanceMatchingFragment({
metas,
instances,
Expand Down
3 changes: 2 additions & 1 deletion apps/builder/app/shared/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ export const findClosestInstanceMatchingFragment = ({
instances: Instances;
metas: Map<string, WsComponentMeta>;
instanceSelector: InstanceSelector;
fragment: WebstudioFragment;
// require only subset of fragment
fragment: Pick<WebstudioFragment, "children" | "instances">;
onError?: (message: string) => void;
}) => {
const mergedInstances = new Map(instances);
Expand Down
Loading
Loading