瀏覽代碼

minor fixes and complete entityManager.test.ts

Olivier Massot 2 年之前
父節點
當前提交
3cfa5aa32e

+ 2 - 1
composables/data/useAp2iRequestService.ts

@@ -98,9 +98,10 @@ export const useAp2iRequestService = () => {
     }
 
     //Avoid memory leak
-    if(apiRequestServiceClass === null){
+    if (apiRequestServiceClass === null) {
         // Utilise la fonction `create` d'ohmyfetch pour générer un fetcher dédié à l'interrogation de Ap2i
         const fetcher = $fetch.create(config)
+        // @ts-ignore
         apiRequestServiceClass = new ApiRequestService(fetcher)
     }
 

+ 7 - 3
composables/data/useEntityManager.ts

@@ -1,13 +1,17 @@
 import EntityManager from "~/services/data/entityManager";
 import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";
+import ApiResource from "~/models/ApiResource";
+import {useRepo} from "pinia-orm";
 
 let entityManager:EntityManager|null = null
 
 export const useEntityManager = () => {
-    const { apiRequestService, pending } = useAp2iRequestService()
     //Avoid memory leak
     if(entityManager === null){
-        entityManager = new EntityManager(apiRequestService)
+        const { apiRequestService } = useAp2iRequestService()
+        const getRepo = (model: typeof ApiResource) => useRepo(model)
+
+        entityManager = new EntityManager(apiRequestService, getRepo)
     }
-    return { em: entityManager, pending: pending }
+    return { em: entityManager }
 }

+ 5 - 4
composables/data/useEnumManager.ts

@@ -2,13 +2,14 @@ import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";
 import EnumManager from "~/services/data/enumManager";
 import {useI18n} from "vue-i18n";
 
-let enumManager:EnumManager|null = null
+let enumManager:EnumManager | null = null
+
 export const useEnumManager = () => {
-    const { apiRequestService, pending } = useAp2iRequestService()
     //Avoid memory leak
-    if(enumManager === null){
+    if (enumManager === null) {
+        const { apiRequestService } = useAp2iRequestService()
         const i18n = useI18n() as any
         enumManager = new EnumManager(apiRequestService, i18n)
     }
-    return { enumManager: enumManager, pending: pending }
+    return { enumManager: enumManager }
 }

+ 4 - 2
composables/data/useImageManager.ts

@@ -1,12 +1,14 @@
 import {useAp2iRequestService} from "~/composables/data/useAp2iRequestService";
 import ImageManager from "~/services/data/imageManager";
 
-let imageManager:ImageManager|null = null
+let imageManager:ImageManager | null = null
+
 export const useImageManager = () => {
     //Avoid memory leak
-    if(imageManager === null){
+    if (imageManager === null) {
         const { apiRequestService } = useAp2iRequestService()
         imageManager = new ImageManager(apiRequestService)
     }
+
     return { imageManager: imageManager }
 }

+ 15 - 4
services/data/entityManager.ts

@@ -1,5 +1,5 @@
 import ApiRequestService from "./apiRequestService"
-import {Repository, useRepo} from "pinia-orm"
+import {Repository} from "pinia-orm"
 import UrlUtils from "~/services/utils/urlUtils"
 import HydraDenormalizer from "./normalizer/hydraDenormalizer"
 import ApiModel from "~/models/ApiModel"
@@ -19,12 +19,24 @@ import _ from "lodash"
 class EntityManager {
     protected CLONE_PREFIX = '_clone_'
 
+    /**
+     * In instance of ApiRequestService
+     * @protected
+     */
     protected apiRequestService: ApiRequestService
 
+    /**
+     * A method to retrieve the repository of a given ApiResource
+     * @protected
+     */
+    protected _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
+
     public constructor(
-        apiRequestService: ApiRequestService
+        apiRequestService: ApiRequestService,
+        getRepo: (model: typeof ApiResource) => Repository<ApiResource>
     ) {
         this.apiRequestService = apiRequestService
+        this._getRepo = getRepo
     }
 
     /**
@@ -33,8 +45,7 @@ class EntityManager {
      * @param model
      */
     public getRepository(model: typeof ApiResource): Repository<ApiResource> {
-        // TODO: voir si possible de passer par une injection de dépendance plutôt que par un use
-        return useRepo(model)
+        return this._getRepo(model)
     }
 
     /**

+ 2 - 2
services/utils/urlUtils.ts

@@ -66,9 +66,9 @@ class UrlUtils {
    */
   public static extractIdFromUri (uri: string, isLiteral: boolean = false): number|string|null {
     const partUri: Array<string> = uri.split('/')
-    const id: any = partUri.pop()
+    const id: string = partUri.pop() ?? ''
 
-    if (!id || (!isLiteral && isNaN(id))) {
+    if (!id || (!isLiteral && isNaN(id as any))) {
       throw new Error('no id found')
     }
     return isLiteral ? id : parseInt(id)

+ 29 - 8
tests/units/services/data/entityManager.test.ts

@@ -1,9 +1,10 @@
-import { describe, test, expect } from 'vitest'
+import { describe, test, it, expect } from 'vitest'
 import EntityManager from "~/services/data/entityManager";
 import ApiResource from "~/models/ApiResource";
 import ApiModel from "~/models/ApiModel";
 import ApiRequestService from "~/services/data/apiRequestService";
 import {Element, Repository} from "pinia-orm";
+import models from "~/models/models";
 
 
 
@@ -22,18 +23,32 @@ let _console: any = {
     'error': console.error,
 }
 
+vi.mock("~/models/models", async () => {
+    class MyModel {
+        static entity = 'myModel'
+    }
+
+    const models: Record<string, any> = {'myModel': MyModel}
+
+    return {
+        default: models
+    }
+})
+
 let apiRequestService: ApiRequestService
 let entityManager: EntityManager
 let repo: Repository<ApiResource>
+let _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
 
 beforeEach(() => {
     // @ts-ignore
-    apiRequestService = vi.fn() as ApiRequestService
-
-    entityManager = new EntityManager(apiRequestService)
+    repo = vi.fn() as Repository<ApiResource>
 
     // @ts-ignore
-    repo = vi.fn() as Repository<ApiResource>
+    apiRequestService = vi.fn() as ApiRequestService
+    _getRepo = vi.fn((model: typeof ApiResource) => repo)
+
+    entityManager = new EntityManager(apiRequestService, _getRepo)
 })
 
 afterEach(() => {
@@ -44,7 +59,11 @@ afterEach(() => {
 })
 
 describe('getRepository', () => {
-    // TODO: à revoir
+  test('simple call', () => {
+      entityManager.getRepository(DummyApiResource)
+
+      expect(_getRepo).toHaveBeenCalledWith(DummyApiResource)
+  })
 })
 
 describe('cast', () => {
@@ -57,7 +76,9 @@ describe('cast', () => {
 })
 
 describe('getModelFor', () => {
-    // TODO: à revoir
+    test('simple call', () => {
+        expect(entityManager.getModelFor('myModel').entity).toEqual('myModel')
+    })
 })
 
 describe('getModelFromIri', () => {
@@ -70,7 +91,7 @@ describe('getModelFromIri', () => {
 
         expect(result).toEqual(DummyApiResource)
     })
-    test('invalid Iri', () => {
+    test('invalide Iri', () => {
         expect(() => entityManager.getModelFromIri('/invalid')).toThrowError('cannot parse the IRI')
     })
 })