|
|
@@ -40,6 +40,10 @@ class TestableEntityManager extends EntityManager {
|
|
|
public makeProfileHash() {
|
|
|
return super.makeProfileHash()
|
|
|
}
|
|
|
+
|
|
|
+ public validateEntity(instance: unknown): void {
|
|
|
+ return super.validateEntity(instance)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const _console: any = {
|
|
|
@@ -187,8 +191,7 @@ describe('newInstance', () => {
|
|
|
|
|
|
// @ts-ignore
|
|
|
entityManager.save = vi.fn(
|
|
|
- (entity: ApiResource, permanent: boolean) =>
|
|
|
- entity,
|
|
|
+ (entity: ApiResource, permanent: boolean) => entity,
|
|
|
)
|
|
|
|
|
|
const result = entityManager.newInstance(DummyApiResource, properties)
|
|
|
@@ -247,12 +250,15 @@ describe('save', () => {
|
|
|
return model === DummyApiResource ? repo : null
|
|
|
})
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
+
|
|
|
// @ts-ignore
|
|
|
repo.save = vi.fn((record: Element) => entity)
|
|
|
|
|
|
const entity = new DummyApiResource({ id: 1 })
|
|
|
entityManager.save(entity)
|
|
|
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
expect(repo.save).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
|
})
|
|
|
@@ -420,6 +426,10 @@ describe('fetchCollection', () => {
|
|
|
next: undefined,
|
|
|
previous: undefined,
|
|
|
})
|
|
|
+
|
|
|
+ // @ts-expect-error Needed to avoid 'Cannot stringify non POJO' occasional bugs
|
|
|
+ // eslint-disable-next-line
|
|
|
+ expect(result.toJSON()).toEqual('Computed result from fetchCollection at : api/dummyResource')
|
|
|
})
|
|
|
|
|
|
test('with a parent', async () => {
|
|
|
@@ -513,6 +523,8 @@ describe('persist', () => {
|
|
|
(model: typeof ApiResource, entity: ApiResource): ApiResource => entity,
|
|
|
)
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
+
|
|
|
const response = { id: 1, name: 'bob' }
|
|
|
// @ts-ignore
|
|
|
apiRequestService.post = vi.fn((url, data) => response)
|
|
|
@@ -557,6 +569,8 @@ describe('persist', () => {
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
|
+
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(instance)
|
|
|
})
|
|
|
|
|
|
test('existing entity (PUT)', async () => {
|
|
|
@@ -589,6 +603,8 @@ describe('persist', () => {
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
+
|
|
|
const result = await entityManager.persist(entity)
|
|
|
|
|
|
expect(apiRequestService.put).toHaveBeenCalledWith(
|
|
|
@@ -608,6 +624,8 @@ describe('persist', () => {
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
|
+
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
|
})
|
|
|
|
|
|
@@ -661,6 +679,8 @@ describe('delete', () => {
|
|
|
return model === DummyApiModel ? repo : null
|
|
|
})
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
+
|
|
|
apiRequestService.delete = vi.fn()
|
|
|
|
|
|
// @ts-ignore
|
|
|
@@ -671,6 +691,8 @@ describe('delete', () => {
|
|
|
expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
|
expect(apiRequestService.delete).toHaveBeenCalledTimes(0)
|
|
|
expect(repo.destroy).toHaveBeenCalledWith('tmp123')
|
|
|
+
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
|
|
|
|
test('delete persisted entity', async () => {
|
|
|
@@ -1004,3 +1026,19 @@ describe('makeProfileHash', () => {
|
|
|
)
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+describe('validateEntity', () => {
|
|
|
+ test('instance with numeric id', async () => {
|
|
|
+ entityManager.validateEntity({ id: 123 })
|
|
|
+ })
|
|
|
+
|
|
|
+ test('instance with temp id', async () => {
|
|
|
+ entityManager.validateEntity({ id: 'tmpazerty' })
|
|
|
+ })
|
|
|
+
|
|
|
+ test('invalid entity', async () => {
|
|
|
+ expect(() => entityManager.validateEntity({ id: 'azerty' })).toThrowError(
|
|
|
+ 'Definition error for the entity, did you use the entityManager.newInstance(...) method?\n{"id":"azerty"}',
|
|
|
+ )
|
|
|
+ })
|
|
|
+})
|