|
|
@@ -1,4 +1,4 @@
|
|
|
-import { describe, test, it, expect } from 'vitest'
|
|
|
+import { vi, describe, test, it, expect } from 'vitest'
|
|
|
import ApiRequestService from '~/services/data/apiRequestService'
|
|
|
import ImageManager from '~/services/data/imageManager'
|
|
|
import 'blob-polyfill'
|
|
|
@@ -25,16 +25,20 @@ afterEach(() => {
|
|
|
|
|
|
describe('get', () => {
|
|
|
test('simple call', async () => {
|
|
|
- const data = 'azerty'
|
|
|
+ const blobPart = vi.fn()
|
|
|
+ const response = vi.fn()
|
|
|
|
|
|
// @ts-ignore
|
|
|
- apiRequestService.get = vi.fn((url: string) => data)
|
|
|
+ response.blob = () => blobPart
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ apiRequestService.get = vi.fn((url: string) => response)
|
|
|
|
|
|
// @ts-ignore
|
|
|
imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
|
|
|
// @ts-ignore
|
|
|
- imageManager.toBase64 = vi.fn((data: string) => 'base64:' + data)
|
|
|
+ imageManager.toBase64 = vi.fn((_: string) => 'base64:azerty')
|
|
|
|
|
|
const result = await imageManager.get(1)
|
|
|
|
|
|
@@ -42,6 +46,9 @@ describe('get', () => {
|
|
|
'123456',
|
|
|
])
|
|
|
|
|
|
+ // @ts-ignore
|
|
|
+ expect(imageManager.toBase64).toHaveBeenCalledWith(blobPart)
|
|
|
+
|
|
|
expect(result).toEqual('base64:azerty')
|
|
|
})
|
|
|
|
|
|
@@ -56,8 +63,10 @@ describe('get', () => {
|
|
|
})
|
|
|
|
|
|
test('no response, no default image', async () => {
|
|
|
+ const blobPart = vi.fn()
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
- apiRequestService.get = vi.fn((url: string) => '')
|
|
|
+ apiRequestService.get = vi.fn((url: string) => null)
|
|
|
|
|
|
// @ts-ignore
|
|
|
imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
@@ -72,7 +81,7 @@ describe('get', () => {
|
|
|
'123456',
|
|
|
])
|
|
|
expect(console.error).toHaveBeenCalledWith(
|
|
|
- 'Error: image 1 not found or invalid',
|
|
|
+ 'Error: image 1 not found',
|
|
|
)
|
|
|
// @ts-ignore
|
|
|
expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
|
|
|
@@ -81,8 +90,16 @@ describe('get', () => {
|
|
|
})
|
|
|
|
|
|
test('no response, default image', async () => {
|
|
|
+ const blobPart = vi.fn()
|
|
|
+ blobPart.size = 0
|
|
|
+
|
|
|
+ const response = vi.fn()
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ response.blob = () => blobPart
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
- apiRequestService.get = vi.fn((url: string) => '')
|
|
|
+ apiRequestService.get = vi.fn((url: string) => response)
|
|
|
|
|
|
// @ts-ignore
|
|
|
imageManager.getCacheKey = vi.fn(() => '123456')
|
|
|
@@ -98,7 +115,7 @@ describe('get', () => {
|
|
|
'123456',
|
|
|
])
|
|
|
expect(console.error).toHaveBeenCalledWith(
|
|
|
- 'Error: image 1 not found or invalid',
|
|
|
+ 'Error: image 1 is invalid',
|
|
|
)
|
|
|
// @ts-ignore
|
|
|
expect(imageManager.toBase64).toHaveBeenCalledTimes(0)
|