Browse Source

fix unit tests

Olivier Massot 1 year ago
parent
commit
388aa550a1

+ 6 - 3
services/data/imageManager.ts

@@ -50,11 +50,14 @@ class ImageManager {
     const query = [this.getCacheKey()]
 
     const response: Response = await this.apiRequestService.get(imageUrl, query)
+    if (!response) {
+      console.error('Error: image ' + id + ' not found')
+      return defaultUrl
+    }
 
     const blobPart = await response.blob()
-
-    if (!response || blobPart.size === 0) {
-      console.error('Error: image ' + id + ' not found or invalid')
+    if (blobPart.size === 0) {
+      console.error('Error: image ' + id + ' is invalid')
       return defaultUrl
     }
 

+ 1 - 1
services/utils/urlUtils.ts

@@ -80,7 +80,7 @@ class UrlUtils {
     const partUri: Array<string> = uri.split('/')
     const id: string = partUri.pop() ?? ''
 
-    if (!id || (!isLiteral && /\d+/.test(id))) {
+    if (!id || (!isLiteral && !/\d+/.test(id))) {
       throw new Error('no id found')
     }
     return isLiteral ? id : parseInt(id)

+ 25 - 8
tests/units/services/data/imageManager.test.ts

@@ -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)