|
@@ -1,19 +1,31 @@
|
|
|
-import { describe, test, it, expect } from 'vitest'
|
|
|
|
|
|
|
+import { describe, test, vi, expect, beforeEach, afterEach } from 'vitest'
|
|
|
import EntityManager from "~/services/data/entityManager";
|
|
import EntityManager from "~/services/data/entityManager";
|
|
|
import ApiResource from "~/models/ApiResource";
|
|
import ApiResource from "~/models/ApiResource";
|
|
|
import ApiModel from "~/models/ApiModel";
|
|
import ApiModel from "~/models/ApiModel";
|
|
|
import ApiRequestService from "~/services/data/apiRequestService";
|
|
import ApiRequestService from "~/services/data/apiRequestService";
|
|
|
import {Element, Repository} from "pinia-orm";
|
|
import {Element, Repository} from "pinia-orm";
|
|
|
|
|
+import {Str, Uid} from "pinia-orm/dist/decorators";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DummyApiResource extends ApiResource {
|
|
class DummyApiResource extends ApiResource {
|
|
|
static entity = 'dummyResource'
|
|
static entity = 'dummyResource'
|
|
|
|
|
|
|
|
|
|
+ @Uid()
|
|
|
|
|
+ declare id: number | string
|
|
|
|
|
+
|
|
|
|
|
+ @Str(null)
|
|
|
|
|
+ declare name: string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class DummyApiModel extends ApiModel {
|
|
class DummyApiModel extends ApiModel {
|
|
|
static entity = 'dummyModel'
|
|
static entity = 'dummyModel'
|
|
|
|
|
+
|
|
|
|
|
+ @Uid()
|
|
|
|
|
+ declare id: number | string
|
|
|
|
|
+
|
|
|
|
|
+ @Str(null)
|
|
|
|
|
+ declare name: string
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
let _console: any = {
|
|
let _console: any = {
|
|
@@ -142,7 +154,6 @@ describe('newInstance', () => {
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
const entity = new DummyApiResource(properties)
|
|
const entity = new DummyApiResource(properties)
|
|
|
- entity.setModel = vi.fn((model: typeof ApiResource) => null)
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
repo.make = vi.fn((properties: object) => {
|
|
repo.make = vi.fn((properties: object) => {
|
|
@@ -225,7 +236,7 @@ describe('fetch', () => {
|
|
|
|
|
|
|
|
expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
|
|
expect(entityManager.find).toHaveBeenCalledWith(DummyApiResource, 1)
|
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, properties)
|
|
|
|
|
|
|
+ expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, { id: 1, name: null, _model: undefined})
|
|
|
|
|
|
|
|
expect(result).toEqual(entity)
|
|
expect(result).toEqual(entity)
|
|
|
})
|
|
})
|
|
@@ -264,7 +275,7 @@ describe('fetch', () => {
|
|
|
|
|
|
|
|
expect(entityManager.find).toHaveBeenCalledTimes(0)
|
|
expect(entityManager.find).toHaveBeenCalledTimes(0)
|
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource/1')
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, properties)
|
|
|
|
|
|
|
+ expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, { id: 1, name: null, _model: undefined})
|
|
|
|
|
|
|
|
expect(result).toEqual(entity)
|
|
expect(result).toEqual(entity)
|
|
|
})
|
|
})
|
|
@@ -295,9 +306,9 @@ describe('fetchCollection', () => {
|
|
|
|
|
|
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', null)
|
|
expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', null)
|
|
|
expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
|
|
expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 1})
|
|
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 2})
|
|
|
|
|
- expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {id: 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 })
|
|
|
|
|
|
|
|
expect(result.items).toEqual([
|
|
expect(result.items).toEqual([
|
|
|
new DummyApiResource({id: 1}),
|
|
new DummyApiResource({id: 1}),
|
|
@@ -405,8 +416,8 @@ describe('persist', () => {
|
|
|
return {id: 'tmp1', name: 'bob'}
|
|
return {id: 'tmp1', name: 'bob'}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- // 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
|
|
|
|
|
+ entityManager.cast = vi.fn((model: typeof ApiResource, entity: ApiResource): ApiResource => entity)
|
|
|
|
|
|
|
|
const response = { id: 1, name: 'bob' }
|
|
const response = { id: 1, name: 'bob' }
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|