imageManager.test.ts 3.2 KB

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