Browse Source

minor fixes and fix unit tests

Olivier Massot 1 năm trước cách đây
mục cha
commit
f88c5a6c75

+ 1 - 0
nuxt.config.ts

@@ -215,4 +215,5 @@ export default defineNuxtConfig({
   build: {
     transpile,
   },
+  ignore: [process.env.NUXT_ENV === 'prod' ? 'pages/dev/*' : ''],
 })

+ 0 - 0
pages/tests/poc_fetch_collection.vue → pages/dev/poc_fetch_collection.vue


+ 3 - 0
pages/dev/readme.md

@@ -0,0 +1,3 @@
+# Pages de test
+
+Ces pages sont exclues de la build de l'application en production (cf. nuxt.config.ts => ignore section).

+ 1 - 0
services/data/entityManager.ts

@@ -10,6 +10,7 @@ import type { AnyJson, AssociativeArray, Collection } from '~/types/data.d'
 import models from '~/models/models'
 import HydraNormalizer from '~/services/data/normalizer/hydraNormalizer'
 import Query from '~/services/data/Query'
+import { computed } from '@vue/reactivity'
 
 /**
  * Entity manager: make operations on the models defined with the Pinia-Orm library

+ 41 - 36
tests/units/services/data/entityManager.test.ts

@@ -324,12 +324,14 @@ describe('fetchCollection', () => {
       },
     )
 
+    const piniaOrmQuery = vi.fn()
+
+    // @ts-ignore
+    entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
+
     const result = await entityManager.fetchCollection(DummyApiResource, null)
 
-    expect(apiRequestService.get).toHaveBeenCalledWith(
-      'api/dummyResource',
-      null,
-    )
+    expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource')
     expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
     expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
       id: 1,
@@ -347,13 +349,20 @@ describe('fetchCollection', () => {
       _model: undefined,
     })
 
-    expect(result.items).toEqual([
+    // @ts-ignore
+    piniaOrmQuery.get = vi.fn(() => [
       new DummyApiResource({ id: 1 }),
       new DummyApiResource({ id: 2 }),
       new DummyApiResource({ id: 3 }),
     ])
 
-    expect(result.pagination, 'default pagination').toEqual({
+    expect(result.value.items).toEqual([
+      new DummyApiResource({ id: 1 }),
+      new DummyApiResource({ id: 2 }),
+      new DummyApiResource({ id: 3 }),
+    ])
+
+    expect(result.value.pagination, 'default pagination').toEqual({
       first: 1,
       last: 1,
       next: undefined,
@@ -371,6 +380,11 @@ describe('fetchCollection', () => {
     // @ts-ignore
     apiRequestService.get = vi.fn(async (url: string) => collection)
 
+    const piniaOrmQuery = vi.fn()
+
+    // @ts-ignore
+    entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
+
     // @ts-ignore
     entityManager.newInstance = vi.fn(
       (model: typeof ApiResource, props: object) => {
@@ -386,7 +400,6 @@ describe('fetchCollection', () => {
 
     expect(apiRequestService.get).toHaveBeenCalledWith(
       'api/dummyModel/100/dummyResource',
-      null,
     )
   })
 
@@ -397,6 +410,19 @@ describe('fetchCollection', () => {
       'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
     }
 
+    const query = vi.fn()
+
+    // @ts-ignore
+    query.getUrlQuery = vi.fn(() => 'foo=bar')
+
+    // @ts-ignore
+    query.applyToPiniaOrmQuery = vi.fn((q) => q)
+
+    const piniaOrmQuery = vi.fn()
+
+    // @ts-ignore
+    entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
+
     // @ts-ignore
     apiRequestService.get = vi.fn(async (url: string) => collection)
 
@@ -407,37 +433,16 @@ describe('fetchCollection', () => {
       },
     )
 
-    await entityManager.fetchCollection(DummyApiResource, null, { page: 10 })
-
-    expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', {
-      page: 10,
-    })
-  })
-
-  test('with pagination', async () => {
-    const collection = {
-      '@type': 'hydra:Collection',
-      'hydra:totalItems': 1000,
-      'hydra:member': [],
-      'hydra:view': {
-        '@id': '/api/subdomains?organization=498&page=50',
-        'hydra:first': '/api/subdomains?organization=498&page=1',
-        'hydra:last': '/api/subdomains?organization=498&page=100',
-        'hydra:next': '/api/subdomains?organization=498&page=51',
-        'hydra:previous': '/api/subdomains?organization=498&page=49',
-      },
-    }
-
     // @ts-ignore
-    apiRequestService.get = vi.fn(async (url: string) => collection)
-
-    const result = await entityManager.fetchCollection(DummyApiResource, null)
+    await entityManager.fetchCollection(DummyApiResource, null, query)
 
-    expect(result.totalItems).toEqual(1000)
-    expect(result.pagination.first).toEqual(1)
-    expect(result.pagination.last).toEqual(100)
-    expect(result.pagination.previous).toEqual(49)
-    expect(result.pagination.next).toEqual(51)
+    expect(apiRequestService.get).toHaveBeenCalledWith(
+      'api/dummyResource?foo=bar',
+    )
+    // @ts-ignore
+    expect(query.getUrlQuery).toHaveBeenCalledWith()
+    // @ts-ignore
+    expect(query.applyToPiniaOrmQuery).toHaveBeenCalledWith(piniaOrmQuery)
   })
 })