|
@@ -40,6 +40,10 @@ class TestableEntityManager extends EntityManager {
|
|
|
public makeProfileHash() {
|
|
public makeProfileHash() {
|
|
|
return super.makeProfileHash()
|
|
return super.makeProfileHash()
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public validateEntity(instance: unknown): void {
|
|
|
|
|
+ return super.validateEntity(instance)
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const _console: any = {
|
|
const _console: any = {
|
|
@@ -187,18 +191,13 @@ describe('newInstance', () => {
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
entityManager.save = vi.fn(
|
|
entityManager.save = vi.fn(
|
|
|
- (model: typeof ApiResource, entity: ApiResource, permanent: boolean) =>
|
|
|
|
|
- entity,
|
|
|
|
|
|
|
+ (entity: ApiResource, permanent: boolean) => entity,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const result = entityManager.newInstance(DummyApiResource, properties)
|
|
const result = entityManager.newInstance(DummyApiResource, properties)
|
|
|
|
|
|
|
|
expect(repo.make).toHaveBeenCalledWith(properties)
|
|
expect(repo.make).toHaveBeenCalledWith(properties)
|
|
|
- expect(entityManager.save).toHaveBeenCalledWith(
|
|
|
|
|
- DummyApiResource,
|
|
|
|
|
- entity,
|
|
|
|
|
- true,
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ expect(entityManager.save).toHaveBeenCalledWith(entity, true)
|
|
|
|
|
|
|
|
expect(result.id).toEqual(properties.id)
|
|
expect(result.id).toEqual(properties.id)
|
|
|
})
|
|
})
|
|
@@ -251,12 +250,15 @@ describe('save', () => {
|
|
|
return model === DummyApiResource ? repo : null
|
|
return model === DummyApiResource ? repo : null
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
|
|
+
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
repo.save = vi.fn((record: Element) => entity)
|
|
repo.save = vi.fn((record: Element) => entity)
|
|
|
|
|
|
|
|
- const entity = new DummyApiResource()
|
|
|
|
|
- entityManager.save(DummyApiResource, entity)
|
|
|
|
|
|
|
+ const entity = new DummyApiResource({ id: 1 })
|
|
|
|
|
+ entityManager.save(entity)
|
|
|
|
|
|
|
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
expect(repo.save).toHaveBeenCalledWith(entity)
|
|
expect(repo.save).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
@@ -424,6 +426,12 @@ describe('fetchCollection', () => {
|
|
|
next: undefined,
|
|
next: undefined,
|
|
|
previous: 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 () => {
|
|
test('with a parent', async () => {
|
|
@@ -517,6 +525,8 @@ describe('persist', () => {
|
|
|
(model: typeof ApiResource, entity: ApiResource): ApiResource => entity,
|
|
(model: typeof ApiResource, entity: ApiResource): ApiResource => entity,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
|
|
+
|
|
|
const response = { id: 1, name: 'bob' }
|
|
const response = { id: 1, name: 'bob' }
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
apiRequestService.post = vi.fn((url, data) => response)
|
|
apiRequestService.post = vi.fn((url, data) => response)
|
|
@@ -536,7 +546,7 @@ describe('persist', () => {
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
|
|
|
|
|
|
- const result = await entityManager.persist(DummyApiModel, instance)
|
|
|
|
|
|
|
+ const result = await entityManager.persist(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(
|
|
expect(apiRequestService.post).toHaveBeenCalledWith(
|
|
@@ -561,6 +571,8 @@ describe('persist', () => {
|
|
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
expect(result.name).toEqual('bob')
|
|
|
|
|
+
|
|
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(instance)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('existing entity (PUT)', async () => {
|
|
test('existing entity (PUT)', async () => {
|
|
@@ -593,7 +605,9 @@ describe('persist', () => {
|
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
entityManager.removeTempAfterPersist = vi.fn()
|
|
|
entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
|
|
|
|
|
|
|
|
- const result = await entityManager.persist(DummyApiModel, entity)
|
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
|
|
+
|
|
|
|
|
+ const result = await entityManager.persist(entity)
|
|
|
|
|
|
|
|
expect(apiRequestService.put).toHaveBeenCalledWith(
|
|
expect(apiRequestService.put).toHaveBeenCalledWith(
|
|
|
'api/dummyModel/1',
|
|
'api/dummyModel/1',
|
|
@@ -612,6 +626,8 @@ describe('persist', () => {
|
|
|
|
|
|
|
|
expect(result.id).toEqual(1)
|
|
expect(result.id).toEqual(1)
|
|
|
expect(result.name).toEqual('bob')
|
|
expect(result.name).toEqual('bob')
|
|
|
|
|
+
|
|
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -665,16 +681,20 @@ describe('delete', () => {
|
|
|
return model === DummyApiModel ? repo : null
|
|
return model === DummyApiModel ? repo : null
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ entityManager.validateEntity = vi.fn((instance: unknown) => {})
|
|
|
|
|
+
|
|
|
apiRequestService.delete = vi.fn()
|
|
apiRequestService.delete = vi.fn()
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
repo.destroy = vi.fn((id: number) => null)
|
|
repo.destroy = vi.fn((id: number) => null)
|
|
|
|
|
|
|
|
- entityManager.delete(DummyApiModel, entity)
|
|
|
|
|
|
|
+ entityManager.delete(entity)
|
|
|
|
|
|
|
|
expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
|
expect(apiRequestService.delete).toHaveBeenCalledTimes(0)
|
|
expect(apiRequestService.delete).toHaveBeenCalledTimes(0)
|
|
|
expect(repo.destroy).toHaveBeenCalledWith('tmp123')
|
|
expect(repo.destroy).toHaveBeenCalledWith('tmp123')
|
|
|
|
|
+
|
|
|
|
|
+ expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
test('delete persisted entity', async () => {
|
|
test('delete persisted entity', async () => {
|
|
@@ -696,7 +716,7 @@ describe('delete', () => {
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
repo.destroy = vi.fn((id: number) => null)
|
|
repo.destroy = vi.fn((id: number) => null)
|
|
|
|
|
|
|
|
- await entityManager.delete(DummyApiModel, entity)
|
|
|
|
|
|
|
+ await entityManager.delete(entity)
|
|
|
|
|
|
|
|
expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
|
|
|
expect(apiRequestService.delete).toHaveBeenCalledWith('api/dummyModel/1')
|
|
expect(apiRequestService.delete).toHaveBeenCalledWith('api/dummyModel/1')
|
|
@@ -1008,3 +1028,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"}',
|
|
|
|
|
+ )
|
|
|
|
|
+ })
|
|
|
|
|
+})
|