Bladeren bron

fix unit tests

Olivier Massot 1 jaar geleden
bovenliggende
commit
9bc5e91579
1 gewijzigde bestanden met toevoegingen van 40 en 10 verwijderingen
  1. 40 10
      tests/units/services/data/entityManager.test.ts

+ 40 - 10
tests/units/services/data/entityManager.test.ts

@@ -27,6 +27,12 @@ class DummyApiModel extends ApiModel {
   declare name: string
   declare name: string
 }
 }
 
 
+class TestableEntityManager extends EntityManager {
+  public makeProfileHash() {
+    return super.makeProfileHash()
+  }
+}
+
 const _console: any = {
 const _console: any = {
   log: console.log,
   log: console.log,
   warn: console.warn,
   warn: console.warn,
@@ -46,9 +52,10 @@ vi.mock('~/models/models', async () => {
 })
 })
 
 
 let apiRequestService: ApiRequestService
 let apiRequestService: ApiRequestService
-let entityManager: EntityManager
+let entityManager: TestableEntityManager
 let repo: Repository<ApiResource>
 let repo: Repository<ApiResource>
 let _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
 let _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
+let _getProfileMask: () => object
 
 
 beforeEach(() => {
 beforeEach(() => {
   // @ts-ignore
   // @ts-ignore
@@ -57,8 +64,15 @@ beforeEach(() => {
   // @ts-ignore
   // @ts-ignore
   apiRequestService = vi.fn() as ApiRequestService
   apiRequestService = vi.fn() as ApiRequestService
   _getRepo = vi.fn((model: typeof ApiResource) => repo)
   _getRepo = vi.fn((model: typeof ApiResource) => repo)
+  _getProfileMask = vi.fn(() => {
+    return {}
+  })
 
 
-  entityManager = new EntityManager(apiRequestService, _getRepo)
+  entityManager = new TestableEntityManager(
+    apiRequestService,
+    _getRepo,
+    _getProfileMask,
+  )
 })
 })
 
 
 afterEach(() => {
 afterEach(() => {
@@ -473,13 +487,19 @@ describe('persist', () => {
 
 
     // @ts-ignore
     // @ts-ignore
     entityManager.removeTempAfterPersist = vi.fn()
     entityManager.removeTempAfterPersist = vi.fn()
+    entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
 
 
     const result = await entityManager.persist(DummyApiModel, instance)
     const result = await entityManager.persist(DummyApiModel, instance)
 
 
     // temp id should have been purged from the posted data
     // temp id should have been purged from the posted data
-    expect(apiRequestService.post).toHaveBeenCalledWith('api/dummyModel', {
-      name: 'bob',
-    })
+    expect(apiRequestService.post).toHaveBeenCalledWith(
+      'api/dummyModel',
+      {
+        name: 'bob',
+      },
+      null,
+      { profileHash: 'azerty' },
+    )
     expect(entityManager.newInstance).toHaveBeenCalledWith(
     expect(entityManager.newInstance).toHaveBeenCalledWith(
       DummyApiModel,
       DummyApiModel,
       response,
       response,
@@ -487,8 +507,10 @@ describe('persist', () => {
     // @ts-ignore
     // @ts-ignore
     expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(
     expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(
       DummyApiModel,
       DummyApiModel,
-      instance.id,
+      instance.id
     )
     )
+    // @ts-ignore
+    expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
 
 
     expect(result.id).toEqual(1)
     expect(result.id).toEqual(1)
     expect(result.name).toEqual('bob')
     expect(result.name).toEqual('bob')
@@ -522,16 +544,24 @@ describe('persist', () => {
 
 
     // @ts-ignore
     // @ts-ignore
     entityManager.removeTempAfterPersist = vi.fn()
     entityManager.removeTempAfterPersist = vi.fn()
+    entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
 
 
     const result = await entityManager.persist(DummyApiModel, entity)
     const result = await entityManager.persist(DummyApiModel, entity)
 
 
-    expect(apiRequestService.put).toHaveBeenCalledWith('api/dummyModel/1', {
-      id: 1,
-      name: 'bob',
-    })
+    expect(apiRequestService.put).toHaveBeenCalledWith(
+      'api/dummyModel/1',
+      {
+        id: 1,
+        name: 'bob',
+      },
+      null,
+      { profileHash: 'azerty' },
+    )
     expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, props)
     expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, props)
     // @ts-ignore
     // @ts-ignore
     expect(entityManager.removeTempAfterPersist).toHaveBeenCalledTimes(0)
     expect(entityManager.removeTempAfterPersist).toHaveBeenCalledTimes(0)
+    // @ts-ignore
+    expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
 
 
     expect(result.id).toEqual(1)
     expect(result.id).toEqual(1)
     expect(result.name).toEqual('bob')
     expect(result.name).toEqual('bob')