|
|
@@ -0,0 +1,110 @@
|
|
|
+import { describe, test, it, expect } from 'vitest'
|
|
|
+import ApiRequestService from "~/services/data/apiRequestService";
|
|
|
+import ImageManager from "~/services/data/imageManager";
|
|
|
+import 'blob-polyfill';
|
|
|
+
|
|
|
+let apiRequestService: ApiRequestService
|
|
|
+let imageManager: TestableImageManager
|
|
|
+
|
|
|
+class TestableImageManager extends ImageManager {
|
|
|
+ public async toBase64(data: string) { return super.toBase64(data) }
|
|
|
+ public getCacheKey() { return super.getCacheKey() }
|
|
|
+}
|
|
|
+
|
|
|
+let init_console_error: any
|
|
|
+
|
|
|
+beforeEach(() => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService = vi.fn() as ApiRequestService
|
|
|
+
|
|
|
+ imageManager = new TestableImageManager(apiRequestService)
|
|
|
+
|
|
|
+ // Important : Restore console error after mocking
|
|
|
+ init_console_error = console.error
|
|
|
+})
|
|
|
+
|
|
|
+afterEach(() => {
|
|
|
+ vi.restoreAllMocks()
|
|
|
+ console.error = init_console_error
|
|
|
+})
|
|
|
+
|
|
|
+describe('get', () => {
|
|
|
+ test('simple call', async () => {
|
|
|
+ const data = "azerty"
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.get = vi.fn((url: string) => data)
|
|
|
+
|
|
|
+ imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ imageManager.toBase64 = vi.fn((data: string) => 'base64:' + data)
|
|
|
+
|
|
|
+ const result = await imageManager.get(1)
|
|
|
+
|
|
|
+ expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', ['123456'])
|
|
|
+
|
|
|
+ expect(result).toEqual('base64:azerty')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('no id, no default image provided', async () => {
|
|
|
+ expect(await imageManager.get(null)).toEqual(ImageManager.defaultImage)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('no id, default image provided', async () => {
|
|
|
+ const defaultImage = 'a_picture.jpg'
|
|
|
+
|
|
|
+ expect(await imageManager.get(null, defaultImage)).toEqual(defaultImage)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('no response, no default image', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.get = vi.fn((url: string) => '')
|
|
|
+
|
|
|
+ imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
+ // @ts-ignore
|
|
|
+ imageManager.toBase64 = vi.fn()
|
|
|
+
|
|
|
+ console.error = vi.fn()
|
|
|
+
|
|
|
+ const result = await imageManager.get(1)
|
|
|
+
|
|
|
+ expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', ['123456'])
|
|
|
+ expect(console.error).toHaveBeenCalledWith('Error: image 1 not found or invalid')
|
|
|
+ expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
|
|
|
+
|
|
|
+ expect(result).toEqual(ImageManager.defaultImage)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('no response, default image', async () => {
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.get = vi.fn((url: string) => '')
|
|
|
+
|
|
|
+ imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ imageManager.toBase64 = vi.fn()
|
|
|
+
|
|
|
+ console.error = vi.fn()
|
|
|
+
|
|
|
+ const result = await imageManager.get(1, 'some_default.jpg')
|
|
|
+
|
|
|
+ expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', ['123456'])
|
|
|
+ expect(console.error).toHaveBeenCalledWith('Error: image 1 not found or invalid')
|
|
|
+ expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
|
|
|
+
|
|
|
+ expect(result).toEqual('some_default.jpg')
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+describe('toBase64', () => {
|
|
|
+ test('simple call', async () => {
|
|
|
+ expect(await imageManager.toBase64('some_data')).toEqual('data:image/jpeg;base64,c29tZV9kYXRh')
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+describe('getCacheKey', () => {
|
|
|
+ test('simple call', () => {
|
|
|
+ expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
|
|
|
+ })
|
|
|
+})
|