imageManager.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { vi, describe, test, expect, beforeEach, afterEach } from 'vitest'
  2. import type ApiRequestService from '~/services/data/apiRequestService'
  3. import ImageManager from '~/services/data/imageManager'
  4. import 'blob-polyfill'
  5. let apiRequestService: ApiRequestService
  6. let imageManager: ImageManager
  7. let initConsoleError: any
  8. beforeEach(() => {
  9. // @ts-ignore
  10. apiRequestService = vi.fn() as ApiRequestService
  11. imageManager = new ImageManager(apiRequestService)
  12. // Important : Restore console error after mocking
  13. initConsoleError = console.error
  14. })
  15. afterEach(() => {
  16. vi.restoreAllMocks()
  17. console.error = initConsoleError
  18. })
  19. describe('get', () => {
  20. test('simple call', async () => {
  21. const blobPart = new Blob(['some_data'])
  22. // @ts-ignore
  23. apiRequestService.get = vi.fn((url: string, query: object) => blobPart)
  24. // @ts-ignore
  25. imageManager.getCacheKey = vi.fn(() => '123456')
  26. // @ts-ignore
  27. imageManager.toBase64 = vi.fn((_: string) => 'base64:azerty')
  28. const result = await imageManager.get(1)
  29. expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
  30. '123456',
  31. ])
  32. // @ts-ignore
  33. expect(imageManager.toBase64).toHaveBeenCalledWith(blobPart)
  34. expect(result).toEqual('base64:azerty')
  35. })
  36. test('no id, no default image provided', async () => {
  37. expect(await imageManager.get(null)).toEqual(ImageManager.defaultImage)
  38. })
  39. test('no id, default image provided', async () => {
  40. const defaultImage = 'a_picture.jpg'
  41. expect(await imageManager.get(null, defaultImage)).toEqual(defaultImage)
  42. })
  43. test('no response, no default image', async () => {
  44. const blobPart = vi.fn()
  45. // @ts-ignore
  46. apiRequestService.get = vi.fn((url: string) => null)
  47. // @ts-ignore
  48. imageManager.getCacheKey = vi.fn(() => '123456')
  49. // @ts-ignore
  50. imageManager.toBase64 = vi.fn()
  51. console.error = vi.fn()
  52. const result = await imageManager.get(1)
  53. expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
  54. '123456',
  55. ])
  56. expect(console.error).toHaveBeenCalledWith('Error: image 1 not found')
  57. // @ts-ignore
  58. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  59. expect(result).toEqual(ImageManager.defaultImage)
  60. })
  61. test('no response, default image', async () => {
  62. const blobPart = vi.fn()
  63. // @ts-ignore
  64. blobPart.size = 0
  65. const response = vi.fn()
  66. // @ts-ignore
  67. response.blob = () => blobPart
  68. // @ts-ignore
  69. apiRequestService.get = vi.fn((url: string) => response)
  70. // @ts-ignore
  71. imageManager.getCacheKey = vi.fn(() => '123456')
  72. // @ts-ignore
  73. imageManager.toBase64 = vi.fn()
  74. console.error = vi.fn()
  75. const result = await imageManager.get(1, 'some_default.jpg')
  76. expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
  77. '123456',
  78. ])
  79. expect(console.error).toHaveBeenCalledWith('Error: image 1 is invalid')
  80. // @ts-ignore
  81. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  82. expect(result).toEqual('some_default.jpg')
  83. })
  84. })
  85. describe('toBase64', () => {
  86. test('simple call', async () => {
  87. // @ts-ignore
  88. expect(await imageManager.toBase64('some_data')).toEqual(
  89. 'data:image/jpeg;base64,c29tZV9kYXRh',
  90. )
  91. })
  92. })
  93. describe('getCacheKey', () => {
  94. test('simple call', () => {
  95. // @ts-ignore
  96. expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
  97. })
  98. })