| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
- import type ApiRequestService from '~/services/data/apiRequestService'
- import ImageManager from '~/services/data/imageManager'
- import 'blob-polyfill'
- import { IMAGE_SIZE } from '~/types/enum/enums'
- let apiRequestService: ApiRequestService
- let imageManager: ImageManager
- let initConsoleError: any
- beforeEach(() => {
- // @ts-ignore
- apiRequestService = vi.fn() as ApiRequestService
- imageManager = new ImageManager(apiRequestService)
- // Important : Restore console error after mocking
- initConsoleError = console.error
- })
- afterEach(() => {
- vi.restoreAllMocks()
- console.error = initConsoleError
- })
- describe('get', () => {
- test('simple call', async () => {
- const blobPart = new Blob(['some_data'])
- // @ts-ignore
- apiRequestService.get = vi.fn((url: string, query: object) => blobPart)
- // @ts-ignore
- imageManager.getCacheKey = vi.fn(() => '123456')
- // @ts-ignore
- blobPart.toString = vi.fn(() => 'image_url')
- const result = await imageManager.get(1)
- expect(apiRequestService.get).toHaveBeenCalledWith(
- 'api/image/download/1/md',
- { 0: '123456' },
- )
- expect(result).toEqual('/image_url?0=123456')
- })
- 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, IMAGE_SIZE.MD, defaultImage)).toEqual(
- defaultImage,
- )
- })
- test('no response, no default image', async () => {
- // @ts-ignore
- imageManager.getProcessed = vi.fn(() => {
- throw new Error('Error: image 1 not found')
- })
- console.error = vi.fn()
- const result = await imageManager.get(1)
- expect(console.error).toHaveBeenCalled()
- expect(result).toEqual(ImageManager.defaultImage)
- })
- test('no response, default image', async () => {
- // @ts-ignore
- imageManager.getProcessed = vi.fn(() => {
- throw new Error('Error: image 1 not found')
- })
- console.error = vi.fn()
- const defaultImage = 'some_default.jpg'
- const result = await imageManager.get(1, IMAGE_SIZE.MD, defaultImage)
- expect(console.error).toHaveBeenCalled()
- expect(result).toEqual(defaultImage)
- })
- })
- describe('toBase64', () => {
- test('simple call', async () => {
- // @ts-ignore
- expect(await imageManager.toBase64('some_data')).toEqual(
- 'data:image/jpeg;base64,c29tZV9kYXRh',
- )
- })
- })
- describe('getCacheKey', () => {
- test('simple call', () => {
- // @ts-ignore
- expect(imageManager.getCacheKey()).toMatch(/\d{10,}/)
- })
- })
|