Skip to content

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.

Add tests

Ivan Starkova50199a19d21
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
index 7b41c59c6f..8f9875699e 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.test.ts
@@ -1,6 +1,10 @@
import { jest } from "@jest/globals";
import { renderHook, act } from "@testing-library/react-hooks";
-import type { ComponentName, UserProp } from "@webstudio-is/react-sdk";
+import {
+ ComponentName,
+ getComponentMeta,
+ UserProp,
+} from "@webstudio-is/react-sdk";
import { nanoid } from "nanoid";
import type { SelectedInstanceData } from "@webstudio-is/project";
import { usePropsLogic } from "./use-props-logic";
@@ -453,4 +457,113 @@ describe("usePropsLogic", () => {
]
`);
});
+
+ test("should respect initialProps ordering", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData: getSelectedInstanceData("Image", [
+ {
+ id: "22",
+ prop: "aria-label",
+ type: "string",
+ value: "https://example.com",
+ required: false,
+ },
+ {
+ id: "0",
+ prop: "alt",
+ type: "string",
+ value: "alt text",
+ required: true,
+ },
+ {
+ id: "1",
+ prop: "width",
+ type: "number",
+ value: 101,
+ required: false,
+ },
+ {
+ id: "33",
+ prop: "title",
+ type: "string",
+ value: "title text",
+ required: true,
+ },
+ {
+ id: "2",
+ prop: "src",
+ type: "string",
+ value: "https://example.com",
+ required: true,
+ },
+ ]),
+ publish: jest.fn(),
+ })
+ );
+
+ const imgMeta = getComponentMeta("Image");
+
+ const propNames = result.current.userProps.map((userProp) => userProp.prop);
+
+ expect(imgMeta.initialProps).toEqual(
+ propNames.slice(0, imgMeta.initialProps?.length ?? 0)
+ );
+
+ expect(propNames.slice(imgMeta.initialProps?.length ?? 0)).toEqual(
+ result.current.userProps
+ .filter(
+ (userProp) => imgMeta.initialProps?.includes(userProp.prop) === false
+ )
+ .map((userProp) => userProp.prop)
+ );
+ });
+
+ test("should return isRequired true for initial props", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData: getSelectedInstanceData("Image", []),
+ publish: jest.fn(),
+ })
+ );
+
+ expect(
+ result.current.isRequired({
+ id: "2",
+ prop: "src",
+ type: "string",
+ value: "https://example.com",
+ required: false,
+ })
+ ).toBe(true);
+ });
+
+ test("should return isRequired true for required props", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData: getSelectedInstanceData("Image", []),
+ publish: jest.fn(),
+ })
+ );
+
+ expect(
+ result.current.isRequired({
+ id: "2",
+ prop: "arial-label",
+ type: "string",
+ value: "https://example.com",
+ required: true,
+ })
+ ).toBe(true);
+
+ expect(
+ result.current.isRequired({
+ id: "2",
+ prop: "arial-label",
+ type: "string",
+ value: "https://example.com",
+ required: false,
+ })
+ ).toBe(false);
+ });
});
diff --git a/apps/designer/app/designer/features/props-panel/use-props-logic.ts b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
index d2a23a3ad8..d0daf33095 100644
--- a/apps/designer/app/designer/features/props-panel/use-props-logic.ts
+++ b/apps/designer/app/designer/features/props-panel/use-props-logic.ts
@@ -300,6 +300,10 @@ export const usePropsLogic = ({
};
const isRequired = (prop: UserProp) => {
+ if (prop.required) {
+ return true;
+ }
+
return requiredProps.some(
(requiredProp) => requiredProp.prop === prop.prop
);