imageManager.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
  2. import type ApiRequestService from '~/services/data/apiRequestService'
  3. import ImageManager from '~/services/data/imageManager'
  4. import 'blob-polyfill'
  5. import { IMAGE_SIZE } from '~/types/enum/enums'
  6. let apiRequestService: ApiRequestService
  7. let imageManager: ImageManager
  8. let initConsoleError: any
  9. beforeEach(() => {
  10. // @ts-ignore
  11. apiRequestService = vi.fn() as ApiRequestService
  12. imageManager = new ImageManager(apiRequestService)
  13. // Important : Restore console error after mocking
  14. initConsoleError = console.error
  15. })
  16. afterEach(() => {
  17. vi.restoreAllMocks()
  18. console.error = initConsoleError
  19. })
  20. describe('get', () => {
  21. test('simple call', async () => {
  22. const blobPart = new Blob(['some_data'])
  23. // @ts-ignore
  24. apiRequestService.get = vi.fn((url: string, query: object) => blobPart)
  25. // @ts-ignore
  26. imageManager.getCacheKey = vi.fn(() => '123456')
  27. // @ts-ignore
  28. blobPart.toString = vi.fn(() => 'image_url')
  29. const result = await imageManager.get(1)
  30. expect(apiRequestService.get).toHaveBeenCalledWith(
  31. 'api/image/download/1/md',
  32. { 0: '123456' },
  33. )
  34. expect(result).toEqual('/image_url?0=123456')
  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, IMAGE_SIZE.MD, defaultImage)).toEqual(
  42. defaultImage,
  43. )
  44. })
  45. test('no response, no default image', async () => {
  46. // @ts-ignore
  47. imageManager.getProcessed = vi.fn(() => {
  48. throw new Error('Error: image 1 not found')
  49. })
  50. console.error = vi.fn()
  51. const result = await imageManager.get(1)
  52. expect(console.error).toHaveBeenCalled()
  53. expect(result).toEqual(ImageManager.defaultImage)
  54. })
  55. test('no response, default image', async () => {
  56. // @ts-ignore
  57. imageManager.getProcessed = vi.fn(() => {
  58. throw new Error('Error: image 1 not found')
  59. })
  60. console.error = vi.fn()
  61. const defaultImage = 'some_default.jpg'
  62. const result = await imageManager.get(1, IMAGE_SIZE.MD, defaultImage)
  63. expect(console.error).toHaveBeenCalled()
  64. expect(result).toEqual(defaultImage)
  65. })
  66. })
  67. describe('toBase64', () => {
  68. test('simple call', async () => {
  69. // @ts-ignore
  70. expect(await imageManager.toBase64('some_data')).toEqual(
  71. 'data:image/jpeg;base64,c29tZV9kYXRh',
  72. )
  73. })
  74. })
  75. describe('getCacheKey', () => {
  76. test('simple call', () => {
  77. // @ts-ignore
  78. expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
  79. })
  80. })