| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046 |
- 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'
- class DummyApiResource extends ApiResource {
- static entity = 'dummyResource'
- @Uid()
- declare id: number | string
- @Str(null)
- declare name: string
- }
- class DummyApiModel extends ApiModel {
- static entity = 'dummyModel'
- @Uid()
- declare id: number | string
- @Str(null)
- 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()
- }
- public validateEntity(instance: unknown): void {
- return super.validateEntity(instance)
- }
- }
- const _console: any = {
- log: console.log,
- warn: console.warn,
- error: console.error,
- }
- vi.mock('~/models/models', async () => {
- class MyModel {
- static entity = 'my-model'
- }
- const modelsIndex: Record<string, any> = { 'my-model': () => MyModel }
- return {
- default: modelsIndex,
- }
- })
- let apiRequestService: ApiRequestService
- let entityManager: TestableEntityManager
- let repo: Repository<ApiResource>
- let _getRepo: (model: typeof ApiResource) => Repository<ApiResource>
- let _getProfileMask: () => object
- beforeEach(() => {
- // @ts-ignore
- repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- apiRequestService = vi.fn() as ApiRequestService
- _getRepo = vi.fn((model: typeof ApiResource) => repo)
- _getProfileMask = vi.fn(() => {
- return {}
- })
- entityManager = new TestableEntityManager(
- apiRequestService,
- _getRepo,
- _getProfileMask,
- )
- })
- afterEach(() => {
- // Reset console methods after mock
- console.log = _console.log
- console.warn = _console.warn
- console.error = _console.error
- })
- describe('getRepository', () => {
- test('simple call', () => {
- entityManager.getRepository(DummyApiResource)
- expect(_getRepo).toHaveBeenCalledWith(DummyApiResource)
- })
- })
- describe('getQuery', () => {
- test('simple call', () => {
- // @ts-ignore
- const repo = vi.fn()
- // @ts-ignore
- entityManager.getRepository = vi.fn(() => repo)
- const query = vi.fn()
- // @ts-ignore
- repo.where = vi.fn((method) => {
- // Query method is supposed to exclude NaN ids values
- expect(method({ id: 123 })).toBeTruthy()
- expect(method({ id: 'abc' })).toBeFalsy()
- return query
- })
- const result = entityManager.getQuery(DummyApiResource)
- expect(result).toEqual(query)
- })
- })
- describe('cast', () => {
- test('simple cast', () => {
- // @ts-ignore
- const result = entityManager.cast(DummyApiResource, { id: 1 })
- expect(result instanceof DummyApiResource).toEqual(true)
- })
- })
- describe('getModelFor', () => {
- test('simple call', async () => {
- const model = await entityManager.getModelFor('my-model')
- expect(model!.entity).toEqual('my-model')
- })
- test('non existing model', async () => {
- expect(
- async () => await entityManager.getModelFor('non-existing-model'),
- ).rejects.toThrowError(
- "No model found for entity name 'non-existing-model'",
- )
- })
- })
- describe('getModelFromIri', () => {
- test('simple call', async () => {
- // @ts-ignore
- entityManager.getModelFor = vi.fn(
- async (entityName: string) => DummyApiResource,
- )
- // @ts-ignore
- const result = await entityManager.getModelFromIri('/api/dummy/123')
- console.log(result)
- expect(result).toEqual(DummyApiResource)
- })
- test('invalide Iri', () => {
- expect(
- async () => await entityManager.getModelFromIri('/invalid'),
- ).rejects.toThrowError('cannot parse the IRI')
- })
- })
- describe('newInstance', () => {
- test('simple call', () => {
- const properties = { id: 1 }
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- const entity = new DummyApiResource(properties)
- // @ts-ignore
- repo.make = vi.fn((properties: object) => {
- // @ts-ignore
- entity.id = properties.id
- return entity
- })
- // @ts-ignore
- entityManager.save = vi.fn(
- (entity: ApiResource, permanent: boolean) => entity,
- )
- const result = entityManager.newInstance(DummyApiResource, properties)
- expect(repo.make).toHaveBeenCalledWith(properties)
- expect(entityManager.save).toHaveBeenCalledWith(entity, true)
- expect(result.id).toEqual(properties.id)
- })
- test('with no id provided', () => {
- const properties = { name: 'bob' }
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- entityManager.saveInitialState = vi.fn(
- (model: typeof ApiResource, entity: ApiResource) => null,
- )
- // @ts-ignore
- const entity = new DummyApiResource(properties)
- // @ts-ignore
- repo.make = vi.fn((properties: object) => {
- // @ts-ignore
- entity.name = properties.name
- return entity
- })
- // @ts-ignore
- repo.save = vi.fn((record: Element) => entity)
- const result = entityManager.newInstance(DummyApiResource, properties)
- expect(result.id, "id is 'tmp' followed by a valid uuid-V4").toMatch(
- /tmp[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}/,
- )
- expect(result.name).toEqual(properties.name)
- })
- })
- describe('save', () => {
- test('simple call', () => {
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- 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)
- })
- })
- describe('find', () => {
- test('simple call', () => {
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- const entity = new DummyApiResource({ id: 1 })
- // @ts-ignore
- repo.find = vi.fn((id: string | number) => entity)
- entityManager.find(DummyApiResource, 1)
- expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiResource)
- expect(repo.find).toHaveBeenCalledWith(1)
- })
- })
- describe('fetch', () => {
- test('not in store, no force refresh', async () => {
- const properties = { id: 1 }
- const entity = new DummyApiResource({ id: 1 })
- // @ts-ignore
- entityManager.find = vi.fn(
- (model: typeof ApiResource, id: number) => undefined,
- )
- // @ts-ignore
- apiRequestService.get = vi.fn(async (url: string) => properties)
- // @ts-ignore
- entityManager.newInstance = vi.fn(
- (model: typeof ApiResource, props: object) => entity,
- )
- const result = await entityManager.fetch(DummyApiResource, 1)
- expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
- expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
- id: 1,
- name: null,
- _model: undefined,
- })
- expect(result).toEqual(entity)
- })
- test('in store, no force refresh', async () => {
- const properties = { id: 1 }
- const entity = new DummyApiResource({ id: 1 })
- // @ts-ignore
- entityManager.find = vi.fn(
- (model: typeof ApiResource, id: number) => entity,
- )
- const result = await entityManager.fetch(DummyApiResource, 1)
- expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
- expect(result).toEqual(entity)
- })
- test('in store, but with force refresh', async () => {
- const properties = { id: 1 }
- const entity = new DummyApiResource({ id: 1 })
- // @ts-ignore
- entityManager.find = vi.fn(
- (model: typeof ApiResource, id: number) => undefined,
- )
- // @ts-ignore
- apiRequestService.get = vi.fn(async (url: string) => properties)
- // @ts-ignore
- entityManager.newInstance = vi.fn(
- (model: typeof ApiResource, props: object) => entity,
- )
- const result = await entityManager.fetch(DummyApiResource, 1, true)
- expect(entityManager.find).toHaveBeenCalledTimes(0)
- expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
- id: 1,
- name: null,
- _model: undefined,
- })
- expect(result).toEqual(entity)
- })
- })
- describe('fetchCollection', () => {
- test('simple call', async () => {
- const collection = {
- '@type': 'hydra:Collection',
- 'hydra:totalItems': 3,
- 'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
- }
- // @ts-ignore
- apiRequestService.get = vi.fn(async (url: string) => collection)
- // @ts-ignore
- entityManager.newInstance = vi.fn(
- (model: typeof ApiResource, props: object) => {
- return new DummyApiResource(props)
- },
- )
- const piniaOrmQuery = vi.fn()
- // @ts-ignore
- entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
- const result = await entityManager.fetchCollection(DummyApiResource, null)
- expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource')
- expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
- id: 1,
- name: null,
- _model: undefined,
- })
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
- id: 2,
- name: null,
- _model: undefined,
- })
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
- id: 3,
- name: null,
- _model: undefined,
- })
- // @ts-ignore
- piniaOrmQuery.get = vi.fn(() => [
- new DummyApiResource({ id: 1 }),
- new DummyApiResource({ id: 2 }),
- new DummyApiResource({ id: 3 }),
- ])
- expect(result.value.items).toEqual([
- new DummyApiResource({ id: 1 }),
- new DummyApiResource({ id: 2 }),
- new DummyApiResource({ id: 3 }),
- ])
- expect(result.value.pagination, 'default pagination').toEqual({
- first: 1,
- last: 1,
- next: undefined,
- previous: undefined,
- })
- // @ts-expect-error Needed to avoid 'Cannot stringify non POJO' occasional bugs
- expect(result.toJSON()).toEqual(
- 'Computed result from fetchCollection at : api/dummyResource',
- )
- })
- test('with a parent', async () => {
- const collection = {
- '@type': 'hydra:Collection',
- 'hydra:totalItems': 3,
- 'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
- }
- // @ts-ignore
- apiRequestService.get = vi.fn(async (url: string) => collection)
- const piniaOrmQuery = vi.fn()
- // @ts-ignore
- entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
- // @ts-ignore
- entityManager.newInstance = vi.fn(
- (model: typeof ApiResource, props: object) => {
- return new DummyApiResource(props)
- },
- )
- const parent = new DummyApiModel()
- parent.id = 100
- parent.entity = 'dummyModel' // TODO: je ne comprend pas pqoi cette ligne est nécessaire...
- await entityManager.fetchCollection(DummyApiResource, parent)
- expect(apiRequestService.get).toHaveBeenCalledWith(
- 'api/dummyModel/100/dummyResource',
- )
- })
- test('with a query', async () => {
- const collection = {
- '@type': 'hydra:Collection',
- 'hydra:totalItems': 3,
- 'hydra:member': [{ id: 1 }, { id: 2 }, { id: 3 }],
- }
- const query = vi.fn()
- // @ts-ignore
- query.getUrlQuery = vi.fn(() => 'foo=bar')
- // @ts-ignore
- query.applyToPiniaOrmQuery = vi.fn((q) => q)
- const piniaOrmQuery = vi.fn()
- // @ts-ignore
- entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
- // @ts-ignore
- apiRequestService.get = vi.fn(async (url: string) => collection)
- // @ts-ignore
- entityManager.newInstance = vi.fn(
- (model: typeof ApiResource, props: object) => {
- return new DummyApiResource(props)
- },
- )
- // @ts-ignore
- await entityManager.fetchCollection(DummyApiResource, null, query)
- expect(apiRequestService.get).toHaveBeenCalledWith(
- 'api/dummyResource?foo=bar',
- )
- // @ts-ignore
- expect(query.getUrlQuery).toHaveBeenCalledWith()
- // @ts-ignore
- expect(query.applyToPiniaOrmQuery).toHaveBeenCalledWith(piniaOrmQuery)
- })
- })
- describe('persist', () => {
- test('new entity (POST)', async () => {
- const instance = new DummyApiModel({ id: 'tmp1', name: 'bob' })
- instance.isNew = vi.fn(() => true)
- // @ts-ignore
- instance.$toJson = vi.fn(() => {
- return { id: 'tmp1', name: 'bob' }
- })
- // @ts-ignore
- entityManager.cast = vi.fn(
- (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)
- // @ts-ignore
- entityManager.newInstance = vi.fn((model, response) => {
- const newEntity = new DummyApiModel(response)
- // @ts-ignore
- newEntity.id = response.id
- // @ts-ignore
- newEntity.name = response.name
- return newEntity
- })
- // @ts-ignore
- entityManager.removeTempAfterPersist = vi.fn()
- entityManager.makeProfileHash = vi.fn(async () => await 'azerty')
- const result = await entityManager.persist(instance)
- // temp id should have been purged from the posted data
- expect(apiRequestService.post).toHaveBeenCalledWith(
- 'api/dummyModel',
- {
- name: 'bob',
- },
- null,
- { profileHash: 'azerty' },
- )
- expect(entityManager.newInstance).toHaveBeenCalledWith(
- DummyApiModel,
- response,
- )
- // @ts-ignore
- expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(
- DummyApiModel,
- instance.id,
- )
- // @ts-ignore
- expect(entityManager.makeProfileHash).toHaveBeenCalledTimes(1)
- expect(result.id).toEqual(1)
- expect(result.name).toEqual('bob')
- expect(entityManager.validateEntity).toHaveBeenCalledWith(instance)
- })
- test('existing entity (PUT)', async () => {
- const props = { id: 1, name: 'bob' }
- const entity = new DummyApiModel(props)
- entity.id = 1
- entity.isNew = vi.fn(() => false)
- // @ts-ignore
- entity.$toJson = vi.fn(() => props)
- // 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.newInstance = vi.fn((model, response) => {
- const newEntity = new DummyApiModel(response)
- // @ts-ignore
- newEntity.id = response.id
- // @ts-ignore
- newEntity.name = response.name
- return newEntity
- })
- // @ts-ignore
- 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(
- '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')
- expect(entityManager.validateEntity).toHaveBeenCalledWith(entity)
- })
- })
- describe('patch', () => {
- test('simple call', async () => {
- const props = { id: 1, name: 'bobby' }
- // @ts-ignore
- apiRequestService.put = vi.fn((url, data) => props)
- // @ts-ignore
- entityManager.newInstance = vi.fn((model, response) => {
- const newEntity = new DummyApiModel(response)
- // @ts-ignore
- newEntity.id = response.id
- // @ts-ignore
- newEntity.name = response.name
- return newEntity
- })
- const result = await entityManager.patch(DummyApiModel, 1, {
- name: 'bobby',
- })
- expect(apiRequestService.put).toHaveBeenCalledWith(
- 'api/dummyModel/1',
- '{"name":"bobby"}',
- )
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, {
- id: 1,
- name: 'bobby',
- })
- expect(result.id).toEqual(1)
- expect(result.name).toEqual('bobby')
- })
- })
- describe('delete', () => {
- test('delete non persisted entity', () => {
- const entity = new DummyApiModel()
- entity.isNew = vi.fn(() => true)
- entity.id = 'tmp123'
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiModel ? repo : null
- })
- entityManager.validateEntity = vi.fn((instance: unknown) => {})
- apiRequestService.delete = vi.fn()
- // @ts-ignore
- repo.destroy = vi.fn((id: number) => null)
- entityManager.delete(entity)
- 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 () => {
- const entity = new DummyApiModel()
- entity.isNew = vi.fn(() => false)
- entity.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
- apiRequestService.delete = vi.fn((id: number) => null)
- // @ts-ignore
- repo.destroy = vi.fn((id: number) => null)
- await entityManager.delete(entity)
- expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
- expect(apiRequestService.delete).toHaveBeenCalledWith('api/dummyModel/1')
- expect(repo.destroy).toHaveBeenCalledWith(1)
- })
- })
- describe('reset', () => {
- test('simple call', () => {
- const entity = new DummyApiModel()
- entity.id = 1
- entity.name = 'paul'
- const initialEntity = new DummyApiModel()
- initialEntity.id = 1
- initialEntity.name = 'serges'
- // @ts-ignore
- entityManager.getInitialStateOf = vi.fn(
- (model: typeof ApiResource, id: string | number) => initialEntity,
- )
- // @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
- repo.save = vi.fn((data: any) => null)
- const result = entityManager.reset(DummyApiModel, entity)
- // @ts-ignore
- expect(entityManager.getInitialStateOf).toHaveBeenCalledWith(
- DummyApiModel,
- 1,
- )
- expect(repo.save).toHaveBeenCalledWith(initialEntity)
- expect(result).toEqual(initialEntity)
- })
- test('no initial state stored', () => {
- const entity = new DummyApiModel()
- entity.id = 1
- // @ts-ignore
- entityManager.getInitialStateOf = vi.fn(
- (model: typeof ApiResource, id: string | number) => null,
- )
- expect(() => entityManager.reset(DummyApiModel, entity)).toThrowError(
- 'no initial state recorded for this object - abort [dummyModel/1]',
- )
- })
- })
- describe('flush', () => {
- test('simple call', () => {
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiModel ? repo : null
- })
- repo.flush = vi.fn()
- entityManager.flush(DummyApiModel)
- expect(repo.flush).toHaveBeenCalled()
- })
- })
- describe('isNewEntity', () => {
- test('with new entity', () => {
- const entity = new DummyApiModel()
- entity.isNew = vi.fn(() => true)
- // @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
- repo.find = vi.fn((id: number) => entity)
- const result = entityManager.isNewInstance(DummyApiModel, 1)
- expect(result).toBeTruthy()
- })
- test('with existing entity', () => {
- const entity = new DummyApiModel()
- entity.isNew = vi.fn(() => false)
- // @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
- repo.find = vi.fn((id: number) => entity)
- const result = entityManager.isNewInstance(DummyApiModel, 1)
- expect(result).toBeFalsy()
- })
- test('non-existing entity', () => {
- // @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
- repo.find = vi.fn((id: number) => null)
- console.error = vi.fn()
- const result = entityManager.isNewInstance(DummyApiModel, 1)
- expect(result).toBeFalsy()
- expect(console.error).toHaveBeenCalledWith('dummyModel/1 does not exist!')
- })
- })
- describe('saveInitialState', () => {
- test('simple call', () => {
- // @ts-ignore
- const entity = { id: 1, name: 'bob' } as DummyApiResource
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.save = vi.fn((record: Element) => null)
- // @ts-ignore
- entityManager.saveInitialState(DummyApiResource, entity)
- expect(repo.save).toHaveBeenCalledWith({ id: '_clone_1', name: 'bob' })
- expect(entity.id).toEqual(1)
- })
- })
- describe('getInitialStateOf', () => {
- test('with initial state', () => {
- // @ts-ignore
- const entity = { id: 1, name: 'bob' } as DummyApiResource
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.find = vi.fn((id: number | string) => {
- // @ts-ignore
- return { id: 1, name: 'robert' } as DummyApiResource
- })
- // @ts-ignore
- const result = entityManager.getInitialStateOf(
- DummyApiResource,
- 1,
- ) as DummyApiResource
- expect(repo.find).toHaveBeenCalledWith('_clone_1')
- expect(result.id).toEqual(1)
- expect(result.name).toEqual('robert')
- })
- test('without initial state', () => {
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.find = vi.fn((id: number | string) => null)
- // @ts-ignore
- const result = entityManager.getInitialStateOf(
- DummyApiResource,
- 1,
- ) as DummyApiResource
- expect(repo.find).toHaveBeenCalledWith('_clone_1')
- expect(result).toEqual(null)
- })
- })
- describe('removeTempAfterPersist', () => {
- test('simple call', () => {
- // @ts-ignore
- const entity = new DummyApiResource()
- entity.id = 'tmp123'
- entity.isNew = vi.fn(() => true)
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.find = vi.fn((id: number | string) => entity)
- // @ts-ignore
- repo.destroy = vi.fn()
- // @ts-ignore
- entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')
- expect(repo.destroy).toHaveBeenCalledWith('tmp123')
- expect(repo.destroy).toHaveBeenCalledWith('_clone_tmp123')
- })
- test('entity is not temporary', () => {
- // @ts-ignore
- const entity = new DummyApiResource()
- entity.id = 1
- entity.isNew = vi.fn(() => false)
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.find = vi.fn((id: number | string) => entity)
- // @ts-ignore
- repo.destroy = vi.fn()
- // @ts-ignore
- expect(() =>
- entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123'),
- ).toThrowError('Error: Can not remove a non-temporary model instance')
- expect(repo.destroy).toHaveBeenCalledTimes(0)
- })
- test('entity does not exist', () => {
- // @ts-ignore
- const repo = vi.fn() as Repository<ApiResource>
- // @ts-ignore
- entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
- return model === DummyApiResource ? repo : null
- })
- // @ts-ignore
- repo.find = vi.fn((id: number | string) => null)
- // @ts-ignore
- repo.destroy = vi.fn()
- console.error = vi.fn()
- // @ts-ignore
- entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')
- expect(repo.destroy).toHaveBeenCalledTimes(0)
- expect(console.error).toHaveBeenCalledWith(
- 'dummyResource/tmp123 does not exist!',
- )
- })
- })
- describe('makeProfileHash', () => {
- test('simple call', async () => {
- entityManager._getProfileMask = vi.fn(() => {
- return { a: 1 }
- })
- expect(await entityManager.makeProfileHash()).toEqual(
- '9f89c740ceb46d7418c924a78ac57941d5e96520',
- )
- })
- })
- 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"}',
- )
- })
- })
|