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.

test: Add a test for use-props-logic

Andrey Okonetchnikov2ad532653475
diff --git a/app/designer/features/props-panel/use-props-logic.test.ts b/app/designer/features/props-panel/use-props-logic.test.ts
new file mode 100644
index 0000000000..d1317e2b85
--- /dev/null
+++ b/app/designer/features/props-panel/use-props-logic.test.ts
@@ -0,0 +1,100 @@
+import { renderHook, act } from "@testing-library/react-hooks";
+import { usePropsLogic } from "./use-props-logic";
+
+const selectedInstanceData = {
+ id: "2",
+ component: "Button",
+ cssRules: [],
+ browserStyle: {},
+ props: {
+ id: "1",
+ props: [
+ {
+ id: "disabled",
+ prop: "disabled",
+ value: true,
+ },
+ ],
+ },
+};
+
+describe("usePropsLogic", () => {
+ test("should return initial props for a given instance", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData,
+ publish: jest.fn(),
+ })
+ );
+ expect(result.current.userProps).toMatchInlineSnapshot(`
+ Array [
+ Object {
+ "id": "disabled",
+ "prop": "disabled",
+ "value": true,
+ },
+ ]
+ `);
+ });
+
+ test("should add an empty prop", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData,
+ publish: jest.fn(),
+ })
+ );
+
+ expect(result.current.userProps.length).toEqual(1);
+
+ act(() => {
+ result.current.addEmptyProp();
+ });
+
+ expect(result.current.userProps.length).toEqual(2);
+ expect(result.current.userProps[1]).toMatchObject({ prop: "", value: "" });
+ });
+
+ test("should remove a prop", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData,
+ publish: jest.fn(),
+ })
+ );
+
+ act(() => {
+ result.current.handleDeleteProp("disabled");
+ });
+
+ expect(result.current.userProps.length).toEqual(0);
+ expect(result.current.userProps).toMatchInlineSnapshot(`Array []`);
+ });
+
+ test("should add update a prop", () => {
+ const { result } = renderHook(() =>
+ usePropsLogic({
+ selectedInstanceData,
+ publish: jest.fn(),
+ })
+ );
+
+ act(() => {
+ result.current.handleChangeProp("disabled", "prop", "disabled2");
+ });
+
+ act(() => {
+ result.current.handleChangeProp("disabled", "value", false);
+ });
+
+ expect(result.current.userProps).toMatchInlineSnapshot(`
+ Array [
+ Object {
+ "id": "disabled",
+ "prop": "disabled2",
+ "value": false,
+ },
+ ]
+ `);
+ });
+});
diff --git a/jest.config.js b/jest.config.js
index 44f9c175bd..4bcfacedcd 100644
--- a/jest.config.js
+++ b/jest.config.js
@@ -2,7 +2,17 @@ module.exports = {
testEnvironment: "node",
testMatch: ["<rootDir>/app/**/*.test.ts"],
transform: {
- "^.+\\.tsx?$": "esbuild-jest",
+ "^.+\\.tsx?$": [
+ "esbuild-jest",
+ {
+ // This is needed for inline snapshots to work
+ // See: https://github.com/aelbore/esbuild-jest#setting-up-jest-config-file-with-transformoptions
+ sourcemap: true,
+ loaders: {
+ ".test.ts": "tsx",
+ },
+ },
+ ],
},
moduleNameMapper: {
"^~/(.*)$": "<rootDir>/app/$1",
diff --git a/package.json b/package.json
index fe5c076f61..0410db66fa 100644
--- a/package.json
+++ b/package.json
@@ -71,6 +71,7 @@
"react-dnd-html5-backend": "^14.1.0",
"react-dnd-touch-backend": "^15.1.1",
"react-dom": "^17.0.2",
+ "react-error-boundary": "^3.1.4",
"react-hotkeys-hook": "^3.4.4",
"react-nano-state": "^0.4.0",
"react-router-dom": "^6.2.2",
@@ -89,6 +90,8 @@
"@storybook/manager-webpack4": "^6.5.9",
"@storybook/react": "^6.5.9",
"@storybook/testing-library": "^0.0.13",
+ "@testing-library/react": "^12.1.5",
+ "@testing-library/react-hooks": "^8.0.1",
"@types/css-tree": "^1.0.7",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/hyphenate-style-name": "^1.0.0",
@@ -121,6 +124,7 @@
"lint-staged": "^13.0.2",
"prettier": "^2.6.2",
"prisma": "^3.14.0",
+ "react-test-renderer": "^17.0.2",
"relative-deps": "^1.0.7",
"typescript": "^4.1.2"
},
diff --git a/yarn.lock b/yarn.lock
index 811845096d..d12e0d3b22 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -3471,7 +3471,7 @@
dependencies:
defer-to-connect "^2.0.0"
-"@testing-library/dom@^8.3.0":
+"@testing-library/dom@^8.0.0", "@testing-library/dom@^8.3.0":
version "8.14.0"
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.14.0.tgz#c9830a21006d87b9ef6e1aae306cf49b0283e28e"
integrity sha512-m8FOdUo77iMTwVRCyzWcqxlEIk+GnopbrRI15a0EaLbpZSCinIVI4kSQzWhkShK83GogvEFJSsHF3Ws0z1vrqA==
@@ -3485,6 +3485,23 @@
lz-string "^1.4.4"
pretty-format "^27.0.2"
+"@testing-library/react-hooks@^8.0.1":
+ version "8.0.1"
+ resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
+ integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
+ dependencies:
+ "@babel/runtime" "^7.12.5"
+ react-error-boundary "^3.1.0"
+
+"@testing-library/react@^12.1.5":
+ version "12.1.5"
+ resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-12.1.5.tgz#bb248f72f02a5ac9d949dea07279095fa577963b"
+ integrity sha512-OfTXCJUFgjd/digLUuPxa0+/3ZxsQmE7ub9kcbW/wi96Bh3o/p5vrETcBGfP17NWPGqeYYl5LTRpwyGoMC4ysg==
+ dependencies:
+ "@babel/runtime" "^7.12.5"
+ "@testing-library/dom" "^8.0.0"
+ "@types/react-dom" "<18.0.0"
+
"@testing-library/user-event@^13.2.1":
version "13.5.0"
resolved "https://registry.yarnpkg.com/@testing-library/user-event/-/user-event-13.5.0.tgz#69d77007f1e124d55314a2b73fd204b333b13295"
@@ -3851,6 +3868,13 @@
"@types/react" "*"
"@types/reactcss" "*"
+"@types/react-dom@<18.0.0":
+ version "17.0.17"
+ resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.17.tgz#2e3743277a793a96a99f1bf87614598289da68a1"
+ integrity sha512-VjnqEmqGnasQKV0CWLevqMTXBYG9GbwuE6x3VetERLh0cq2LTptFE73MrQi2S7GkKXCf2GgwItB/melLnxfnsg==
+ dependencies:
+ "@types/react" "^17"
+
"@types/react-dom@^17.0.9":
version "17.0.11"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.11.tgz#e1eadc3c5e86bdb5f7684e00274ae228e7bcc466"
@@ -3874,6 +3898,15 @@
"@types/scheduler" "*"
csstype "^3.0.2"
+"@types/react@^17":
+ version "17.0.47"
+ resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.47.tgz#4ee71aaf4c5a9e290e03aa4d0d313c5d666b3b78"
+ integrity sha512-mk0BL8zBinf2ozNr3qPnlu1oyVTYq+4V7WA76RgxUAtf0Em/Wbid38KN6n4abEkvO4xMTBWmnP1FtQzgkEiJoA==
+ dependencies:
+ "@types/prop-types" "*"
+ "@types/scheduler" "*"
+ csstype "^3.0.2"
+
"@types/reactcss@*":
version "1.2.6"
resolved "https://registry.yarnpkg.com/@types/reactcss/-/reactcss-1.2.6.tgz#133c1e7e896f2726370d1d5a26bf06a30a038bcc"
@@ -12417,6 +12450,13 @@ react-element-to-jsx-string@^14.3.4:
is-plain-object "5.0.0"
react-is "17.0.2"
+react-error-boundary@^3.1.0, react-error-boundary@^3.1.4:
+ version "3.1.4"
+ resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
+ integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
+ dependencies:
+ "@babel/runtime" "^7.12.5"
+
react-hotkeys-hook@^3.4.4:
version "3.4.4"
resolved "https://registry.yarnpkg.com/react-hotkeys-hook/-/react-hotkeys-hook-3.4.4.tgz#52ba5d8ef5e47cc2e776c70a9036d518e0993d51"
@@ -12433,11 +12473,16 @@ react-inspector@^5.1.0:
is-dom "^1.0.0"
prop-types "^15.0.0"
[email protected], react-is@^17.0.1:
[email protected], react-is@^17.0.1, react-is@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
+"react-is@^16.12.0 || ^17.0.0 || ^18.0.0":
+ version "18.2.0"
+ resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b"
+ integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==
+
react-is@^16.13.1, react-is@^16.7.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
@@ -12492,6 +12537,14 @@ [email protected]:
dependencies:
history "^5.2.0"
+react-shallow-renderer@^16.13.1:
+ version "16.15.0"
+ resolved "https://registry.yarnpkg.com/react-shallow-renderer/-/react-shallow-renderer-16.15.0.tgz#48fb2cf9b23d23cde96708fe5273a7d3446f4457"
+ integrity sha512-oScf2FqQ9LFVQgA73vr86xl2NaOIX73rh+YFqcOp68CWj56tSfgtGKrEbyhCj0rSijyG9M1CYprTh39fBi5hzA==
+ dependencies:
+ object-assign "^4.1.1"
+ react-is "^16.12.0 || ^17.0.0 || ^18.0.0"
+
react-style-singleton@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/react-style-singleton/-/react-style-singleton-2.1.1.tgz#ce7f90b67618be2b6b94902a30aaea152ce52e66"
@@ -12512,6 +12565,16 @@ react-syntax-highlighter@^15.4.5:
prismjs "^1.27.0"
refractor "^3.6.0"
+react-test-renderer@^17.0.2:
+ version "17.0.2"
+ resolved "https://registry.yarnpkg.com/react-test-renderer/-/react-test-renderer-17.0.2.tgz#4cd4ae5ef1ad5670fc0ef776e8cc7e1231d9866c"
+ integrity sha512-yaQ9cB89c17PUb0x6UfWRs7kQCorVdHlutU1boVPEsB8IDZH6n9tHxMacc3y0JoXOJUsZb/t/Mb8FUWMKaM7iQ==
+ dependencies:
+ object-assign "^4.1.1"
+ react-is "^17.0.2"
+ react-shallow-renderer "^16.13.1"
+ scheduler "^0.20.2"
+
react-universal-interface@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/react-universal-interface/-/react-universal-interface-0.6.2.tgz#5e8d438a01729a4dbbcbeeceb0b86be146fe2b3b"