entityManager.test.ts 25 KB

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