Skip to content

Commit a51fb63

Browse files
committed
update test for imageutils update function
1 parent 654d2a1 commit a51fb63

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/utils/imageUtils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ export async function uploadImage(
3737
}
3838
const ext = mime.split("/")[1];
3939

40-
41-
if (fileUrl) {
42-
await deleteImage(supabase,fileUrl);
43-
}
4440
const filename:string = `${uuidv4()}.${ext}`;
4541

4642
const filePath = `${folder}/${filename}`;
@@ -64,6 +60,10 @@ export async function uploadImage(
6460
throw new ApiError("Failed to get public URL", 500);
6561
}
6662

63+
if (fileUrl) {
64+
await deleteImage(supabase, fileUrl);
65+
}
66+
6767
return urlData.publicUrl;
6868
}
6969

tests/imageUtils.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { uploadImage, deleteImage } from "../src/utils/imageUtils";
22
import { SupabaseClient, PostgrestError } from "@supabase/supabase-js";
33
import { ApiError } from "../src/utils/apiError";
44
import { v4 as uuidv4 } from "uuid";
5+
import { error } from "console";
56

67
jest.mock("uuid");
78
const mockedUuid = uuidv4 as jest.Mock;
@@ -21,6 +22,7 @@ describe("imageUtils", () => {
2122
from: jest.fn().mockReturnThis(),
2223
upload: jest.fn(),
2324
getPublicUrl: jest.fn(),
25+
deleteImage: jest.fn(),
2426
remove: jest.fn(),
2527
};
2628

@@ -61,6 +63,7 @@ describe("imageUtils", () => {
6163
storageMock.getPublicUrl.mockReturnValue({
6264
data: { publicUrl: "https://public.url/existing.png" },
6365
});
66+
storageMock.remove.mockReturnValue({error:null});
6467

6568
const url = await uploadImage(
6669
mockSupabase as SupabaseClient,
@@ -70,7 +73,7 @@ describe("imageUtils", () => {
7073
);
7174
// Should use filename 'existing.png' instead of generating with uuid
7275
expect(storageMock.upload).toHaveBeenCalledWith(
73-
"folder/existing.png",
76+
"folder/test-uuid.png",
7477
dummyFile.buffer,
7578
{ contentType: dummyFile.mimetype, upsert: true },
7679
);

0 commit comments

Comments
 (0)