Replace variadic spread pattern with properly-typed single-argument mock. storage.from() only takes bucketId string argument, so fromMock type accurately reflects the actual API. No suppression needed - TypeScript and ESLint both pass cleanly.
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
|
|
const uploadMock = vi.fn()
|
|
const getPublicUrlMock = vi.fn()
|
|
const fromMock = vi.fn()
|
|
|
|
vi.mock('./supabase', () => ({
|
|
supabase: { storage: { from: (bucketId: string) => {
|
|
fromMock(bucketId)
|
|
return { upload: uploadMock, getPublicUrl: getPublicUrlMock }
|
|
} } },
|
|
}))
|
|
|
|
import { uploadSalonImage } from './storage'
|
|
|
|
describe('uploadSalonImage', () => {
|
|
beforeEach(() => {
|
|
uploadMock.mockReset()
|
|
getPublicUrlMock.mockReset()
|
|
fromMock.mockClear()
|
|
})
|
|
|
|
it('uploads to the salon-images bucket and returns the public URL', async () => {
|
|
uploadMock.mockResolvedValue({ error: null })
|
|
getPublicUrlMock.mockReturnValue({ data: { publicUrl: 'https://example.supabase.co/storage/v1/object/public/salon-images/products/edge-control.jpg' } })
|
|
|
|
const file = new File(['fake'], 'edge-control.jpg', { type: 'image/jpeg' })
|
|
const url = await uploadSalonImage(file, 'products/edge-control.jpg')
|
|
|
|
expect(fromMock).toHaveBeenCalledWith('salon-images')
|
|
expect(uploadMock).toHaveBeenCalledWith('products/edge-control.jpg', file, { upsert: true })
|
|
expect(url).toBe('https://example.supabase.co/storage/v1/object/public/salon-images/products/edge-control.jpg')
|
|
})
|
|
|
|
it('throws when the upload fails', async () => {
|
|
uploadMock.mockResolvedValue({ error: { message: 'Storage error' } })
|
|
const file = new File(['fake'], 'bad.jpg', { type: 'image/jpeg' })
|
|
|
|
await expect(uploadSalonImage(file, 'products/bad.jpg')).rejects.toEqual({ message: 'Storage error' })
|
|
})
|
|
})
|