Skip to content

Commit eb2a275

Browse files
authored
refactor: support resources in jsx templates (#4800)
Added full resource configuration in templates. Will be useful for testing.
1 parent 2fab4ba commit eb2a275

File tree

5 files changed

+171
-95
lines changed

5 files changed

+171
-95
lines changed

apps/builder/app/shared/instance-utils.test.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createDefaultPages } from "@webstudio-is/project-build";
55
import {
66
$,
77
ws,
8-
ExpressionValue,
8+
expression,
99
renderTemplate,
1010
renderData,
1111
} from "@webstudio-is/template";
@@ -2793,9 +2793,7 @@ describe("find closest insertable", () => {
27932793
const { instances } = renderData(
27942794
<$.Body ws:id="bodyId">
27952795
<$.Box ws:id="box1Id"></$.Box>
2796-
<$.Paragraph ws:id="paragraphId">
2797-
{new ExpressionValue(`"bla"`)}
2798-
</$.Paragraph>
2796+
<$.Paragraph ws:id="paragraphId">{expression`"bla"`}</$.Paragraph>
27992797
<$.Box ws:id="box2Id"></$.Box>
28002798
</$.Body>
28012799
);

apps/builder/app/shared/matcher.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, test, vi } from "vitest";
22
import {
33
$,
4-
ExpressionValue,
4+
expression,
55
renderTemplate,
66
renderData,
77
} from "@webstudio-is/template";
@@ -947,9 +947,7 @@ describe("find closest container", () => {
947947
...renderData(
948948
<$.Body ws:id="body">
949949
<$.Box ws:id="box">
950-
<$.Box ws:id="box-with-expr">
951-
{new ExpressionValue("1 + 1")}
952-
</$.Box>
950+
<$.Box ws:id="box-with-expr">{expression`1 + 1`}</$.Box>
953951
</$.Box>
954952
</$.Body>
955953
),
@@ -1009,9 +1007,7 @@ describe("find closest non textual container", () => {
10091007
...renderData(
10101008
<$.Body ws:id="body">
10111009
<$.Box ws:id="box">
1012-
<$.Box ws:id="box-with-expr">
1013-
{new ExpressionValue("1 + 1")}
1014-
</$.Box>
1010+
<$.Box ws:id="box-with-expr">{expression`1 + 1`}</$.Box>
10151011
</$.Box>
10161012
</$.Body>
10171013
),

packages/react-sdk/src/component-generator.test.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,16 @@ return (condition) &&
892892
});
893893

894894
test("generate resource prop", () => {
895+
const myResource = new ResourceValue("myResource", {
896+
url: expression`"https://my-url.com?with-secret"`,
897+
method: "get",
898+
headers: [],
899+
});
900+
const anotherResource = new ResourceValue("anotherResource", {
901+
url: expression`"https://another-url.com?with-secret"`,
902+
method: "get",
903+
headers: [],
904+
});
895905
expect(
896906
generateWebstudioComponent({
897907
classesMap: new Map(),
@@ -902,14 +912,8 @@ test("generate resource prop", () => {
902912
indexesWithinAncestors: new Map(),
903913
...renderData(
904914
<$.Body ws:id="body">
905-
<$.Form
906-
ws:id="form1"
907-
action={new ResourceValue("https://my-url.com?with-secret")}
908-
></$.Form>
909-
<$.Form
910-
ws:id="form2"
911-
action={new ResourceValue("https://another-url.com?with-secret")}
912-
></$.Form>
915+
<$.Form ws:id="form1" action={myResource}></$.Form>
916+
<$.Form ws:id="form2" action={anotherResource}></$.Form>
913917
</$.Body>
914918
),
915919
})

packages/template/src/jsx.test.tsx

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {
55
ActionValue,
66
AssetValue,
77
expression,
8-
ExpressionValue,
98
PageValue,
109
Parameter,
11-
ParameterValue,
1210
PlaceholderValue,
1311
renderTemplate,
12+
ResourceValue,
1413
Variable,
1514
} from "./jsx";
1615
import { css } from "./css";
@@ -154,10 +153,7 @@ test("render literal props", () => {
154153

155154
test("render defined props", () => {
156155
const { props } = renderTemplate(
157-
<$.Body
158-
data-expression={new ExpressionValue("1 + 1")}
159-
data-parameter={new ParameterValue("parameterId")}
160-
>
156+
<$.Body>
161157
<$.Box
162158
data-asset={new AssetValue("assetId")}
163159
data-page={new PageValue("pageId")}
@@ -166,20 +162,6 @@ test("render defined props", () => {
166162
</$.Body>
167163
);
168164
expect(props).toEqual([
169-
{
170-
id: "0:data-expression",
171-
instanceId: "0",
172-
name: "data-expression",
173-
type: "expression",
174-
value: "1 + 1",
175-
},
176-
{
177-
id: "0:data-parameter",
178-
instanceId: "0",
179-
name: "data-parameter",
180-
type: "parameter",
181-
value: "parameterId",
182-
},
183165
{
184166
id: "1:data-asset",
185167
instanceId: "1",
@@ -493,6 +475,86 @@ test("render parameter bound to prop expression", () => {
493475
]);
494476
});
495477

478+
test("render resource variable", () => {
479+
const value = new Variable("value", "value");
480+
const myResource = new ResourceValue("myResource", {
481+
url: expression`"https://my-url.com/" + ${value}`,
482+
method: "get",
483+
headers: [{ name: "auth", value: expression`${value}` }],
484+
body: expression`${value}`,
485+
});
486+
const { dataSources, resources } = renderTemplate(
487+
<$.Body ws:id="body">{expression`${myResource}.title`}</$.Body>
488+
);
489+
expect(dataSources).toEqual([
490+
{
491+
id: "1",
492+
name: "value",
493+
scopeInstanceId: "body",
494+
type: "variable",
495+
value: { type: "string", value: "value" },
496+
},
497+
{
498+
id: "0",
499+
scopeInstanceId: "body",
500+
name: "myResource",
501+
type: "resource",
502+
resourceId: "resource:0",
503+
},
504+
]);
505+
expect(resources).toEqual([
506+
{
507+
id: "resource:0",
508+
name: "myResource",
509+
url: `"https://my-url.com/" + $ws$dataSource$1`,
510+
method: "get",
511+
headers: [{ name: "auth", value: `$ws$dataSource$1` }],
512+
body: `$ws$dataSource$1`,
513+
},
514+
]);
515+
});
516+
517+
test("render resource prop", () => {
518+
const value = new Variable("value", "value");
519+
const myResource = new ResourceValue("myResource", {
520+
url: expression`"https://my-url.com/" + ${value}`,
521+
method: "get",
522+
headers: [{ name: "auth", value: expression`${value}` }],
523+
body: expression`${value}`,
524+
});
525+
const { props, dataSources, resources } = renderTemplate(
526+
<$.Body ws:id="body" action={myResource}></$.Body>
527+
);
528+
expect(props).toEqual([
529+
{
530+
id: "body:action",
531+
instanceId: "body",
532+
name: "action",
533+
type: "resource",
534+
value: "resource:0",
535+
},
536+
]);
537+
expect(dataSources).toEqual([
538+
{
539+
id: "1",
540+
name: "value",
541+
scopeInstanceId: "body",
542+
type: "variable",
543+
value: { type: "string", value: "value" },
544+
},
545+
]);
546+
expect(resources).toEqual([
547+
{
548+
id: "resource:0",
549+
name: "myResource",
550+
url: `"https://my-url.com/" + $ws$dataSource$1`,
551+
method: "get",
552+
headers: [{ name: "auth", value: `$ws$dataSource$1` }],
553+
body: `$ws$dataSource$1`,
554+
},
555+
]);
556+
});
557+
496558
test("render ws:show attribute", () => {
497559
const { props } = renderTemplate(
498560
<$.Body ws:id="body" ws:show={true}></$.Body>

0 commit comments

Comments
 (0)