Browse Source

fix unit tests

Olivier Massot 7 months ago
parent
commit
478b005ebd

+ 1 - 0
services/data/imageManager.ts

@@ -55,6 +55,7 @@ class ImageManager {
       return defaultUrl;
     }
 
+    console.log(blobPart, blobPart instanceof Blob, blobPart.size)
     if (!(blobPart instanceof Blob) || blobPart.size === 0) {
       console.error('Error: image ' + id + ' is invalid');
       return defaultUrl;

+ 40 - 0
tests/units/services/data/apiRequestService.test.ts

@@ -202,6 +202,10 @@ describe('request', () => {
     // @ts-ignore
     expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
       method: 'GET',
+           "headers": {
+             "Accept": "application/ld+json",
+             "Content-Type": "application/ld+json",
+           },
     })
   })
 
@@ -218,6 +222,10 @@ describe('request', () => {
     expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
       method: 'POST',
       body: 'a_body',
+      "headers": {
+        "Accept": "application/ld+json",
+        "Content-Type": "application/ld+json",
+      },
     })
   })
 
@@ -234,6 +242,30 @@ describe('request', () => {
     expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
       method: 'PUT',
       body: 'a_body',
+      "headers": {
+        "Accept": "application/ld+json",
+        "Content-Type": "application/ld+json",
+      },
+    })
+  })
+
+  test('patch with body', async () => {
+    // @ts-ignore
+    const result = await apiRequestService.request(
+      HTTP_METHOD.PATCH,
+      'https://myapi.com/api/item',
+      'a_body',
+    )
+
+    expect(result).toEqual('fetch_response')
+    // @ts-ignore
+    expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
+      method: 'PATCH',
+      body: 'a_body',
+      "headers": {
+        "Accept": "application/ld+json",
+        "Content-Type": "application/merge-patch+json",
+      },
     })
   })
 
@@ -249,6 +281,10 @@ describe('request', () => {
     // @ts-ignore
     expect(fetcher).toHaveBeenCalledWith('https://myapi.com/api/item', {
       method: 'GET',
+      "headers": {
+        "Accept": "application/ld+json",
+        "Content-Type": "application/ld+json",
+      },
     })
   })
 
@@ -267,6 +303,10 @@ describe('request', () => {
       method: 'PUT',
       body: 'a_body',
       query: { a: 1 },
+      "headers": {
+        "Accept": "application/ld+json",
+        "Content-Type": "application/ld+json",
+      },
     })
   })
 })

+ 12 - 12
tests/units/services/data/entityManager.test.ts

@@ -6,6 +6,7 @@ import EntityManager from '~/services/data/entityManager'
 import ApiResource from '~/models/ApiResource'
 import ApiModel from '~/models/ApiModel'
 import ApiRequestService from '~/services/data/apiRequestService'
