uppy/packages/@uppy/compressor/src/index.test.ts
Merlijn Vos 3393b537d3
Upgrade Yarn to 4.x (#4849)
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
2024-04-29 18:49:02 +02:00

74 lines
2.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import Core from '@uppy/core'
import getFileNameAndExtension from '@uppy/utils/lib/getFileNameAndExtension'
import fs from 'node:fs'
import path from 'node:path'
import CompressorPlugin from './index.ts'
// Compressor uses browser canvas API, so need to mock compress()
// @ts-expect-error mocked
CompressorPlugin.prototype.compress = async (blob: File) => {
return {
name: `${getFileNameAndExtension(blob.name).name}.webp`,
type: 'image/webp',
data: blob,
size: 123,
}
}
// eslint-disable-next-line no-restricted-globals
const sampleImage = fs.readFileSync(
path.join(__dirname, '../../../../e2e/cypress/fixtures/images/image.jpg'),
)
const file1 = {
source: 'jest',
name: 'image-1.jpeg',
type: 'image/jpeg',
data: new File([sampleImage], 'image-1.jpeg', { type: 'image/jpeg' }),
}
const file2 = {
source: 'jest',
name: 'yolo',
type: 'image/jpeg',
data: new File([sampleImage], 'yolo', { type: 'image/jpeg' }),
}
const file3 = {
source: 'jest',
name: 'my.file.is.weird.png',
type: 'image/png',
data: new File([sampleImage], 'my.file.is.weird.png', { type: 'image/png' }),
}
describe('CompressorPlugin', () => {
it('should change update extension in file.name and file.meta.name', () => {
const uppy = new Core()
uppy.use(CompressorPlugin, {
quality: 0.85,
mimeType: 'image/webp',
})
uppy.addFile(file1)
uppy.addFile(file2)
uppy.addFile(file3)
// User changed file.meta.name
uppy.setFileMeta(uppy.getFiles()[0].id, { name: 'new-name.jpeg' })
return uppy.upload().then(() => {
const files = uppy.getFiles()
expect(files[0].meta.name).toEqual('new-name.webp')
expect(files[0].name).toEqual('image-1.webp')
expect(files[0].meta.type).toEqual('image/webp')
expect(files[1].meta.name).toEqual('yolo.webp')
expect(files[1].meta.type).toEqual('image/webp')
expect(files[1].name).toEqual('yolo.webp')
expect(files[2].meta.name).toEqual('my.file.is.weird.webp')
expect(files[2].meta.type).toEqual('image/webp')
expect(files[2].name).toEqual('my.file.is.weird.webp')
})
})
})