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 tests

Andras Bacsai963f3ab65049
diff --git a/packages/http-client/jest.config.js b/packages/http-client/jest.config.js
new file mode 100644
index 0000000000..4eeecbaf4e
--- /dev/null
+++ b/packages/http-client/jest.config.js
@@ -0,0 +1 @@
+module.exports = require("@webstudio-is/jest-config");
diff --git a/packages/http-client/package.json b/packages/http-client/package.json
index f5785a95b6..154c15b97c 100644
--- a/packages/http-client/package.json
+++ b/packages/http-client/package.json
@@ -6,19 +6,21 @@
"homepage": "https://webstudio.is",
"scripts": {
"dev": "tsup src/index.ts --format esm,cjs --watch --out-dir=lib",
+ "test": "jest",
"build": "rm -fr lib && tsc",
"typecheck": "tsc --noEmit",
"lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
"checks": "yarn typecheck && yarn lint"
},
"devDependencies": {
- "@types/isomorphic-fetch": "^0.0.36",
+ "@webstudio-is/jest-config": "*",
+ "@webstudio-is/prisma-client": "*",
"tsup": "^6.1.3",
"typescript": "^4.6.2"
},
"peerDependencies": {},
"dependencies": {
- "isomorphic-fetch": "^3.0.0"
+ "isomorphic-unfetch": "^3.1.0"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
diff --git a/packages/http-client/src/index.d.ts b/packages/http-client/src/index.d.ts
index 3185eb209b..f5d8eb37b7 100644
--- a/packages/http-client/src/index.d.ts
+++ b/packages/http-client/src/index.d.ts
@@ -1,7 +1,17 @@
export type IncludeTypes = "tree" | "props" | "breakpoints";
export type Includes<T> = { [include in IncludeTypes]: T };
export type Project = {
- tree?: string;
- props?: string;
- breakpoints?: string;
+ tree: {
+ id: string;
+ root: {
+ comopnent: string;
+ id: string;
+ cssRules: Array<string>;
+ children: Array<string>;
+ };
+ } & { errors: string };
+ props: Array<string> & { errors: string };
+ breakpoints: Array<{ label: string; minWidtrh: number; id: string }> & {
+ errors: string;
+ };
};
diff --git a/packages/http-client/src/index.test.ts b/packages/http-client/src/index.test.ts
new file mode 100644
index 0000000000..cdca00f77e
--- /dev/null
+++ b/packages/http-client/src/index.test.ts
@@ -0,0 +1,26 @@
+import { loadProjectFromRest } from "./index";
+
+const existingProject = "ab3619d3-831f-46f8-abb6-609558cffd99";
+const notPublishedProject = "7ec397c6-b3d0-4967-9073-9d83623fcf8e";
+const nonExistingProject = "1";
+
+describe("getProjectDetails", () => {
+ test("loads existing project", async () => {
+ const response = await loadProjectFromRest({ projectId: existingProject });
+ expect(response.tree.id).toBeTruthy();
+ });
+ test("loads non-existing project", async () => {
+ const response = await loadProjectFromRest({
+ projectId: nonExistingProject,
+ });
+ expect(response.tree.errors).toBe("Project required");
+ });
+ test("loads not published project", async () => {
+ const response = await loadProjectFromRest({
+ projectId: notPublishedProject,
+ });
+ expect(response.tree.errors).toBe(
+ "Site needs to be published, production tree ID is null."
+ );
+ });
+});
diff --git a/packages/http-client/src/index.ts b/packages/http-client/src/index.ts
index e9e4befb22..e4ba6dbda8 100644
--- a/packages/http-client/src/index.ts
+++ b/packages/http-client/src/index.ts
@@ -1,14 +1,19 @@
-import isoMorphicFetch from "isomorphic-fetch";
+import isoMorphicFetch from "isomorphic-unfetch";
import type { Includes, Project } from "./index.d";
const protocol = process.env.NODE_ENV === "production" ? "https" : "http";
+
const fetch = async (url: string, options?: any) => {
const response = await isoMorphicFetch(url, options);
- const contentType = response.headers.get("content-type");
- if (contentType && contentType.indexOf("application/json") !== -1) {
- return response.json();
+ if (response.ok) {
+ const contentType = response.headers.get("content-type");
+ if (contentType && contentType.indexOf("application/json") !== -1) {
+ return response.json();
+ } else {
+ return response.text();
+ }
} else {
- return response.text();
+ throw new Error(response.statusText);
}
};
diff --git a/yarn.lock b/yarn.lock
index c9c32a8cf1..22cf874352 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5030,11 +5030,6 @@
resolved "https://registry.yarnpkg.com/@types/is-function/-/is-function-1.0.1.tgz#2d024eace950c836d9e3335a66b97960ae41d022"
integrity sha512-A79HEEiwXTFtfY+Bcbo58M2GRYzCr9itHWzbzHVFNEYCcoU/MMGwYYf721gBrnhpj1s6RGVVha/IgNFnR0Iw/Q==
-"@types/isomorphic-fetch@^0.0.36":
- version "0.0.36"
- resolved "https://registry.yarnpkg.com/@types/isomorphic-fetch/-/isomorphic-fetch-0.0.36.tgz#3a6d8f63974baf26b10fd1314db910633108a769"
- integrity sha512-ulw4d+vW1HKn4oErSmNN2HYEcHGq0N1C5exlrMM0CRqX1UUpFhGb5lwiom5j9KN3LBJJDLRmYIZz1ghm7FIzZw==
-
"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz#8467d4b3c087805d63580480890791277ce35c44"
@@ -11280,14 +11275,6 @@ isobject@^4.0.0:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-4.0.0.tgz#3f1c9155e73b192022a80819bacd0343711697b0"
integrity sha512-S/2fF5wH8SJA/kmwr6HYhK/RI/OkhD84k8ntalo0iJjZikgq1XFvR5M8NPT1x5F7fBwCG3qHfnzeP/Vh/ZxCUA==
-isomorphic-fetch@^3.0.0:
- version "3.0.0"
- resolved "https://registry.yarnpkg.com/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz#0267b005049046d2421207215d45d6a262b8b8b4"
- integrity sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==
- dependencies:
- node-fetch "^2.6.1"
- whatwg-fetch "^3.4.1"
-
isomorphic-unfetch@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/isomorphic-unfetch/-/isomorphic-unfetch-3.1.0.tgz#87341d5f4f7b63843d468438128cb087b7c3e98f"
@@ -17746,11 +17733,6 @@ whatwg-encoding@^1.0.5:
dependencies:
iconv-lite "0.4.24"
-whatwg-fetch@^3.4.1:
- version "3.6.2"
- resolved "https://registry.yarnpkg.com/whatwg-fetch/-/whatwg-fetch-3.6.2.tgz#dced24f37f2624ed0281725d51d0e2e3fe677f8c"
- integrity sha512-bJlen0FcuU/0EMLrdbJ7zOnW6ITZLrZMIarMUVmdKtsGvZna8vxKYaexICWPfZ8qwf9fzNq+UEIZrnSaApt6RA==
-
whatwg-mimetype@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"