+import { IdField } from '~/models/decorators'
 
 class DummyApiResource extends ApiResource {
   static entity = 'dummyResource'
@@ -367,9 +368,9 @@ describe('fetch', () => {
 describe('fetchCollection', () => {
   test('simple call', async () => {
     const collection = {
-      '@type': 'hydra:Collection',
-      'hydra:totalItems': 3,
-      'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
+      '@type': 'Collection',
+      'totalItems': 3,
+      'member': [{ id: 1 }, { id: 2 }, { id: 3 }],
     }
 
     // @ts-ignore
@@ -436,9 +437,9 @@ describe('fetchCollection', () => {
 
   test('with a parent', async () => {
     const collection = {
-      '@type': 'hydra:Collection',
-      'hydra:totalItems': 3,
-      'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
+      '@type': 'Collection',
+      'totalItems': 3,
+      'member': [{ id: 1 }, { id: 2 }, { id: 3 }],
     }
 
     // @ts-ignore
@@ -469,9 +470,9 @@ describe('fetchCollection', () => {
 
   test('with a query', async () => {
     const collection = {
-      '@type': 'hydra:Collection',
-      'hydra:totalItems': 3,
-      'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
+      '@type': 'Collection',
+      'totalItems': 3,
+      'member': [{ id: 1 }, { id: 2 }, { id: 3 }],
     }
 
     const query = vi.fn()
@@ -588,7 +589,7 @@ describe('persist', () => {
     // entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
 
     // @ts-ignore
-    apiRequestService.put = vi.fn((url, data) => props)
+    apiRequestService.patch = vi.fn((url, data) => props)
 
     // @ts-ignore
     entityManager.newInstance = vi.fn((model, response) => {
@@ -609,7 +610,7 @@ describe('persist', () => {
 
     const result = await entityManager.persist(entity)
 
-    expect(apiRequestService.put).toHaveBeenCalledWith(
+    expect(apiRequestService.patch).toHaveBeenCalledWith(
       'api/dummyModel/1',
       {
         id: 1,
@@ -670,7 +671,6 @@ describe('patch', () => {
 describe('delete', () => {
   test('delete non persisted entity', () => {
     const entity = new DummyApiModel()
-    entity.isNew = vi.fn(() => true)
     entity.id = 'tmp123'
 
     // @ts-ignore

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

@@ -25,14 +25,13 @@ afterEach(() => {
 
 describe('get', () => {
   test('simple call', async () => {
-    const blobPart = vi.fn()
-    const response = vi.fn()
 
-    // @ts-ignore
-    response.blob = () => blobPart
+    const blobPart = new Blob(['some_data'])
 
     // @ts-ignore
-    apiRequestService.get = vi.fn((url: string) => response)
+    apiRequestService.get = vi.fn(
+      (url: string, query: object) => blobPart
+    )
 
     // @ts-ignore
     imageManager.getCacheKey = vi.fn(() => '123456')
@@ -42,7 +41,7 @@ describe('get', () => {
 
     const result = await imageManager.get(1)
 
-    expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
+    expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
       '123456',
     ])
 
@@ -77,7 +76,7 @@ describe('get', () => {
 
     const result = await imageManager.get(1)
 
-    expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
+    expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
       '123456',
     ])
     expect(console.error).toHaveBeenCalledWith('Error: image 1 not found')
@@ -111,7 +110,7 @@ describe('get', () => {
 
     const result = await imageManager.get(1, 'some_default.jpg')
 
-    expect(apiRequestService.get).toHaveBeenCalledWith('api/download/1', [
+    expect(apiRequestService.get).toHaveBeenCalledWith('api/file/download/1', [
       '123456',
     ])
     expect(console.error).toHaveBeenCalledWith('Error: image 1 is invalid')

+ 2 - 2
tests/units/services/data/normalizer/hydraNormalizer.test.ts

@@ -126,8 +126,8 @@ describe('getData', () => {
     const data = {
       '@context': '/api/contexts/Foo',
       '@id': '/api/foo',
-      '@type': 'hydra:Collection',
-      'hydra:member': ['foo'],
+      '@type': 'Collection',
+      'member': ['foo'],
     }
 
     // @ts-ignore

+ 6 - 4
tests/units/services/layout/menuBuilder/configurationMenuBuilder.test.ts

@@ -23,6 +23,9 @@ beforeEach(() => {
   router = vi.fn() as Router
 
   runtimeConfig.baseUrlAdminLegacy = 'https://mydomain.com/'
+  router.resolve = vi.fn(() => {
+    return 'some_route'
+  })
 
   menuBuilder = new ConfigurationMenuBuilder(
     runtimeConfig,
@@ -107,11 +110,10 @@ describe('build', () => {
     menuBuilder.organizationProfile.id = 123
 
     expect(menuBuilder.build()).toEqual({
-      label: 'parameters',
+      label: 'parameters_page',
       icon: undefined,
-      to: 'https://mydomain.com/#/main/edit/parameters/123',
-      target: '_self',
-      type: MENU_LINK_TYPE.V1,
+      to: undefined,
+      type: MENU_LINK_TYPE.INTERNAL,
       active: false,
     })