imageManager.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: TestableImageManager
  7. class TestableImageManager extends ImageManager {
  8. public async toBase64(data: string) { return super.toBase64(data) }
  9. public getCacheKey() { return super.getCacheKey() }
  10. }
  11. let init_console_error: any
  12. beforeEach(() => {
  13. // @ts-ignore
  14. apiRequestService = vi.fn() as ApiRequestService
  15. imageManager = new TestableImageManager(apiRequestService)
  16. // Important : Restore console error after mocking
  17. init_console_error = console.error
  18. })
  19. afterEach(() => {
  20. vi.restoreAllMocks()
  21. console.error = init_console_error
  22. })
  23. describe('get', () => {
  24. test('simple call', async () => {
  25. const data = "azerty"
  26. // @ts-ignore
  27. apiRequestService.get = vi.fn((url: string) => data)
  28. imageManager.getCacheKey = vi.fn(() => '123456')
  29. // @ts-ignore
  30. imageManager.toBase64 = vi.fn((data: string) => 'base64:' + data)
  31. const result = await imageManager.get(1)
  32. expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', ['123456'])
  33. expect(result).toEqual('base64:azerty')
  34. })
  35. test('no id, no default image provided', async () => {
  36. expect(await imageManager.get(null)).toEqual(ImageManager.defaultImage)
  37. })
  38. test('no id, default image provided', async () => {
  39. const defaultImage = 'a_picture.jpg'
  40. expect(await imageManager.get(null, defaultImage)).toEqual(defaultImage)
  41. })
  42. test('no response, no default image', async () => {
  43. // @ts-ignore
  44. apiRequestService.get = vi.fn((url: string) => '')
  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', ['123456'])
  51. expect(console.error).toHaveBeenCalledWith('Error: image 1 not found or invalid')
  52. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  53. expect(result).toEqual(ImageManager.defaultImage)
  54. })
  55. test('no response, default image', async () => {
  56. // @ts-ignore
  57. apiRequestService.get = vi.fn((url: string) => '')
  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. expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
  66. expect(result).toEqual('some_default.jpg')
  67. })
  68. })
  69. describe('toBase64', () => {
  70. test('simple call', async () => {
  71. expect(await imageManager.toBase64('some_data')).toEqual('data:image/jpeg;base64,c29tZV9kYXRh')
  72. })
  73. })
  74. describe('getCacheKey', () => {
  75. test('simple call', () => {
  76. expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
  77. })
  78. })