imageManager.test.ts 3.1 KB

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