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 type { StyleProperty } from "@webstudio-is/css-engine";
import type { Pages, WebstudioData } from "@webstudio-is/sdk";
import {
migrateWebstudioDataMutable,
serializePages,
migratePages,
} from "./index";
const emptyData: WebstudioData = {
pages: {
homePageId: "homePageId",
rootFolderId: "root",
pages: new Map([
[
"homePageId",
{
id: "homePageId",
name: "Home",
path: "",
title: `"Home"`,
meta: {},
rootInstanceId: "rootInstanceId",
},
],
]),
folders: new Map([
[
"root",
{
id: "root",
name: "Root",
slug: "",
children: ["homePageId"],
},
],
]),
},
assets: new Map(),
dataSources: new Map(),
resources: new Map(),
instances: new Map(),
props: new Map(),
breakpoints: new Map(),
styleSources: new Map(),
styleSourceSelections: new Map(),
styles: new Map(),
};
test("migrates legacy pages inside webstudio data", () => {
const data = structuredClone(emptyData);
data.pages = {
homePage: {
id: "homePageId",
name: "Home",
path: "",
title: `"Home"`,
meta: {},
rootInstanceId: "rootInstanceId",
},
pages: [
{
id: "pageId",
name: "Page",
path: "/page",
title: `"Page"`,
meta: {},
rootInstanceId: "pageRootInstanceId",
},
],
folders: [
{
id: "root",
name: "Root",
slug: "",
children: [],
},
],
} as unknown as Pages;
migrateWebstudioDataMutable(data);
expect(data.pages).toEqual({
meta: undefined,
compiler: undefined,
redirects: undefined,
homePageId: "homePageId",
rootFolderId: "root",
pages: new Map([
["homePageId", expect.objectContaining({ id: "homePageId" })],
["pageId", expect.objectContaining({ id: "pageId" })],
]),
pageTemplates: new Map(),
folders: new Map([
[
"root",
{
id: "root",
name: "Root",
slug: "",
children: ["homePageId", "pageId"],
},
],
]),
});
});
test("migrates unmarked historical and mixed form identities exactly once", () => {
const data = structuredClone(emptyData);
data.instances.set("native", {
type: "instance",
id: "native",
component: "NativeForm",
children: [],
});
data.instances.set("managed", {
type: "instance",
id: "managed",
component: "ResourceForm",
children: [],
});
data.instances.set("legacy", {
type: "instance",
id: "legacy",
component: "Form",
children: [],
});
data.instances.set("webhook", {
type: "instance",
id: "webhook",
component: "WebhookForm",
children: [],
});
migrateWebstudioDataMutable(data);
expect(data.instances.get("native")?.component).toBe("ResourceForm");
expect(data.instances.get("managed")?.component).toBe("ResourceForm");
expect(data.instances.get("legacy")?.component).toBe("WebhookForm");
expect(data.instances.get("webhook")?.component).toBe("WebhookForm");
expect("formComponentIdentityVersion" in data.pages).toBe(false);
data.instances.set("new", {
type: "instance",
id: "new",
component: "ResourceForm",
children: [],
});
migrateWebstudioDataMutable(data);
expect(data.instances.get("native")?.component).toBe("ResourceForm");
expect(data.instances.get("managed")?.component).toBe("ResourceForm");
expect(data.instances.get("legacy")?.component).toBe("WebhookForm");
expect(data.instances.get("webhook")?.component).toBe("WebhookForm");
expect(data.instances.get("new")?.component).toBe("ResourceForm");
const reloaded = structuredClone(data);
reloaded.pages = migratePages(serializePages(data.pages));
expect("formComponentIdentityVersion" in reloaded.pages).toBe(false);
migrateWebstudioDataMutable(reloaded);
expect(reloaded.instances.get("new")?.component).toBe("ResourceForm");
});
test("migrates marked modern Form and preserves all instance data", () => {
const data = structuredClone(emptyData);
(
data.pages as Pages & { formComponentIdentityVersion: number }
).formComponentIdentityVersion = 1;
data.instances.set("modern", {
type: "instance",
id: "modern",
component: "Form",
children: [{ type: "text", value: "keep" }],
});
migrateWebstudioDataMutable(data);
expect(data.instances.get("modern")).toEqual({
type: "instance",
id: "modern",
component: "ResourceForm",
children: [{ type: "text", value: "keep" }],
});
expect("formComponentIdentityVersion" in data.pages).toBe(false);
migrateWebstudioDataMutable(data);
expect(data.instances.get("modern")?.component).toBe("ResourceForm");
});
test("migrates styles inside webstudio data", () => {
const data = structuredClone(emptyData);
data.styles.set("base:local:overflow::hover", {
breakpointId: "base",
styleSourceId: "local",
state: ":hover",
property: "overflow" as StyleProperty,
value: {
type: "tuple",
value: [
{ type: "keyword", value: "auto" },
{ type: "keyword", value: "hidden" },
],
},
});
migrateWebstudioDataMutable(data);
expect(Array.from(data.styles.values())).toEqual([
{
breakpointId: "base",
property: "overflowX",
state: ":hover",
styleSourceId: "local",
value: { type: "keyword", value: "auto" },
},
{
breakpointId: "base",
property: "overflowY",
state: ":hover",
styleSourceId: "local",
value: { type: "keyword", value: "hidden" },
},
]);
});
test("moves legacy Code Text properties into text content", () => {
const data = structuredClone(emptyData);
data.instances.set("literal", {
type: "instance",
id: "literal",
component: "CodeText",
children: [{ type: "text", value: "ignored legacy child" }],
});
data.instances.set("bound", {
type: "instance",
id: "bound",
component: "CodeText",
children: [],
});
data.props.set("literal-code", {
id: "literal-code",
instanceId: "literal",
name: "code",
type: "string",
value: "const answer = 42;",
});
data.props.set("bound-code", {
id: "bound-code",
instanceId: "bound",
name: "code",
type: "expression",
value: "$ws$dataSource$code",
});
migrateWebstudioDataMutable(data);
migrateWebstudioDataMutable(data);
expect(data.instances.get("literal")?.children).toEqual([
{ type: "text", value: "const answer = 42;" },
]);
expect(data.instances.get("bound")?.children).toEqual([
{ type: "expression", value: "$ws$dataSource$code" },
]);
expect(Array.from(data.props.values())).toEqual([]);
});
test("removes redundant Code Text defaults and preserves custom selections", () => {
const data = structuredClone(emptyData);
data.instances.set("defaults", {
type: "instance",
id: "defaults",
component: "CodeText",
children: [],
});
data.instances.set("custom", {
type: "instance",
id: "custom",
component: "CodeText",
children: [],
});
data.props.set("default-language", {
id: "default-language",
instanceId: "defaults",
name: "language",
type: "string",
value: "javascript",
});
data.props.set("default-theme", {
id: "default-theme",
instanceId: "defaults",
name: "theme",
type: "string",
value: "github-light",
});
data.props.set("custom-language", {
id: "custom-language",
instanceId: "custom",
name: "language",
type: "string",
value: "css",
});
data.props.set("custom-theme", {
id: "custom-theme",
instanceId: "custom",
name: "theme",
type: "string",
value: "nord",
});
migrateWebstudioDataMutable(data);
migrateWebstudioDataMutable(data);
expect(Array.from(data.props.values())).toEqual([
expect.objectContaining({ name: "language", value: "css" }),
expect.objectContaining({ name: "theme", value: "nord" }),
]);
});
test("removes the legacy local styles from Content Block Image templates", () => {
const data = structuredClone(emptyData);
data.instances.set("templates", {
type: "instance",
id: "templates",
component: "ws:block-template",
children: [{ type: "id", value: "image" }],
});
data.instances.set("image", {
type: "instance",
id: "image",
component: "Image",
children: [],
});
data.styleSources.set("image-local", {
type: "local",
id: "image-local",
});
data.styleSources.set("custom-local", {
type: "local",
id: "custom-local",
});
data.styleSourceSelections.set("image", {
instanceId: "image",
values: ["image-local", "custom-local"],
});
for (const [property, value] of [
["marginRight", { type: "keyword", value: "auto" }],
["marginLeft", { type: "keyword", value: "auto" }],
["width", { type: "unit", unit: "%", value: 100 }],
["height", { type: "keyword", value: "auto" }],
] as const) {
data.styles.set(`image-local:base:${property}:`, {
breakpointId: "base",
styleSourceId: "image-local",
property,
value,
});
}
data.styles.set("custom-local:base:color:", {
breakpointId: "base",
styleSourceId: "custom-local",
property: "color",
value: { type: "keyword", value: "red" },
});
migrateWebstudioDataMutable(data);
migrateWebstudioDataMutable(data);
expect(data.styleSourceSelections.get("image")?.values).toEqual([
"custom-local",
]);
expect(data.styleSources.has("image-local")).toBe(false);
expect(data.styleSources.has("custom-local")).toBe(true);
expect(Array.from(data.styles.values())).toEqual([
expect.objectContaining({ styleSourceId: "custom-local" }),
]);
});
test("marks legacy direct Content Block document bindings as readwrite", () => {
const data = structuredClone(emptyData);
data.instances.set("block", {
type: "instance",
id: "block",
component: "ws:block",
children: [{ type: "id", value: "title" }],
});
data.instances.set("title", {
type: "instance",
id: "title",
component: "Heading",
children: [
{
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
},
],
});
data.props.set("source", {
id: "source",
instanceId: "block",
name: "src",
type: "asset",
value: "article",
});
data.props.set("document", {
id: "document",
instanceId: "block",
name: "document",
type: "parameter",
value: "document",
});
data.props.set("label", {
id: "label",
instanceId: "title",
name: "aria-label",
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
});
data.props.set("read-only-label", {
id: "read-only-label",
instanceId: "title",
name: "title",
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
mode: "read",
});
migrateWebstudioDataMutable(data);
migrateWebstudioDataMutable(data);
expect(data.instances.get("title")?.children).toEqual([
{
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
mode: "readwrite",
},
]);
expect(data.props.get("label")).toMatchObject({ mode: "readwrite" });
expect(data.props.get("read-only-label")).toMatchObject({ mode: "read" });
});
test("marks legacy document bindings before a Content Block is connected", () => {
const data = structuredClone(emptyData);
data.instances.set("block", {
type: "instance",
id: "block",
component: "ws:block",
children: [{ type: "id", value: "title" }],
});
data.instances.set("title", {
type: "instance",
id: "title",
component: "Heading",
children: [
{
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
},
],
});
data.props.set("document", {
id: "document",
instanceId: "block",
name: "document",
type: "parameter",
value: "document",
});
migrateWebstudioDataMutable(data);
expect(data.instances.get("title")?.children).toEqual([
{
type: "expression",
value: "$ws$dataSource$document.frontmatter.title",
mode: "readwrite",
},
]);
});