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.

start adding tests

Sara Vieira495b4399a0eb
diff --git a/packages/asset-uploader/src/helpers/get-asset-path.test.ts b/packages/asset-uploader/src/helpers/get-asset-path.test.ts
new file mode 100644
index 0000000000..68190db7c3
--- /dev/null
+++ b/packages/asset-uploader/src/helpers/get-asset-path.test.ts
@@ -0,0 +1,51 @@
+/* eslint-disable @typescript-eslint/no-var-requires */
+import { Decimal } from "@prisma/client/runtime";
+
+const commonAsset = {
+ id: "sa-546",
+ width: new Decimal(200),
+ height: new Decimal(200),
+ projectId: "jdhajk",
+ size: 2135,
+ format: "png",
+ createdAt: new Date(),
+ alt: "",
+};
+
+describe("getAssetPath", () => {
+ const OLD_ENV = process.env;
+
+ beforeEach(() => {
+ jest.resetModules(); // Most important - it clears the cache
+ process.env = { ...OLD_ENV }; // Make a copy
+ });
+
+ afterAll(() => {
+ process.env = OLD_ENV; // Restore old environment
+ });
+
+ describe("local", () => {
+ test("return local path with no custom path", () => {
+ const { getAssetPath } = require("./get-asset-path");
+ expect(
+ getAssetPath({
+ ...commonAsset,
+ name: "test.png",
+ location: "FS",
+ })
+ ).toBe("/uploads/test.png");
+ });
+
+ test("return local path with custom path", () => {
+ process.env.FILE_UPLOAD_PATH = "assets";
+ const { getAssetPath } = require("./get-asset-path");
+ expect(
+ getAssetPath({
+ ...commonAsset,
+ name: "test.png",
+ location: "FS",
+ })
+ ).toBe("/assets/test.png");
+ });
+ });
+});
diff --git a/packages/asset-uploader/src/helpers/get-asset-path.ts b/packages/asset-uploader/src/helpers/get-asset-path.ts
index ce103e2283..bfefab552e 100644
--- a/packages/asset-uploader/src/helpers/get-asset-path.ts
+++ b/packages/asset-uploader/src/helpers/get-asset-path.ts
@@ -7,7 +7,7 @@ const fsEnvs = fsEnvVariables.parse(process.env);
export const getAssetPath = (asset: Asset) => {
if (asset.location === "FS") {
- return path.join(fsEnvs.FILE_UPLOAD_PATH, asset.name);
+ return path.join("/", fsEnvs.FILE_UPLOAD_PATH, asset.name);
}
if (asset.location === "REMOTE" && s3Envs.success) {