files.spec.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. getAcceptableFileTypesString,
  4. humanizeFileSize,
  5. validateFileSizes,
  6. type AllowedFile,
  7. } from '#shared/utils/files.ts'
  8. describe('files utility', () => {
  9. describe('getAcceptableFileTypesString', () => {
  10. it('returns a string of acceptable file types for native input', () => {
  11. const allowedFiles: AllowedFile[] = [
  12. { label: 'Image', types: ['image/jpeg', 'image/png'], size: 5000000 },
  13. { label: 'Document', types: ['application/pdf'], size: 10000000 },
  14. ]
  15. const result = getAcceptableFileTypesString(allowedFiles)
  16. expect(result).toBe('image/jpeg, image/png, application/pdf')
  17. })
  18. it('returns an empty string when no file types are allowed', () => {
  19. const allowedFiles: AllowedFile[] = []
  20. const result = getAcceptableFileTypesString(allowedFiles)
  21. expect(result).toBe('')
  22. })
  23. })
  24. describe('humanizeFileSize', () => {
  25. it('returns file size in MB when size is more than 1GB', () => {
  26. const size = 1024 * 1024 * 1024 * 1.5 // 1.5GB
  27. const result = humanizeFileSize(size)
  28. expect(result).toBe('1536 MB')
  29. })
  30. it('returns file size in KB when size is more than 1MB and less than 1GB', () => {
  31. const size = 1024 * 1024 * 0.5 // 0.5MB
  32. const result = humanizeFileSize(size)
  33. expect(result).toBe('512 KB')
  34. })
  35. it('returns file size in Bytes when size is less than 1MB', () => {
  36. const size = 1024 * 0.5 // 0.5KB
  37. const result = humanizeFileSize(size)
  38. expect(result).toBe('512 Bytes')
  39. })
  40. })
  41. describe('validateFileSizes', () => {
  42. it('returns an empty array when all files are within the allowed size', () => {
  43. const file1 = new File(['content'], 'file1.txt', { type: 'text/plain' })
  44. const file2 = new File(['content'], 'file2.txt', { type: 'text/plain' })
  45. const files: File[] = [file1, file2]
  46. const allowedFiles: AllowedFile[] = [
  47. { label: 'Text', types: ['text/plain'], size: 5000000 },
  48. ]
  49. const result = validateFileSizes(files, allowedFiles)
  50. expect(result).toEqual([])
  51. })
  52. it('returns an array of failed files when some files exceed the allowed size', () => {
  53. // :TODO let's add createFile util
  54. const file1 = new File(['content'], 'file1.txt', { type: 'text/plain' })
  55. Object.defineProperty(file1, 'size', { value: 6000000 })
  56. const file2 = new File(['content'], 'file1.jpeg', { type: 'image/jpeg' })
  57. Object.defineProperty(file2, 'size', { value: 6000000 })
  58. const file3 = new File(['content'], 'file1.jpeg', { type: 'image/jpeg' })
  59. Object.defineProperty(file3, 'size', { value: 4000000 })
  60. const files = [file1, file2, file3]
  61. const allowedFiles: AllowedFile[] = [
  62. { label: 'Text', types: ['text/plain'], size: 5000000 },
  63. { label: 'Image', types: ['image/jpeg'], size: 5000000 },
  64. ]
  65. const result = validateFileSizes(files, allowedFiles)
  66. expect(result).toEqual([
  67. { file: file1, label: 'Text', maxSize: 5000000 },
  68. { file: file2, label: 'Image', maxSize: 5000000 },
  69. ])
  70. })
  71. })
  72. })