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
import { expect, test } from "vitest";
import {
renderData,
createTemplateComponentFixture,
expression,
ResourceValue,
} from "@webstudio-is/template";
import type { Page } from "./schema/pages";
import { createScope } from "./scope";
import { encodeDataSourceVariable } from "./expression";
import {
generateResourceRequestFields,
generateResources,
replaceFormActionsWithResources,
} from "./resources-generator";
import { generatePageResources } from "./managed-form-resources-generator";
import type { DataSource } from "./schema/data-sources";
const Body = createTemplateComponentFixture("Body");
const Form = createTemplateComponentFixture("ResourceForm");
const WebhookForm = createTemplateComponentFixture("WebhookForm");
test("base request fields do not generate Email delivery configuration", () => {
const fields = generateResourceRequestFields({
resource: {
id: "email",
name: "Email",
control: "email",
method: "post",
url: '""',
headers: [],
email: { recipientMode: "project" },
},
indent: "",
dataSources: new Map(),
usedDataSources: new Map(),
scope: createScope(),
});
expect(fields).toContain('control: "email"');
expect(fields).not.toContain("email: {");
});
test("does not fetch a Form-bound Resource during page load", () => {
const generated = generatePageResources({
scope: createScope(),
page: { rootInstanceId: "form" } as Page,
instances: toMap([
{ type: "instance", id: "form", component: "ResourceForm", children: [] },
]),
dataSources: toMap([
{
id: "formDataId",
type: "parameter",
scopeInstanceId: "form",
name: "formData",
},
{
id: "resourceVariableId",
type: "resource",
scopeInstanceId: "form",
name: "Submission",
resourceId: "resourceId",
},
]),
resources: toMap([
{
id: "resourceId",
name: "Submission",
method: "post",
url: '"https://example.com"',
headers: [],
body: encodeDataSourceVariable("formDataId"),
},
]),
props: toMap([
{
id: "action-prop",
instanceId: "form",
type: "json",
name: "action",
value: [{ dataSourceId: "resourceVariableId" }],
},
]),
});
expect(generated).not.toContain('id: "resourceId"');
expect(generated).not.toContain("formDataId");
expect(generated).not.toContain("formData");
});
test("does not evaluate configured Form Actions during page load", () => {
const generated = generatePageResources({
scope: createScope(),
page: { rootInstanceId: "form" } as Page,
instances: toMap([
{ type: "instance", id: "form", component: "ResourceForm", children: [] },
]),
dataSources: toMap([
{
id: "graphql-source",
type: "resource",
scopeInstanceId: "form",
name: "GraphQL action",
resourceId: "graphql-resource",
},
{
id: "disabled-source",
type: "resource",
scopeInstanceId: "form",
name: "Disabled action",
resourceId: "disabled-resource",
},
{
id: "action-dependency-source",
type: "resource",
scopeInstanceId: "form",
name: "Action dependency",
resourceId: "action-dependency-resource",
},
]),
resources: toMap([
{
id: "graphql-resource",
name: "GraphQL action",
control: "graphql",
method: "post",
url: `${encodeDataSourceVariable("action-dependency-source")}.url`,
headers: [],
body: "{ variables: { name: formData.name } }",
},
{
id: "disabled-resource",
name: "Disabled action",
method: "post",
url: '"https://example.com/disabled"',
headers: [],
},
{
id: "action-dependency-resource",
name: "Action dependency",
method: "get",
url: '"https://example.com/graphql"',
headers: [],
},
]),
props: toMap([
{
id: "action-prop",
instanceId: "form",
type: "json",
name: "action",
value: [
{ dataSourceId: "graphql-source" },
{ dataSourceId: "disabled-source" },
],
},
]),
});
expect(generated).not.toContain("graphql-resource");
expect(generated).not.toContain("disabled-resource");
expect(generated).not.toContain("action-dependency-resource");
expect(generated).not.toContain("formData.name");
});
test("keeps a page-used Resource selected as a Form Action", () => {
const generated = generatePageResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
instances: toMap([
{
type: "instance",
id: "body",
component: "Body",
children: [{ type: "id", value: "form" }],
},
{ type: "instance", id: "form", component: "ResourceForm", children: [] },
]),
dataSources: toMap([
{
id: "form-shared-source",
type: "resource",
scopeInstanceId: "form",
name: "Shared action",
resourceId: "shared-resource",
},
{
id: "page-resource-source",
type: "resource",
scopeInstanceId: "form",
name: "Page content",
resourceId: "page-resource",
},
]),
resources: toMap([
{
id: "shared-resource",
name: "Shared",
method: "get",
url: '"https://example.com/shared"',
headers: [],
},
{
id: "page-resource",
name: "Page content",
method: "get",
url: `${encodeDataSourceVariable("form-shared-source")}.url`,
headers: [],
},
]),
props: toMap([
{
id: "content-prop",
instanceId: "form",
type: "expression",
name: "data-content",
value: encodeDataSourceVariable("page-resource-source"),
},
{
id: "action-prop",
instanceId: "form",
type: "json",
name: "action",
value: [{ dataSourceId: "form-shared-source" }],
},
]),
});
expect(generated).toContain('id: "shared-resource"');
expect(generated).toContain('id: "page-resource"');
expect(generated).toContain('dependencies: ["shared-resource"]');
});
test("emits only the resolver imports required by bound Email Resources", () => {
const emailResource = {
id: "email-resource",
name: "Receipt",
control: "email" as const,
method: "post" as const,
url: '""',
headers: [],
email: {
recipientMode: "custom" as const,
recipientsExpression: encodeDataSourceVariable("recipients-value"),
senderExpression: encodeDataSourceVariable("sender-value"),
},
};
const dataSources = toMap([
{
id: "email-variable",
scopeInstanceId: "body",
type: "resource" as const,
name: "Receipt",
resourceId: "email-resource",
},
{
id: "recipients-value",
scopeInstanceId: "body",
type: "variable" as const,
name: "recipients",
value: { type: "string" as const, value: "[email protected]" },
},
{
id: "sender-value",
scopeInstanceId: "body",
type: "variable" as const,
name: "sender",
value: { type: "string" as const, value: "[email protected]" },
},
]);
const input = {
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources,
resources: toMap([emailResource]),
props: new Map(),
};
const generated = generatePageResources(input);
const genericGenerated = generateResources({
...input,
scope: createScope(),
});
expect(genericGenerated).not.toContain("resolveEmailRecipientsExpression(");
expect(genericGenerated).not.toContain(
"resolveEmailSenderSettingsExpression("
);
expect(genericGenerated).not.toContain("email: {");
expect(generated).toContain("email: {");
expect(generated).toContain(
'import { resolveEmailRecipientsExpression, resolveEmailSenderSettingsExpression } from "@webstudio-is/sdk";'
);
expect(generated).toContain("resolveEmailRecipientsExpression(");
expect(generated).toContain("resolveEmailSenderSettingsExpression(");
const senderOnlyGenerated = generatePageResources({
...input,
resources: toMap([
{
...emailResource,
email: {
recipientMode: "custom" as const,
recipients: "[email protected]",
senderExpression: encodeDataSourceVariable("sender-value"),
},
},
]),
});
expect(senderOnlyGenerated).toContain(
'import { resolveEmailSenderSettingsExpression } from "@webstudio-is/sdk";'
);
expect(senderOnlyGenerated).not.toContain("resolveEmailRecipientsExpression");
const literalEmailResource = {
...emailResource,
email: { recipientMode: "project" as const },
};
const literalGenerated = generatePageResources({
...input,
resources: toMap([literalEmailResource]),
});
expect(literalGenerated).not.toContain("resolveEmailRecipientsExpression");
expect(literalGenerated).not.toContain(
"resolveEmailSenderSettingsExpression"
);
});
test("excludes transitive Form-only Resources and rejects Dynamic Content Block selection", () => {
const input = {
page: { rootInstanceId: "form" } as Page,
instances: toMap([
{
type: "instance" as const,
id: "form",
component: "ResourceForm",
children: [],
},
]),
dataSources: toMap([
{
id: "formDataId",
type: "parameter" as const,
scopeInstanceId: "form",
name: "formData",
},
{
id: "directResourceVariableId",
type: "resource" as const,
scopeInstanceId: "form",
name: "Direct",
resourceId: "directResourceId",
},
{
id: "derivedResourceVariableId",
type: "resource" as const,
scopeInstanceId: "form",
name: "Derived",
resourceId: "derivedResourceId",
},
]),
resources: toMap([
{
id: "directResourceId",
name: "Direct",
method: "post" as const,
url: '"https://example.com/direct"',
headers: [],
body: encodeDataSourceVariable("formDataId"),
},
{
id: "derivedResourceId",
name: "Derived",
method: "post" as const,
url: '"https://example.com/derived"',
headers: [],
body: encodeDataSourceVariable("directResourceVariableId"),
},
]),
props: new Map(),
};
const generated = generatePageResources({ scope: createScope(), ...input });
expect(generated).not.toContain("directResourceId");
expect(generated).not.toContain("derivedResourceId");
expect(() =>
generatePageResources({
scope: createScope(),
...input,
contentBlockResourceSelections: [
{
sourceExpression: '"choice"',
candidates: [
{ assetId: "choice", resourceIds: ["derivedResourceId"] },
],
},
],
})
).toThrow(
"Dynamic Content Block Resources cannot depend on unavailable inputs"
);
});
const toMap = <T extends { id: string }>(list: T[]) =>
new Map(list.map((item) => [item.id, item] as const));
test("generate resources loader", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "variableResourceId",
scopeInstanceId: "body",
type: "resource",
name: "variableName",
resourceId: "resourceId",
},
]),
resources: toMap([
{
id: "resourceId",
name: "resourceName",
url: `"https://my-json.com"`,
method: "post",
headers: [{ name: "Content-Type", value: `"application/json"` }],
body: `{ body: true }`,
},
]),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const resourceName = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "resourceName",
url: "https://my-json.com",
searchParams: [
],
method: "post",
headers: [
{ name: "Content-Type", value: "application/json" },
],
body: { body: true },
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resourceId", outputName: "resourceName", dependencies: [], createRequest: resourceName, control: undefined, bodyFormat: undefined },
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("does not evaluate a Content Block source without candidate resources", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "collectionItem",
scopeInstanceId: "collection",
type: "parameter",
name: "collectionItem",
},
]),
resources: new Map(),
props: new Map(),
contentBlockResourceSelections: [
{
sourceExpression: `${encodeDataSourceVariable("collectionItem")}.id`,
candidates: [{ assetId: "article", resourceIds: [] }],
},
],
});
expect(generated).not.toContain("collectionItem");
});
test("selects only resources used by the resolved dynamic MDX candidate", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "selectedAsset",
scopeInstanceId: "body",
type: "variable",
name: "Selected Asset",
value: { type: "string", value: "article" },
},
{
id: "articleVariable",
scopeInstanceId: "article",
type: "resource",
name: "Article API",
resourceId: "articleResource",
},
{
id: "otherVariable",
scopeInstanceId: "other",
type: "resource",
name: "Other API",
resourceId: "otherResource",
},
]),
resources: toMap([
{
id: "articleResource",
name: "Article API",
url: '"https://example.com/article"',
method: "get",
headers: [],
},
{
id: "otherResource",
name: "Other API",
url: '"https://example.com/other"',
method: "get",
headers: [],
},
]),
props: new Map(),
contentBlockResourceSelections: [
{
sourceExpression: encodeDataSourceVariable("selectedAsset"),
candidates: [
{ assetId: "article", resourceIds: ["articleResource"] },
{ assetId: "other", resourceIds: ["otherResource"] },
],
},
],
});
expect(generated).toContain('if (SelectedAsset === "article")');
expect(generated).toContain('_contentData.set("ArticleAPI", ArticleAPI)');
expect(generated).toContain('if (SelectedAsset === "other")');
expect(generated).toContain('_contentData.set("OtherAPI", OtherAPI)');
expect(generated).not.toContain('id: "articleResource", outputName:');
expect(generated).not.toContain('id: "otherResource", outputName:');
});
test("loads a remote dynamic selection input as a graph root", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "selectedAsset",
scopeInstanceId: "body",
type: "resource",
name: "Selected Asset",
resourceId: "selectionResource",
},
{
id: "articleVariable",
scopeInstanceId: "article",
type: "resource",
name: "Article API",
resourceId: "articleResource",
},
]),
resources: toMap([
{
id: "selectionResource",
name: "Selected Asset",
url: '"https://example.com/selection"',
method: "get",
headers: [],
},
{
id: "articleResource",
name: "Article API",
url: '"https://example.com/article"',
method: "get",
headers: [],
},
]),
props: new Map(),
contentBlockResourceSelections: [
{
sourceExpression: `${encodeDataSourceVariable("selectedAsset")}.id`,
candidates: [{ assetId: "article", resourceIds: ["articleResource"] }],
},
],
});
expect(generated).toContain('rootIds: [\n "selectionResource"');
expect(generated).toContain('_props.resources?.["SelectedAsset"]');
});
test("generates a configured Assets request on the standard endpoint", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "postsVariable",
scopeInstanceId: "body",
type: "resource",
name: "Posts",
resourceId: "postsResource",
},
]),
resources: toMap([
{
id: "postsResource",
name: "Posts",
control: "system" as const,
url: '"/$resources/assets"',
method: "post" as const,
headers: [],
body: "{ query: { where: { all: [] }, limit: 20, offset: 0 } }",
},
]),
props: new Map(),
});
expect(generated).toContain('url: "/$resources/assets"');
expect(generated).not.toContain('resourceId: "postsResource"');
});
test("generate variable and use in resources loader", () => {
expect(
generateResources({
scope: createScope(),
page: {
rootInstanceId: "body",
} as Page,
dataSources: toMap([
{
id: "variableResourceId",
scopeInstanceId: "body",
name: "variableName",
type: "resource",
resourceId: "resourceId",
},
{
id: "variableTokenId",
scopeInstanceId: "body",
name: "Access Token",
type: "variable",
value: { type: "string", value: "my-token" },
},
]),
resources: toMap([
{
id: "resourceId",
name: "resourceName",
url: `"https://my-json.com/"`,
method: "post",
headers: [
{
name: "Authorization",
value: `"Token " + $ws$dataSource$variableTokenId`,
},
],
body: `{ body: true }`,
},
]),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
let AccessToken = "my-token"
const resourceName = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "resourceName",
url: "https://my-json.com/",
searchParams: [
],
method: "post",
headers: [
{ name: "Authorization", value: "Token " + AccessToken },
],
body: { body: true },
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resourceId", outputName: "resourceName", dependencies: [], createRequest: resourceName, control: undefined, bodyFormat: undefined },
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("generate dependency-gated resource request factories", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "firstDataSource",
scopeInstanceId: "body",
type: "resource",
name: "First",
resourceId: "firstResource",
},
{
id: "secondDataSource",
scopeInstanceId: "body",
type: "resource",
name: "Second",
resourceId: "secondResource",
},
]),
resources: toMap([
{
id: "firstResource",
name: "First",
method: "get",
url: '"https://example.com/first"',
headers: [],
},
{
id: "secondResource",
name: "Second",
method: "get",
url: '"https://example.com/second/" + $ws$dataSource$firstDataSource.data.id',
headers: [],
},
]),
props: toMap([
{
id: "prop",
instanceId: "body",
name: "data-value",
type: "expression",
value: "$ws$dataSource$secondDataSource",
},
]),
});
expect(generated).toContain('const First_1 = documents.get("firstResource")');
expect(generated).toContain("const _data: ResourceRequestGraph");
expect(generated).toContain(
'dependencies: ["firstResource"], createRequest: Second'
);
});
test("generates action resources as on-demand graph roots with dependencies", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "authorDataSource",
scopeInstanceId: "body",
type: "resource",
name: "Author",
resourceId: "authorResource",
},
]),
resources: toMap([
{
id: "authorResource",
name: "Author",
method: "get",
url: '"https://example.com/author"',
headers: [],
},
{
id: "submitResource",
name: "Submit",
method: "post",
url: '"https://example.com/submit/" + $ws$dataSource$authorDataSource.data.id',
headers: [],
},
]),
props: toMap([
{
id: "submitAction",
instanceId: "body",
name: "Submit",
type: "resource",
value: "submitResource",
},
]),
});
expect(generated).toContain(
'const Author_1 = documents.get("authorResource")'
);
expect(generated).toContain(
'dependencies: ["authorResource"], createRequest: Submit'
);
expect(generated).toContain(
'["Submit", { id: "submitResource", outputName: "Submit" }]'
);
expect(generated).toContain("rootIds: [\n ]");
});
test("generates lazy roots only for resource expressions in the page tree", () => {
const generated = generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
instances: toMap([
{ id: "body", type: "instance", component: "Body", children: [] },
{ id: "other", type: "instance", component: "Body", children: [] },
]),
dataSources: toMap([
{
id: "usedDataSource",
scopeInstanceId: "body",
type: "resource",
name: "Used",
resourceId: "usedResource",
},
{
id: "unusedDataSource",
scopeInstanceId: "other",
type: "resource",
name: "Unused",
resourceId: "unusedResource",
},
]),
resources: toMap([
{
id: "usedResource",
name: "Used",
method: "get",
url: '"https://example.com/used"',
headers: [],
},
{
id: "unusedResource",
name: "Unused",
method: "get",
url: '"https://example.com/unused"',
headers: [],
},
]),
props: toMap([
{
id: "usedProp",
instanceId: "body",
name: "data-used",
type: "expression",
value: "$ws$dataSource$usedDataSource",
},
{
id: "unusedProp",
instanceId: "other",
name: "data-unused",
type: "expression",
value: "$ws$dataSource$unusedDataSource",
},
{
id: "otherPageAction",
instanceId: "other",
name: "action",
type: "resource",
value: "usedResource",
},
]),
});
expect(generated).toContain("const _data: ResourceRequestGraph");
expect(generated).toContain('rootIds: [\n "usedResource",\n ]');
expect(generated).not.toContain('["Used", Used]');
});
test("generate page system variable and use in resources loader", () => {
expect(
generateResources({
scope: createScope(),
page: {
rootInstanceId: "body",
systemDataSourceId: "variableSystemId",
} as Page,
dataSources: toMap([
{
id: "variableResourceId",
scopeInstanceId: "body",
name: "variableName",
type: "resource",
resourceId: "resourceId",
},
{
id: "variableSystemId",
scopeInstanceId: "body",
name: "system",
type: "parameter",
},
]),
resources: toMap([
{
id: "resourceId",
name: "resourceName",
url: `"https://my-json.com/" + $ws$dataSource$variableSystemId.params.slug`,
searchParams: [],
method: "post",
headers: [{ name: "Content-Type", value: `"application/json"` }],
body: `{ body: true }`,
},
]),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const system = _props.system
const resourceName = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "resourceName",
url: "https://my-json.com/" + system?.params?.slug,
searchParams: [
],
method: "post",
headers: [
{ name: "Content-Type", value: "application/json" },
],
body: { body: true },
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resourceId", outputName: "resourceName", dependencies: [], createRequest: resourceName, control: undefined, bodyFormat: undefined },
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("generate global system variable and use in resources loader", () => {
const myResource = new ResourceValue("My Resource", {
control: "system",
url: expression`"https://my-json.com/" + $ws$system.params.slug`,
method: "post",
headers: [{ name: "Content-Type", value: expression`"application/json"` }],
body: expression`{ body: true }`,
});
expect(
generateResources({
scope: createScope(),
page: {
rootInstanceId: "bodyId",
} as Page,
...renderData(
<Body ws:id="bodyId" vars={expression`${myResource}`}></Body>
),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const system = _props.system
const MyResource = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "My Resource",
control: "system",
url: "https://my-json.com/" + system?.params?.slug,
searchParams: [
],
method: "post",
headers: [
{ name: "Content-Type", value: "application/json" },
],
body: { body: true },
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resource:0", outputName: "MyResource", dependencies: [], createRequest: MyResource, control: "system", bodyFormat: undefined },
],
rootIds: [
"resource:0",
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("generate empty resources loader", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: new Map(),
resources: new Map(),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const _data: ResourceRequestGraph = {
resources: [
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("generate resource loader with search params", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap<DataSource>([
{
id: "variableTermId",
scopeInstanceId: "body",
type: "variable",
name: "term",
value: { type: "string", value: "my-term" },
},
{
id: "variableResourceId",
scopeInstanceId: "body",
type: "resource",
name: "variableName",
resourceId: "resourceId",
},
]),
resources: toMap([
{
id: "resourceId",
name: "resourceName",
method: "get",
url: `"https://my-json.com"`,
searchParams: [
{
name: "search",
value: `$ws$dataSource$variableTermId`,
},
],
headers: [],
},
]),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
let term = "my-term"
const resourceName = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "resourceName",
url: "https://my-json.com",
searchParams: [
{ name: "search", value: term },
],
method: "get",
headers: [
],
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resourceId", outputName: "resourceName", dependencies: [], createRequest: resourceName, control: undefined, bodyFormat: undefined },
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("prevent generating unused variables", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "unuseVariableId",
scopeInstanceId: "body",
name: "Unused Variable",
type: "variable",
value: { type: "string", value: "" },
},
]),
resources: new Map(),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const _data: ResourceRequestGraph = {
resources: [
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("prevent generating unused system variable", () => {
expect(
generateResources({
scope: createScope(),
page: {
rootInstanceId: "body",
systemDataSourceId: "variableParamsId",
} as Page,
dataSources: toMap([
{
id: "variableParamsId",
scopeInstanceId: "body",
name: "Unused System",
type: "parameter",
},
]),
resources: new Map(),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const _data: ResourceRequestGraph = {
resources: [
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("generates action resources as lazy roots without loading stale data", () => {
expect(
generateResources({
scope: createScope(),
page: {
rootInstanceId: "body",
systemDataSourceId: "variableParamsId",
} as Page,
dataSources: toMap([
{
id: "resourceDataSourceId",
scopeInstanceId: "body",
type: "resource",
name: "resourceDataSource",
resourceId: "resourceId",
},
]),
resources: toMap([
{
id: "resourceId",
name: "resourceName",
url: `"https://my-url.com"`,
method: "post",
headers: [],
},
]),
props: toMap([
{
id: "propId",
instanceId: "body",
name: "myProp",
type: "resource",
value: "resourceId",
},
]),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const resourceName = (documents: ReadonlyMap<string, unknown>): ResourceRequest => {
return {
name: "resourceName",
url: "https://my-url.com",
searchParams: [
],
method: "post",
headers: [
],
}
}
const _data: ResourceRequestGraph = {
resources: [
{ id: "resourceId", outputName: "resourceName", dependencies: [], createRequest: resourceName, control: undefined, bodyFormat: undefined },
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
["resourceName", { id: "resourceId", outputName: "resourceName" }],
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("skip missing resource referenced by data source", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: toMap([
{
id: "variableResourceId",
scopeInstanceId: "body",
type: "resource",
name: "missingResource",
resourceId: "missingResourceId",
},
]),
resources: new Map(),
props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const _data: ResourceRequestGraph = {
resources: [
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("skip missing resource referenced by action prop", () => {
expect(
generateResources({
scope: createScope(),
page: { rootInstanceId: "body" } as Page,
dataSources: new Map(),
resources: new Map(),
props: toMap([
{
id: "propId",
instanceId: "body",
name: "myProp",
type: "resource",
value: "missingResourceId",
},
]),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
import type { ResourceRequestGraph } from "@webstudio-is/sdk/runtime";
export const getResources = (_props: { system: System; resources?: Record<string, any> }) => {
const _data: ResourceRequestGraph = {
resources: [
],
rootIds: [
],
}
const _contentData = new Map<string, ResourceRequest>()
const _action = new Map<string, { id: string; outputName: string }>([
])
return { data: _data, action: _action, contentData: _contentData }
}
"
`);
});
test("replace form action with resource", () => {
const data = renderData(
<WebhookForm ws:id="formId" action="https://my-url.com"></WebhookForm>
);
replaceFormActionsWithResources(data);
expect(data.props).toEqual(
toMap([
{
id: "formId:action",
instanceId: "formId",
name: "action",
type: "resource",
value: "formId",
},
])
);
expect(data.resources).toEqual(
toMap([
{
headers: [{ name: "Content-Type", value: `"application/json"` }],
id: "formId",
method: "post",
name: "action",
url: `"https://my-url.com"`,
},
])
);
});
test("ignore empty form action", () => {
const data = renderData(<WebhookForm ws:id="formId" action=""></WebhookForm>);
replaceFormActionsWithResources(data);
expect(data.props).toEqual(
toMap([
{
id: "formId:action",
instanceId: "formId",
name: "action",
type: "string",
value: "",
},
])
);
expect(data.resources).toEqual(new Map());
});
test("preserve navigation attributes on the canonical Form", () => {
const data = renderData(
<Form ws:id="formId" action="?__webstudio__=navigate" method="get" />
);
const originalProps = new Map(data.props);
replaceFormActionsWithResources(data);
expect(data.props).toEqual(originalProps);
expect(data.resources).toEqual(new Map());
});