Skip to content

Commit 856d08a

Browse files
committed
Add test for using existing filename in uploadImage function
1 parent 0f89997 commit 856d08a

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

tests/imageUtils.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ describe('imageUtils', () => {
4343
expect(url).toBe('https://public.url/file.png');
4444
});
4545

46+
it('uses existing filename from fileUrl if provided', async () => {
47+
// Simulate providing an existing URL so the helper extracts the filename
48+
const existingUrl = 'https://xyz.supabase.co/storage/v1/object/public/images/folder/existing.png';
49+
storageMock.upload.mockResolvedValue({ error: null });
50+
storageMock.getPublicUrl.mockReturnValue({ data: { publicUrl: 'https://public.url/existing.png' } });
51+
52+
const url = await uploadImage(
53+
mockSupabase as SupabaseClient,
54+
dummyFile,
55+
'folder',
56+
existingUrl
57+
);
58+
// Should use filename 'existing.png' instead of generating with uuid
59+
expect(storageMock.upload).toHaveBeenCalledWith(
60+
'folder/existing.png',
61+
dummyFile.buffer,
62+
{ contentType: dummyFile.mimetype, upsert: true }
63+
);
64+
expect(url).toBe('https://public.url/existing.png');
65+
});
66+
4667
it('throws ApiError for missing mimetype', async () => {
4768
const badFile = { ...dummyFile, mimetype: '' };
4869
await expect(

0 commit comments

Comments
 (0)