index.test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { describe, expect, it } from 'vitest'
  2. import Core from '@uppy/core'
  3. import getFileNameAndExtension from '@uppy/utils/lib/getFileNameAndExtension'
  4. import fs from 'node:fs'
  5. import path from 'node:path'
  6. import CompressorPlugin from './index.js'
  7. // Compressor uses browser canvas API, so need to mock compress()
  8. CompressorPlugin.prototype.compress = (blob) => {
  9. return {
  10. name: `${getFileNameAndExtension(blob.name).name}.webp`,
  11. type: 'image/webp',
  12. data: blob,
  13. size: 123,
  14. }
  15. }
  16. // eslint-disable-next-line no-restricted-globals
  17. const sampleImage = fs.readFileSync(path.join(__dirname, '../../../../e2e/cypress/fixtures/images/image.jpg'))
  18. const file1 = { source: 'jest', name: 'image-1.jpeg', type: 'image/jpeg', data: new File([sampleImage], 'image-1.jpeg', { type: 'image/jpeg' }) }
  19. const file2 = { source: 'jest', name: 'yolo', type: 'image/jpeg', data: new File([sampleImage], 'yolo', { type: 'image/jpeg' }) }
  20. 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' }) }
  21. describe('CompressorPlugin', () => {
  22. it('should change update extension in file.name and file.meta.name', () => {
  23. const uppy = new Core()
  24. uppy.use(CompressorPlugin, {
  25. quality: 0.85,
  26. mimeType: 'image/webp',
  27. })
  28. uppy.addFile(file1)
  29. uppy.addFile(file2)
  30. uppy.addFile(file3)
  31. // User changed file.meta.name
  32. uppy.setFileMeta(uppy.getFiles()[0].id, { name: 'new-name.jpeg' })
  33. return uppy.upload().then(() => {
  34. const files = uppy.getFiles()
  35. expect(files[0].meta.name).toEqual('new-name.webp')
  36. expect(files[0].name).toEqual('image-1.webp')
  37. expect(files[0].meta.type).toEqual('image/webp')
  38. expect(files[1].meta.name).toEqual('yolo.webp')
  39. expect(files[1].meta.type).toEqual('image/webp')
  40. expect(files[1].name).toEqual('yolo.webp')
  41. expect(files[2].meta.name).toEqual('my.file.is.weird.webp')
  42. expect(files[2].meta.type).toEqual('image/webp')
  43. expect(files[2].name).toEqual('my.file.is.weird.webp')
  44. })
  45. })
  46. })