|
|
@@ -2,19 +2,10 @@ import { describe, test, vi, expect, beforeEach, afterEach } from 'vitest'
|
|
|
import { Repository } from 'pinia-orm'
|
|
|
import type { Element } from 'pinia-orm'
|
|
|
import { Str, Uid } from 'pinia-orm/dist/decorators'
|
|
|
+import EntityManager from '~/services/data/entityManager'
|
|
|
import ApiResource from '~/models/ApiResource'
|
|
|
import ApiModel from '~/models/ApiModel'
|
|
|
import ApiRequestService from '~/services/data/apiRequestService'
|
|
|
-import EntityManager from '~/services/data/entityManager'
|
|
|
-
|
|
|
-class TestableEntityManager extends EntityManager {
|
|
|
- public removeTempAfterPersist(
|
|
|
- model: typeof ApiResource,
|
|
|
- tempInstanceId: number | string,
|
|
|
- ): void {
|
|
|
- return super.removeTempAfterPersist(model, tempInstanceId)
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
class DummyApiResource extends ApiResource {
|
|
|
static entity = 'dummyResource'
|
|
|
@@ -36,6 +27,21 @@ class DummyApiModel extends ApiModel {
|
|
|
declare name: string
|
|
|
}
|
|
|
|
|
|
+class TestableEntityManager extends EntityManager {
|
|
|
+ public _getProfileMask: () => object
|
|
|
+
|
|
|
+ public removeTempAfterPersist(
|
|
|
+ model: typeof ApiResource,
|
|
|
+ tempInstanceId: number | string,
|
|
|
+ ) {
|
|
|
+ return super.removeTempAfterPersist(model, tempInstanceId)
|
|
|
+ }
|
|
|
+
|
|
|
+ public makeProfileHash() {
|
|
|
+ return super.makeProfileHash()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
const _console: any = {
|
|
|
log: console.log,
|
|
|
warn: console.warn,
|
|
|
@@ -58,6 +64,7 @@ let apiRequestService: ApiRequestService
|
|
|
let entityManager: TestableEntityManager
|
|
|
let repo: Repository<ApiResource>
|
|
|
let _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
|
|
|
+let _getProfileMask: () => object
|
|
|
|
|
|
beforeEach(() => {
|
|
|
// @ts-ignore
|
|
|
@@ -66,8 +73,15 @@ beforeEach(() => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService = vi.fn() as ApiRequestService
|
|
|
_getRepo = vi.fn((model: typeof ApiResource) => repo)
|
|
|
+ _getProfileMask = vi.fn(() => {
|
|
|
+ return {}
|
|
|
+ })
|
|
|
|
|
|
- entityManager = new TestableEntityManager(apiRequestService, _getRepo)
|
|
|
+ entityManager = new TestableEntityManager(
|
|
|
+ apiRequestService,
|
|
|
+ _getRepo,
|
|
|
+ _getProfileMask,
|
|
|
+ )
|
|
|
})
|
|
|
|
|
|
afterEach(() => {
|
|
|
@@ -512,13 +526,19 @@ describe('persist', () => {
|
|
|
|
|
|
// @ts-ignore
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
+ entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
|
|
|
|
const result = await entityManager.persist(DummyApiModel, instance)
|
|
|
|
|
|
// 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(
|
|
|
DummyApiModel,
|
|
|
response,
|
|
|
@@ -528,6 +548,8 @@ describe('persist', () => {
|
|
|
DummyApiModel,
|
|
|
instance.id,
|
|
|
)
|
|
|
+ // @ts-ignore
|
|
|
+ expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
|
@@ -561,16 +583,24 @@ describe('persist', () => {
|
|
|
|
|
|
// @ts-ignore
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
+ entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
|
|
|
|
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)
|
|
|
// @ts-ignore
|
|
|
expect(entityManager.removeTempAfterPersist).toHaveBeenCalledTimes(0)
|
|
|
+ // @ts-ignore
|
|
|
+ expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
|
@@ -958,3 +988,15 @@ describe('removeTempAfterPersist', () => {
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+describe('makeProfileHash', () => {
|
|
|
+ test('simple call', async () => {
|
|
|
+ entityManager._getProfileMask = vi.fn(() => {
|
|
|
+ return { a: 1 }
|
|
|
+ })
|
|
|
+
|
|
|
+ expect(await entityManager.makeProfileHash()).toEqual(
|
|
|
+ '9f89c740ceb46d7418c924a78ac57941d5e96520',
|
|
|
+ )
|
|
|
+ })
|
|
|
+})
|