|
|
@@ -5,14 +5,7 @@ import ApiModel from "~/models/ApiModel";
|
|
|
import ApiRequestService from "~/services/data/apiRequestService";
|
|
|
import {Element, Repository} from "pinia-orm";
|
|
|
|
|
|
-// TODO: vérifier si les classes Testable sont bien nécessaires, sinon les supprimer
|
|
|
-class TestableEntityManager extends EntityManager {
|
|
|
- public cast(model: typeof ApiResource, entity: ApiResource): ApiResource { return super.cast(model, entity) }
|
|
|
- public async saveResponseAsEntity(model: typeof ApiModel, response: Response) { return super.saveResponseAsEntity(model, response) }
|
|
|
- public saveInitialState(model: typeof ApiResource, entity: ApiResource) { return super.saveInitialState(model, entity) }
|
|
|
- public getInitialStateOf(model: typeof ApiResource, id: string | number): ApiResource | null { return super.getInitialStateOf(model, id) }
|
|
|
- public removeTempAfterPersist(model: typeof ApiResource, tempEntityId: number | string) { return super.removeTempAfterPersist(model, tempEntityId) }
|
|
|
-}
|
|
|
+
|
|
|
|
|
|
class DummyApiResource extends ApiResource {
|
|
|
static entity = 'dummyResource'
|
|
|
@@ -23,16 +16,31 @@ class DummyApiModel extends ApiModel {
|
|
|
static entity = 'dummyModel'
|
|
|
}
|
|
|
|
|
|
+let _console: any = {
|
|
|
+ 'log': console.log,
|
|
|
+ 'warn': console.warn,
|
|
|
+ 'error': console.error,
|
|
|
+}
|
|
|
+
|
|
|
let apiRequestService: ApiRequestService
|
|
|
-let entityManager: TestableEntityManager
|
|
|
+let entityManager: EntityManager
|
|
|
+let repo: Repository<ApiResource>
|
|
|
|
|
|
beforeEach(() => {
|
|
|
// @ts-ignore
|
|
|
apiRequestService = vi.fn() as ApiRequestService
|
|
|
|
|
|
- entityManager = new TestableEntityManager(apiRequestService)
|
|
|
+ entityManager = new EntityManager(apiRequestService)
|
|
|
+
|
|
|
+ // @ts-ignore
|
|
|
+ repo = vi.fn() as Repository<ApiResource>
|
|
|
+})
|
|
|
|
|
|
- // TODO: s'assurer que les mocks globaux sont bien réinitialisés après les tests, en particulier les fonctions de console
|
|
|
+afterEach(() => {
|
|
|
+ // Reset console methods after mock
|
|
|
+ console.log = _console['log']
|
|
|
+ console.warn = _console['warn']
|
|
|
+ console.error = _console['error']
|
|
|
})
|
|
|
|
|
|
describe('getRepository', () => {
|
|
|
@@ -62,7 +70,7 @@ describe('getModelFromIri', () => {
|
|
|
|
|
|
expect(result).toEqual(DummyApiResource)
|
|
|
})
|
|
|
- test('invalide Iri', () => {
|
|
|
+ test('invalid Iri', () => {
|
|
|
expect(() => entityManager.getModelFromIri('/invalid')).toThrowError('cannot parse the IRI')
|
|
|
})
|
|
|
})
|
|
|
@@ -72,19 +80,13 @@ describe('newInstance', () => {
|
|
|
test('simple call', () => {
|
|
|
const properties = { 'id': 1 }
|
|
|
|
|
|
- // @ts-ignore TODO: voir à factoriser
|
|
|
- const repo = vi.fn() as Repository<ApiResource>
|
|
|
-
|
|
|
// @ts-ignore
|
|
|
entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
|
|
|
return model === DummyApiResource ? repo : null
|
|
|
})
|
|
|
|
|
|
- entityManager.saveInitialState = vi.fn((model: typeof ApiResource, entity: ApiResource) => null)
|
|
|
-
|
|
|
// @ts-ignore
|
|
|
const entity = new DummyApiResource(properties)
|
|
|
- entity.setModel = vi.fn((model: typeof ApiResource) => null)
|
|
|
|
|
|
// @ts-ignore
|
|
|
repo.make = vi.fn((properties: object) => {
|
|
|
@@ -92,15 +94,14 @@ describe('newInstance', () => {
|
|
|
entity.id = properties.id
|
|
|
return entity
|
|
|
})
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
- repo.save = vi.fn((record: Element) => entity)
|
|
|
+ entityManager.save = vi.fn((model: typeof ApiResource, entity: ApiResource, permanent: boolean) => entity)
|
|
|
|
|
|
const result = entityManager.newInstance(DummyApiResource, properties)
|
|
|
|
|
|
expect(repo.make).toHaveBeenCalledWith(properties)
|
|
|
- expect(entity.setModel).toHaveBeenCalledWith(DummyApiResource)
|
|
|
- expect(repo.save).toHaveBeenCalledWith(entity)
|
|
|
- expect(entityManager.saveInitialState).toHaveBeenCalledWith(DummyApiResource, entity)
|
|
|
+ expect(entityManager.save).toHaveBeenCalledWith(DummyApiResource, entity, true)
|
|
|
|
|
|
expect(result.id).toEqual(properties.id)
|
|
|
})
|
|
|
@@ -373,61 +374,25 @@ describe('fetchCollection', () => {
|
|
|
})
|
|
|
})
|
|
|
|
|
|
-describe('saveResponseAsEntity', () => {
|
|
|
- test('simple call', async () => {
|
|
|
-
|
|
|
- const entity = new DummyApiModel({id: 1})
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- const repo = vi.fn() as Repository<ApiResource>
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
|
|
|
- return model === DummyApiModel ? repo : null
|
|
|
- })
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- const response = {id: 1} as Response
|
|
|
-
|
|
|
- entityManager.newInstance = vi.fn((model: typeof ApiResource, properties: object) => {
|
|
|
- return entity
|
|
|
- })
|
|
|
-
|
|
|
- entityManager.saveInitialState = vi.fn((model: typeof ApiResource, entity: ApiResource) => null)
|
|
|
-
|
|
|
- // @ts-ignore
|
|
|
- repo.save = vi.fn((data: any) => null)
|
|
|
-
|
|
|
- const result = await entityManager.saveResponseAsEntity(DummyApiModel, response)
|
|
|
-
|
|
|
- expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, {id: 1})
|
|
|
- expect(entityManager.saveInitialState).toHaveBeenCalledWith(DummyApiModel, entity)
|
|
|
- expect(repo.save).toHaveBeenCalledWith(entity)
|
|
|
-
|
|
|
- expect(result).toEqual(entity)
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-
|
|
|
describe('persist', () => {
|
|
|
test('new entity (POST)', async () => {
|
|
|
- const entity = new DummyApiModel({id: 'tmp1', name: 'bob'})
|
|
|
- entity.isNew = vi.fn(() => true)
|
|
|
+ const instance = new DummyApiModel({id: 'tmp1', name: 'bob'})
|
|
|
+ instance.isNew = vi.fn(() => true)
|
|
|
|
|
|
// @ts-ignore
|
|
|
- entity.$toJson = vi.fn(() => {
|
|
|
+ instance.$toJson = vi.fn(() => {
|
|
|
return {id: 'tmp1', name: 'bob'}
|
|
|
})
|
|
|
|
|
|
- entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
|
|
|
+ // TODO: attendre de voir si cet appel est nécessaire dans l'entity manager
|
|
|
+ // entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
|
|
|
|
|
|
const response = { id: 1, name: 'bob' }
|
|
|
// @ts-ignore
|
|
|
apiRequestService.post = vi.fn((url, data) => response)
|
|
|
|
|
|
// @ts-ignore
|
|
|
- entityManager.saveResponseAsEntity = vi.fn((model, response) => {
|
|
|
+ entityManager.newInstance = vi.fn((model, response) => {
|
|
|
const newEntity = new DummyApiModel(response)
|
|
|
// @ts-ignore
|
|
|
newEntity.id = response.id
|
|
|
@@ -437,14 +402,16 @@ describe('persist', () => {
|
|
|
return newEntity
|
|
|
})
|
|
|
|
|
|
+ // @ts-ignore
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
|
|
|
- const result = await entityManager.persist(DummyApiModel, entity)
|
|
|
+ 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(entityManager.saveResponseAsEntity).toHaveBeenCalledWith(DummyApiModel, response)
|
|
|
- expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(DummyApiModel, entity.id)
|
|
|
+ expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, response)
|
|
|
+ // @ts-ignore
|
|
|
+ expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(DummyApiModel, instance.id)
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
|
@@ -459,13 +426,14 @@ describe('persist', () => {
|
|
|
// @ts-ignore
|
|
|
entity.$toJson = vi.fn(() => props)
|
|
|
|
|
|
- entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
|
|
|
+ // TODO: attendre de voir si cet appel est nécessaire dans l'entity manager
|
|
|
+ // entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
|
|
|
|
|
|
// @ts-ignore
|
|
|
apiRequestService.put = vi.fn((url, data) => props)
|
|
|
|
|
|
// @ts-ignore
|
|
|
- entityManager.saveResponseAsEntity = vi.fn((model, response) => {
|
|
|
+ entityManager.newInstance = vi.fn((model, response) => {
|
|
|
const newEntity = new DummyApiModel(response)
|
|
|
// @ts-ignore
|
|
|
newEntity.id = response.id
|
|
|
@@ -475,12 +443,14 @@ describe('persist', () => {
|
|
|
return newEntity
|
|
|
})
|
|
|
|
|
|
+ // @ts-ignore
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
|
|
|
const result = await entityManager.persist(DummyApiModel, entity)
|
|
|
|
|
|
expect(apiRequestService.put).toHaveBeenCalledWith('api/dummyModel/1', {id: 1, name: 'bob'})
|
|
|
- expect(entityManager.saveResponseAsEntity).toHaveBeenCalledWith(DummyApiModel, props)
|
|
|
+ expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, props)
|
|
|
+ // @ts-ignore
|
|
|
expect(entityManager.removeTempAfterPersist).toHaveBeenCalledTimes(0)
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
@@ -497,7 +467,7 @@ describe('patch', () => {
|
|
|
apiRequestService.put = vi.fn((url, data) => props)
|
|
|
|
|
|
// @ts-ignore
|
|
|
- entityManager.saveResponseAsEntity = vi.fn((model, response) => {
|
|
|
+ entityManager.newInstance = vi.fn((model, response) => {
|
|
|
const newEntity = new DummyApiModel(response)
|
|
|
// @ts-ignore
|
|
|
newEntity.id = response.id
|
|
|
@@ -510,7 +480,7 @@ describe('patch', () => {
|
|
|
const result = await entityManager.patch(DummyApiModel, 1, {name: 'bobby'})
|
|
|
|
|
|
expect(apiRequestService.put).toHaveBeenCalledWith('api/dummyModel/1', '{"name":"bobby"}')
|
|
|
- expect(entityManager.saveResponseAsEntity).toHaveBeenCalledWith(DummyApiModel, {id: 1, name: 'bobby'})
|
|
|
+ expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, {id: 1, name: 'bobby'})
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bobby')
|
|
|
@@ -647,7 +617,7 @@ describe('isNewEntity', () => {
|
|
|
// @ts-ignore
|
|
|
repo.find = vi.fn((id: number) => entity)
|
|
|
|
|
|
- const result = entityManager.isNewEntity(DummyApiModel, 1)
|
|
|
+ const result = entityManager.isNewInstance(DummyApiModel, 1)
|
|
|
|
|
|
expect(result).toBeTruthy()
|
|
|
})
|
|
|
@@ -667,7 +637,7 @@ describe('isNewEntity', () => {
|
|
|
// @ts-ignore
|
|
|
repo.find = vi.fn((id: number) => entity)
|
|
|
|
|
|
- const result = entityManager.isNewEntity(DummyApiModel, 1)
|
|
|
+ const result = entityManager.isNewInstance(DummyApiModel, 1)
|
|
|
|
|
|
expect(result).toBeFalsy()
|
|
|
})
|
|
|
@@ -686,7 +656,7 @@ describe('isNewEntity', () => {
|
|
|
|
|
|
console.error = vi.fn()
|
|
|
|
|
|
- const result = entityManager.isNewEntity(DummyApiModel, 1)
|
|
|
+ const result = entityManager.isNewInstance(DummyApiModel, 1)
|
|
|
|
|
|
expect(result).toBeFalsy()
|
|
|
|
|
|
@@ -809,8 +779,9 @@ describe('removeTempAfterPersist', () => {
|
|
|
// @ts-ignore
|
|
|
repo.destroy = vi.fn()
|
|
|
|
|
|
+ // @ts-ignore
|
|
|
expect(() => entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')).toThrowError(
|
|
|
- 'Error: Can not remove a non-temporary entity'
|
|
|
+ 'Error: Can not remove a non-temporary model instance'
|
|
|
)
|
|
|
|
|
|
expect(repo.destroy).toHaveBeenCalledTimes(0)
|