imageManager.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { vi, describe, test, expect, beforeEach, afterEach } from 'vitest'
  2. import 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 = vi.fn()
  22. const response = vi.fn()
  23. // @ts-ignore
  24. response.blob = () => blobPart
  25. // @ts-ignore
  26. apiRequestService.get = vi.fn((url: string) => response)
  27. // @ts-ignore
  28. imageManager.getCacheKey = vi.fn(() => '123456')
  29. // @ts-ignore
  30. imageManager.toBase64 = vi.fn((_: string) => 'base64:azerty')
  31. const result = await imageManager.get(1)
  32. expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
  33. '123456',
  34. ])
  35. // @ts-ignore
  36. expect(imageManager.toBase64).toHaveBeenCalledWith(blobPart)
  37. expect(result).toEqual('base64:azerty')
  38. })
  39. test('no id, no default image provided', async () => {
  40. expect(await imageManager.get(null)).toEqual(ImageManager.defaultImage)
  41. })
  42. test('no id, default image provided', async () => {
  43. const defaultImage = 'a_picture.jpg'
  44. expect(await imageManager.get(null, defaultImage)).toEqual(defaultImage)
  45. })
  46. test('no response, no default image', async () => {
  47. const blobPart = vi.fn()
  48. // @ts-ignore
  49. apiRequestService.get = vi.fn((url: string) => null)
  50. // @ts-ignore
  51. imageManager.getCacheKey = vi.fn(() => '123456')
  52. // @ts-ignore
  53. imageManager.toBase64 = vi.fn()
  54. console.error = vi.fn()
  55. const result = await imageManager.get(1)
  56. expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
  57. '123456',
  58. ])
  59. expect(console.error).toHaveBeenCalledWith('Error: image 1 not found')
  60. // @ts-ignore
  61. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  62. expect(result).toEqual(ImageManager.defaultImage)
  63. })
  64. test('no response, default image', async () => {
  65. const blobPart = vi.fn()
  66. // @ts-ignore
  67. blobPart.size = 0
  68. const response = vi.fn()
  69. // @ts-ignore
  70. response.blob = () => blobPart
  71. // @ts-ignore
  72. apiRequestService.get = vi.fn((url: string) => response)
  73. // @ts-ignore
  74. imageManager.getCacheKey = vi.fn(() => '123456')
  75. // @ts-ignore
  76. imageManager.toBase64 = vi.fn()
  77. console.error = vi.fn()
  78. const result = await imageManager.get(1, 'some_default.jpg')
  79. expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
  80. '123456',
  81. ])
  82. expect(console.error).toHaveBeenCalledWith('Error: image 1 is invalid')
  83. // @ts-ignore
  84. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  85. expect(result).toEqual('some_default.jpg')
  86. })
  87. })
  88. describe('toBase64', () => {
  89. test('simple call', async () => {
  90. // @ts-ignore
  91. expect(await imageManager.toBase64('some_data')).toEqual(
  92. 'data:image/jpeg;base64,c29tZV9kYXRh',
  93. )
  94. })
  95. })
  96. describe('getCacheKey', () => {
  97. test('simple call', () => {
  98. // @ts-ignore
  99. expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
  100. })
  101. })