|
| 1 | +import { put } from '@vercel/blob' |
| 2 | +import { describe, expect, it, vi } from 'vitest' |
| 3 | + |
| 4 | +vi.mock('@vercel/blob', async (importOriginal) => ({ |
| 5 | + ...(await importOriginal<typeof import('@vercel/blob')>()), |
| 6 | + put: vi.fn(async (pathname: string) => ({ pathname: pathname.replace('.png', '-suffix.png') })), |
| 7 | +})) |
| 8 | + |
| 9 | +import { createVercelBlobAdapter } from './adapter.js' |
| 10 | + |
| 11 | +const adapter = createVercelBlobAdapter({ |
| 12 | + access: 'public', |
| 13 | + addRandomSuffix: true, |
| 14 | + baseUrl: 'https://example.com', |
| 15 | + cacheControlMaxAge: 60, |
| 16 | + token: 'token', |
| 17 | +})({ collection: { fields: [], slug: 'media', upload: {} }, prefix: '' }) |
| 18 | + |
| 19 | +describe('handleUpload with addRandomSuffix', () => { |
| 20 | + it.each([ |
| 21 | + ['main file', 'image.png', { filename: 'image-suffix.png', square: 'image-400x400.png' }], |
| 22 | + [ |
| 23 | + 'image size', |
| 24 | + 'image-400x400.png', |
| 25 | + { filename: 'image.png', square: 'image-400x400-suffix.png' }, |
| 26 | + ], |
| 27 | + ])('should set the suffixed filename only on the %s', async (_, filename, expected) => { |
| 28 | + const data = { filename: 'image.png', sizes: { square: { filename: 'image-400x400.png' } } } |
| 29 | + |
| 30 | + await adapter.handleUpload({ |
| 31 | + data, |
| 32 | + file: { |
| 33 | + buffer: Buffer.from('file'), |
| 34 | + filename, |
| 35 | + filesize: 4, |
| 36 | + imageSizeName: filename === 'image.png' ? undefined : 'square', |
| 37 | + mimeType: 'image/png', |
| 38 | + }, |
| 39 | + storageFilePath: filename, |
| 40 | + } as never) |
| 41 | + |
| 42 | + expect({ filename: data.filename, square: data.sizes.square.filename }).toEqual(expected) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should give image sizes with the same dimensions their own suffixed filenames', async () => { |
| 46 | + vi.mocked(put) |
| 47 | + .mockResolvedValueOnce({ pathname: 'image-400x400-a.png' } as never) |
| 48 | + .mockResolvedValueOnce({ pathname: 'image-400x400-b.png' } as never) |
| 49 | + |
| 50 | + const data = { |
| 51 | + filename: 'image.png', |
| 52 | + sizes: { card: { filename: 'image-400x400.png' }, square: { filename: 'image-400x400.png' } }, |
| 53 | + } |
| 54 | + const uploadImageSize = (imageSizeName: string) => |
| 55 | + adapter.handleUpload({ |
| 56 | + data, |
| 57 | + file: { |
| 58 | + buffer: Buffer.from('file'), |
| 59 | + filename: 'image-400x400.png', |
| 60 | + filesize: 4, |
| 61 | + imageSizeName, |
| 62 | + mimeType: 'image/png', |
| 63 | + }, |
| 64 | + storageFilePath: 'image-400x400.png', |
| 65 | + } as never) |
| 66 | + |
| 67 | + await Promise.all([uploadImageSize('card'), uploadImageSize('square')]) |
| 68 | + |
| 69 | + expect(data.sizes).toEqual({ |
| 70 | + card: { filename: 'image-400x400-a.png' }, |
| 71 | + square: { filename: 'image-400x400-b.png' }, |
| 72 | + }) |
| 73 | + }) |
| 74 | +}) |
0 commit comments