entityManager.test.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. import { describe, test, it, expect } from 'vitest'
  2. import EntityManager from "~/services/data/entityManager";
  3. import ApiResource from "~/models/ApiResource";
  4. import ApiModel from "~/models/ApiModel";
  5. import ApiRequestService from "~/services/data/apiRequestService";
  6. import {Element, Repository} from "pinia-orm";
  7. class DummyApiResource extends ApiResource {
  8. static entity = 'dummyResource'
  9. }
  10. class DummyApiModel extends ApiModel {
  11. static entity = 'dummyModel'
  12. }
  13. let _console: any = {
  14. 'log': console.log,
  15. 'warn': console.warn,
  16. 'error': console.error,
  17. }
  18. let apiRequestService: ApiRequestService
  19. let entityManager: EntityManager
  20. let repo: Repository<ApiResource>
  21. beforeEach(() => {
  22. // @ts-ignore
  23. apiRequestService = vi.fn() as ApiRequestService
  24. entityManager = new EntityManager(apiRequestService)
  25. // @ts-ignore
  26. repo = vi.fn() as Repository<ApiResource>
  27. })
  28. afterEach(() => {
  29. // Reset console methods after mock
  30. console.log = _console['log']
  31. console.warn = _console['warn']
  32. console.error = _console['error']
  33. })
  34. describe('getRepository', () => {
  35. // TODO: à revoir
  36. })
  37. describe('cast', () => {
  38. test('simple cast', () => {
  39. // @ts-ignore
  40. const result = entityManager.cast(DummyApiResource, { id: 1 })
  41. expect(result instanceof DummyApiResource).toEqual(true)
  42. })
  43. })
  44. describe('getModelFor', () => {
  45. // TODO: à revoir
  46. })
  47. describe('getModelFromIri', () => {
  48. test('simple call', () => {
  49. // @ts-ignore
  50. entityManager.getModelFor = vi.fn((entityName: string) => entityName === 'dummy' ? DummyApiResource : null)
  51. // @ts-ignore
  52. const result = entityManager.getModelFromIri('/api/dummy/123')
  53. expect(result).toEqual(DummyApiResource)
  54. })
  55. test('invalid Iri', () => {
  56. expect(() => entityManager.getModelFromIri('/invalid')).toThrowError('cannot parse the IRI')
  57. })
  58. })
  59. describe('newInstance', () => {
  60. test('simple call', () => {
  61. const properties = { 'id': 1 }
  62. // @ts-ignore
  63. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  64. return model === DummyApiResource ? repo : null
  65. })
  66. // @ts-ignore
  67. const entity = new DummyApiResource(properties)
  68. // @ts-ignore
  69. repo.make = vi.fn((properties: object) => {
  70. // @ts-ignore
  71. entity.id = properties.id
  72. return entity
  73. })
  74. // @ts-ignore
  75. entityManager.save = vi.fn((model: typeof ApiResource, entity: ApiResource, permanent: boolean) => entity)
  76. const result = entityManager.newInstance(DummyApiResource, properties)
  77. expect(repo.make).toHaveBeenCalledWith(properties)
  78. expect(entityManager.save).toHaveBeenCalledWith(DummyApiResource, entity, true)
  79. expect(result.id).toEqual(properties.id)
  80. })
  81. test('with no id provided', () => {
  82. const properties = { 'name': 'bob' }
  83. // @ts-ignore
  84. const repo = vi.fn() as Repository<ApiResource>
  85. // @ts-ignore
  86. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  87. return model === DummyApiResource ? repo : null
  88. })
  89. entityManager.saveInitialState = vi.fn((model: typeof ApiResource, entity: ApiResource) => null)
  90. // @ts-ignore
  91. const entity = new DummyApiResource(properties)
  92. entity.setModel = vi.fn((model: typeof ApiResource) => null)
  93. // @ts-ignore
  94. repo.make = vi.fn((properties: object) => {
  95. // @ts-ignore
  96. entity.name = properties.name
  97. return entity
  98. })
  99. // @ts-ignore
  100. repo.save = vi.fn((record: Element) => entity)
  101. const result = entityManager.newInstance(DummyApiResource, properties)
  102. expect(
  103. result.id,
  104. 'id is \'tmp\' followed by a valid uuid-V4'
  105. ).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}/)
  106. expect(result.name).toEqual(properties.name)
  107. })
  108. })
  109. describe('save', () => {
  110. test('simple call', () => {
  111. // @ts-ignore
  112. const repo = vi.fn() as Repository<ApiResource>
  113. // @ts-ignore
  114. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  115. return model === DummyApiResource ? repo : null
  116. })
  117. // @ts-ignore
  118. repo.save = vi.fn((record: Element) => entity)
  119. const entity = new DummyApiResource()
  120. entityManager.save(DummyApiResource, entity)
  121. expect(repo.save).toHaveBeenCalledWith(entity)
  122. })
  123. })
  124. describe('find', () => {
  125. test('simple call', () => {
  126. // @ts-ignore
  127. const repo = vi.fn() as Repository<ApiResource>
  128. // @ts-ignore
  129. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  130. return model === DummyApiResource ? repo : null
  131. })
  132. const entity = new DummyApiResource({ id: 1 })
  133. // @ts-ignore
  134. repo.find = vi.fn((id: string | number) => entity)
  135. entityManager.find(DummyApiResource, 1)
  136. expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiResource)
  137. expect(repo.find).toHaveBeenCalledWith(1)
  138. })
  139. })
  140. describe('fetch', () => {
  141. test('not in store, no force refresh', async () => {
  142. const properties = { id: 1 }
  143. const entity = new DummyApiResource({ id: 1 })
  144. // @ts-ignore
  145. entityManager.find = vi.fn((model: typeof ApiResource, id: number) => undefined)
  146. // @ts-ignore
  147. apiRequestService.get = vi.fn(async (url: string) => properties)
  148. // @ts-ignore
  149. entityManager.newInstance = vi.fn((model: typeof ApiResource, props: object) => entity)
  150. const result = await entityManager.fetch(DummyApiResource, 1)
  151. expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
  152. expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
  153. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, properties)
  154. expect(result).toEqual(entity)
  155. })
  156. test('in store, no force refresh', async () => {
  157. const properties = { id: 1 }
  158. const entity = new DummyApiResource({ id: 1 })
  159. // @ts-ignore
  160. entityManager.find = vi.fn((model: typeof ApiResource, id: number) => entity)
  161. const result = await entityManager.fetch(DummyApiResource, 1)
  162. expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
  163. expect(result).toEqual(entity)
  164. })
  165. test('in store, but with force refresh', async () => {
  166. const properties = { id: 1 }
  167. const entity = new DummyApiResource({ id: 1 })
  168. // @ts-ignore
  169. entityManager.find = vi.fn((model: typeof ApiResource, id: number) => undefined)
  170. // @ts-ignore
  171. apiRequestService.get = vi.fn(async (url: string) => properties)
  172. // @ts-ignore
  173. entityManager.newInstance = vi.fn((model: typeof ApiResource, props: object) => entity)
  174. const result = await entityManager.fetch(DummyApiResource, 1, true)
  175. expect(entityManager.find).toHaveBeenCalledTimes(0)
  176. expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
  177. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, properties)
  178. expect(result).toEqual(entity)
  179. })
  180. })
  181. describe('fetchCollection', () => {
  182. test('simple call', async () => {
  183. const collection = {
  184. '@type': 'hydra:Collection',
  185. 'hydra:totalItems': 3,
  186. 'hydra:member': [
  187. {id: 1},
  188. {id: 2},
  189. {id: 3},
  190. ]
  191. }
  192. // @ts-ignore
  193. apiRequestService.get = vi.fn(async (url: string) => collection)
  194. // @ts-ignore
  195. entityManager.newInstance = vi.fn((model: typeof ApiResource, props: object) => {
  196. return new DummyApiResource(props)
  197. })
  198. const result = await entityManager.fetchCollection(DummyApiResource, null)
  199. expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', [])
  200. expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
  201. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 1})
  202. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 2})
  203. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 3})
  204. expect(result.items).toEqual([
  205. new DummyApiResource({id: 1}),
  206. new DummyApiResource({id: 2}),
  207. new DummyApiResource({id: 3})
  208. ])
  209. expect(result.pagination, 'default pagination').toEqual({
  210. first: 1,
  211. last: 1,
  212. next: undefined,
  213. previous: undefined
  214. })
  215. })
  216. test('with a parent', async () => {
  217. const collection = {
  218. '@type': 'hydra:Collection',
  219. 'hydra:totalItems': 3,
  220. 'hydra:member': [
  221. {id: 1},
  222. {id: 2},
  223. {id: 3},
  224. ]
  225. }
  226. // @ts-ignore
  227. apiRequestService.get = vi.fn(async (url: string) => collection)
  228. // @ts-ignore
  229. entityManager.newInstance = vi.fn((model: typeof ApiResource, props: object) => {
  230. return new DummyApiResource(props)
  231. })
  232. const parent = new DummyApiModel()
  233. parent.id = 100
  234. parent.entity = 'dummyModel' // TODO: je ne comprend pas pqoi cette ligne est nécessaire...
  235. await entityManager.fetchCollection(DummyApiResource, parent)
  236. expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyModel/100/dummyResource', [])
  237. })
  238. test('with a query', async () => {
  239. const collection = {
  240. '@type': 'hydra:Collection',
  241. 'hydra:totalItems': 3,
  242. 'hydra:member': [
  243. {id: 1},
  244. {id: 2},
  245. {id: 3},
  246. ]
  247. }
  248. // @ts-ignore
  249. apiRequestService.get = vi.fn(async (url: string) => collection)
  250. // @ts-ignore
  251. entityManager.newInstance = vi.fn((model: typeof ApiResource, props: object) => {
  252. return new DummyApiResource(props)
  253. })
  254. await entityManager.fetchCollection(DummyApiResource, null, { page: 10 })
  255. expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', { page: 10 })
  256. })
  257. test('with pagination', async () => {
  258. const collection = {
  259. '@type': 'hydra:Collection',
  260. 'hydra:totalItems': 1000,
  261. 'hydra:member': [],
  262. 'hydra:view': {
  263. "@id": "/api/subdomains?organization=498&page=50",
  264. 'hydra:first': '/api/subdomains?organization=498&page=1',
  265. 'hydra:last': '/api/subdomains?organization=498&page=100',
  266. 'hydra:next': '/api/subdomains?organization=498&page=51',
  267. 'hydra:previous': '/api/subdomains?organization=498&page=49'
  268. }
  269. }
  270. // @ts-ignore
  271. apiRequestService.get = vi.fn(async (url: string) => collection)
  272. const result = await entityManager.fetchCollection(DummyApiResource, null)
  273. expect(result.totalItems).toEqual(1000)
  274. expect(result.pagination.first).toEqual(1)
  275. expect(result.pagination.last).toEqual(100)
  276. expect(result.pagination.previous).toEqual(49)
  277. expect(result.pagination.next).toEqual(51)
  278. })
  279. })
  280. describe('persist', () => {
  281. test('new entity (POST)', async () => {
  282. const instance = new DummyApiModel({id: 'tmp1', name: 'bob'})
  283. instance.isNew = vi.fn(() => true)
  284. // @ts-ignore
  285. instance.$toJson = vi.fn(() => {
  286. return {id: 'tmp1', name: 'bob'}
  287. })
  288. // TODO: attendre de voir si cet appel est nécessaire dans l'entity manager
  289. // entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
  290. const response = { id: 1, name: 'bob' }
  291. // @ts-ignore
  292. apiRequestService.post = vi.fn((url, data) => response)
  293. // @ts-ignore
  294. entityManager.newInstance = vi.fn((model, response) => {
  295. const newEntity = new DummyApiModel(response)
  296. // @ts-ignore
  297. newEntity.id = response.id
  298. // @ts-ignore
  299. newEntity.name = response.name
  300. return newEntity
  301. })
  302. // @ts-ignore
  303. entityManager.removeTempAfterPersist = vi.fn()
  304. const result = await entityManager.persist(DummyApiModel, instance)
  305. // temp id should have been purged from the posted data
  306. expect(apiRequestService.post).toHaveBeenCalledWith('api/dummyModel', {name: 'bob'})
  307. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, response)
  308. // @ts-ignore
  309. expect(entityManager.removeTempAfterPersist).toHaveBeenCalledWith(DummyApiModel, instance.id)
  310. expect(result.id).toEqual(1)
  311. expect(result.name).toEqual('bob')
  312. })
  313. test('existing entity (PUT)', async () => {
  314. const props = {id: 1, name: 'bob'}
  315. const entity = new DummyApiModel(props)
  316. entity.id = 1
  317. entity.isNew = vi.fn(() => false)
  318. // @ts-ignore
  319. entity.$toJson = vi.fn(() => props)
  320. // TODO: attendre de voir si cet appel est nécessaire dans l'entity manager
  321. // entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
  322. // @ts-ignore
  323. apiRequestService.put = vi.fn((url, data) => props)
  324. // @ts-ignore
  325. entityManager.newInstance = vi.fn((model, response) => {
  326. const newEntity = new DummyApiModel(response)
  327. // @ts-ignore
  328. newEntity.id = response.id
  329. // @ts-ignore
  330. newEntity.name = response.name
  331. return newEntity
  332. })
  333. // @ts-ignore
  334. entityManager.removeTempAfterPersist = vi.fn()
  335. const result = await entityManager.persist(DummyApiModel, entity)
  336. expect(apiRequestService.put).toHaveBeenCalledWith('api/dummyModel/1', {id: 1, name: 'bob'})
  337. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, props)
  338. // @ts-ignore
  339. expect(entityManager.removeTempAfterPersist).toHaveBeenCalledTimes(0)
  340. expect(result.id).toEqual(1)
  341. expect(result.name).toEqual('bob')
  342. })
  343. })
  344. describe('patch', () => {
  345. test('simple call', async () => {
  346. const props = {id: 1, name: 'bobby'}
  347. // @ts-ignore
  348. apiRequestService.put = vi.fn((url, data) => props)
  349. // @ts-ignore
  350. entityManager.newInstance = vi.fn((model, response) => {
  351. const newEntity = new DummyApiModel(response)
  352. // @ts-ignore
  353. newEntity.id = response.id
  354. // @ts-ignore
  355. newEntity.name = response.name
  356. return newEntity
  357. })
  358. const result = await entityManager.patch(DummyApiModel, 1, {name: 'bobby'})
  359. expect(apiRequestService.put).toHaveBeenCalledWith('api/dummyModel/1', '{"name":"bobby"}')
  360. expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiModel, {id: 1, name: 'bobby'})
  361. expect(result.id).toEqual(1)
  362. expect(result.name).toEqual('bobby')
  363. })
  364. })
  365. describe('delete', () => {
  366. test('delete non persisted entity', () => {
  367. const entity = new DummyApiModel()
  368. entity.isNew = vi.fn(() => true)
  369. entity.id = 'tmp123'
  370. // @ts-ignore
  371. const repo = vi.fn() as Repository<ApiResource>
  372. // @ts-ignore
  373. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  374. return model === DummyApiModel ? repo : null
  375. })
  376. apiRequestService.delete = vi.fn()
  377. // @ts-ignore
  378. repo.destroy = vi.fn((id: number) => null)
  379. entityManager.delete(DummyApiModel, entity)
  380. expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
  381. expect(apiRequestService.delete).toHaveBeenCalledTimes(0)
  382. expect(repo.destroy).toHaveBeenCalledWith('tmp123')
  383. })
  384. test('delete persisted entity', async () => {
  385. const entity = new DummyApiModel()
  386. entity.isNew = vi.fn(() => false)
  387. entity.id = 1
  388. // @ts-ignore
  389. const repo = vi.fn() as Repository<ApiResource>
  390. // @ts-ignore
  391. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  392. return model === DummyApiModel ? repo : null
  393. })
  394. // @ts-ignore
  395. apiRequestService.delete = vi.fn((id: number) => null)
  396. // @ts-ignore
  397. repo.destroy = vi.fn((id: number) => null)
  398. await entityManager.delete(DummyApiModel, entity)
  399. expect(entityManager.getRepository).toHaveBeenCalledWith(DummyApiModel)
  400. expect(apiRequestService.delete).toHaveBeenCalledWith('api/dummyModel/1')
  401. expect(repo.destroy).toHaveBeenCalledWith(1)
  402. })
  403. })
  404. describe('reset', () => {
  405. test('simple call', () => {
  406. const entity = new DummyApiModel()
  407. entity.id = 1
  408. entity.name = 'paul'
  409. const initialEntity = new DummyApiModel()
  410. initialEntity.id = 1
  411. initialEntity.name = 'serges'
  412. entityManager.getInitialStateOf = vi.fn((model: typeof ApiResource, id: string | number) => initialEntity)
  413. // @ts-ignore
  414. const repo = vi.fn() as Repository<ApiResource>
  415. // @ts-ignore
  416. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  417. return model === DummyApiModel ? repo : null
  418. })
  419. // @ts-ignore
  420. repo.save = vi.fn((data: any) => null)
  421. const result = entityManager.reset(DummyApiModel, entity)
  422. expect(entityManager.getInitialStateOf).toHaveBeenCalledWith(DummyApiModel, 1)
  423. expect(repo.save).toHaveBeenCalledWith(initialEntity)
  424. expect(result).toEqual(initialEntity)
  425. })
  426. test('no initial state stored', () => {
  427. const entity = new DummyApiModel()
  428. entity.id = 1
  429. entityManager.getInitialStateOf = vi.fn((model: typeof ApiResource, id: string | number) => null)
  430. expect(() => entityManager.reset(DummyApiModel, entity)).toThrowError(
  431. 'no initial state recorded for this object - abort [dummyModel/1]'
  432. )
  433. })
  434. })
  435. describe('flush', () => {
  436. test('simple call', () => {
  437. // @ts-ignore
  438. const repo = vi.fn() as Repository<ApiResource>
  439. // @ts-ignore
  440. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  441. return model === DummyApiModel ? repo : null
  442. })
  443. repo.flush = vi.fn()
  444. entityManager.flush(DummyApiModel)
  445. expect(repo.flush).toHaveBeenCalled()
  446. })
  447. })
  448. describe('isNewEntity', () => {
  449. test('with new entity', () => {
  450. const entity = new DummyApiModel()
  451. entity.isNew = vi.fn(() => true)
  452. // @ts-ignore
  453. const repo = vi.fn() as Repository<ApiResource>
  454. // @ts-ignore
  455. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  456. return model === DummyApiModel ? repo : null
  457. })
  458. // @ts-ignore
  459. repo.find = vi.fn((id: number) => entity)
  460. const result = entityManager.isNewInstance(DummyApiModel, 1)
  461. expect(result).toBeTruthy()
  462. })
  463. test('with existing entity', () => {
  464. const entity = new DummyApiModel()
  465. entity.isNew = vi.fn(() => false)
  466. // @ts-ignore
  467. const repo = vi.fn() as Repository<ApiResource>
  468. // @ts-ignore
  469. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  470. return model === DummyApiModel ? repo : null
  471. })
  472. // @ts-ignore
  473. repo.find = vi.fn((id: number) => entity)
  474. const result = entityManager.isNewInstance(DummyApiModel, 1)
  475. expect(result).toBeFalsy()
  476. })
  477. test('non-existing entity', () => {
  478. // @ts-ignore
  479. const repo = vi.fn() as Repository<ApiResource>
  480. // @ts-ignore
  481. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  482. return model === DummyApiModel ? repo : null
  483. })
  484. // @ts-ignore
  485. repo.find = vi.fn((id: number) => null)
  486. console.error = vi.fn()
  487. const result = entityManager.isNewInstance(DummyApiModel, 1)
  488. expect(result).toBeFalsy()
  489. expect(console.error).toHaveBeenCalledWith('dummyModel/1 does not exist!')
  490. })
  491. })
  492. describe('saveInitialState', () => {
  493. test('simple call', () => {
  494. // @ts-ignore
  495. const entity = { id: 1, name: 'bob' } as DummyApiResource
  496. // @ts-ignore
  497. const repo = vi.fn() as Repository<ApiResource>
  498. // @ts-ignore
  499. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  500. return model === DummyApiResource ? repo : null
  501. })
  502. // @ts-ignore
  503. repo.save = vi.fn((record: Element) => null)
  504. entityManager.saveInitialState(DummyApiResource, entity)
  505. expect(repo.save).toHaveBeenCalledWith({ id: '_clone_1', name: 'bob' })
  506. expect(entity.id).toEqual(1)
  507. })
  508. })
  509. describe('getInitialStateOf', () => {
  510. test('with initial state', () => {
  511. // @ts-ignore
  512. const entity = { id: 1, name: 'bob' } as DummyApiResource
  513. // @ts-ignore
  514. const repo = vi.fn() as Repository<ApiResource>
  515. // @ts-ignore
  516. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  517. return model === DummyApiResource ? repo : null
  518. })
  519. // @ts-ignore
  520. repo.find = vi.fn((id: number | string) => {
  521. // @ts-ignore
  522. return { id: 1, name: 'robert' } as DummyApiResource
  523. })
  524. const result = entityManager.getInitialStateOf(DummyApiResource, 1) as DummyApiResource
  525. expect(repo.find).toHaveBeenCalledWith('_clone_1')
  526. expect(result.id).toEqual(1)
  527. expect(result.name).toEqual('robert')
  528. })
  529. test('without initial state', () => {
  530. // @ts-ignore
  531. const repo = vi.fn() as Repository<ApiResource>
  532. // @ts-ignore
  533. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  534. return model === DummyApiResource ? repo : null
  535. })
  536. // @ts-ignore
  537. repo.find = vi.fn((id: number | string) => null)
  538. const result = entityManager.getInitialStateOf(DummyApiResource, 1) as DummyApiResource
  539. expect(repo.find).toHaveBeenCalledWith('_clone_1')
  540. expect(result).toEqual(null)
  541. })
  542. })
  543. describe('removeTempAfterPersist', () => {
  544. test('simple call', () => {
  545. // @ts-ignore
  546. const entity = new DummyApiResource()
  547. entity.id = 'tmp123'
  548. entity.isNew = vi.fn(() => true)
  549. // @ts-ignore
  550. const repo = vi.fn() as Repository<ApiResource>
  551. // @ts-ignore
  552. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  553. return model === DummyApiResource ? repo : null
  554. })
  555. // @ts-ignore
  556. repo.find = vi.fn((id: number | string) => entity)
  557. // @ts-ignore
  558. repo.destroy = vi.fn()
  559. entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')
  560. expect(repo.destroy).toHaveBeenCalledWith('tmp123')
  561. expect(repo.destroy).toHaveBeenCalledWith('_clone_tmp123')
  562. })
  563. test('entity is not temporary', () => {
  564. // @ts-ignore
  565. const entity = new DummyApiResource()
  566. entity.id = 1
  567. entity.isNew = vi.fn(() => false)
  568. // @ts-ignore
  569. const repo = vi.fn() as Repository<ApiResource>
  570. // @ts-ignore
  571. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  572. return model === DummyApiResource ? repo : null
  573. })
  574. // @ts-ignore
  575. repo.find = vi.fn((id: number | string) => entity)
  576. // @ts-ignore
  577. repo.destroy = vi.fn()
  578. // @ts-ignore
  579. expect(() => entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')).toThrowError(
  580. 'Error: Can not remove a non-temporary model instance'
  581. )
  582. expect(repo.destroy).toHaveBeenCalledTimes(0)
  583. })
  584. test('entity does not exist', () => {
  585. // @ts-ignore
  586. const repo = vi.fn() as Repository<ApiResource>
  587. // @ts-ignore
  588. entityManager.getRepository = vi.fn((model: typeof ApiResource) => {
  589. return model === DummyApiResource ? repo : null
  590. })
  591. // @ts-ignore
  592. repo.find = vi.fn((id: number | string) => null)
  593. // @ts-ignore
  594. repo.destroy = vi.fn()
  595. console.error = vi.fn()
  596. entityManager.removeTempAfterPersist(DummyApiResource, 'tmp123')
  597. expect(repo.destroy).toHaveBeenCalledTimes(0)
  598. expect(console.error).toHaveBeenCalledWith('dummyResource/tmp123 does not exist!')
  599. })
  600. })