|
@@ -324,12 +324,14 @@ describe('fetchCollection', () => {
|
|
|
},
|
|
},
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+ const piniaOrmQuery = vi.fn()
|
|
|
|
|
+
|
|
|
|
|
+ // @ts-ignore
|
|
|
|
|
+ entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
|
|
|
|
|
+
|
|
|
const result = await entityManager.fetchCollection(DummyApiResource, null)
|
|
const result = await entityManager.fetchCollection(DummyApiResource, null)
|
|
|
|
|
|
|
|
- expect(apiRequestService.get).toHaveBeenCalledWith(
|
|
|
|
|
- 'api/dummyResource',
|
|
|
|
|
- null,
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource')
|
|
|
expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
|
|
expect(entityManager.newInstance).toHaveBeenCalledTimes(3)
|
|
|
expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
|
|
expect(entityManager.newInstance).toHaveBeenCalledWith(DummyApiResource, {
|
|
|
id: 1,
|
|
id: 1,
|
|
@@ -347,13 +349,20 @@ describe('fetchCollection', () => {
|
|
|
_model: undefined,
|
|
_model: undefined,
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- expect(result.items).toEqual([
|
|
|
|
|
|
|
+ // @ts-ignore
|
|
|
|
|
+ piniaOrmQuery.get = vi.fn(() => [
|
|
|
new DummyApiResource({ id: 1 }),
|
|
new DummyApiResource({ id: 1 }),
|
|
|
new DummyApiResource({ id: 2 }),
|
|
new DummyApiResource({ id: 2 }),
|
|
|
new DummyApiResource({ id: 3 }),
|
|
new DummyApiResource({ id: 3 }),
|
|
|
])
|
|
])
|
|
|
|
|
|
|
|
- expect(result.pagination, 'default pagination').toEqual({
|
|
|
|
|
|
|
+ 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,
|
|
first: 1,
|
|
|
last: 1,
|
|
last: 1,
|
|
|
next: undefined,
|
|
next: undefined,
|
|
@@ -371,6 +380,11 @@ describe('fetchCollection', () => {
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
apiRequestService.get = vi.fn(async (url: string) => collection)
|
|
apiRequestService.get = vi.fn(async (url: string) => collection)
|
|
|
|
|
|
|
|
|
|
+ const piniaOrmQuery = vi.fn()
|
|
|
|
|
+
|
|
|
|
|
+ // @ts-ignore
|
|
|
|
|
+ entityManager.getQuery = vi.fn((model: typeof ApiResource) => piniaOrmQuery)
|
|
|
|
|
+
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
entityManager.newInstance = vi.fn(
|
|
entityManager.newInstance = vi.fn(
|
|
|
(model: typeof ApiResource, props: object) => {
|
|
(model: typeof ApiResource, props: object) => {
|
|
@@ -386,7 +400,6 @@ describe('fetchCollection', () => {
|
|
|
|
|
|
|
|
expect(apiRequestService.get).toHaveBeenCalledWith(
|
|
expect(apiRequestService.get).toHaveBeenCalledWith(
|
|
|
'api/dummyModel/100/dummyResource',
|
|
'api/dummyModel/100/dummyResource',
|
|
|
- null,
|
|
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -397,6 +410,19 @@ describe('fetchCollection', () => {
|
|
|
'hydra:member': [{ id: 1 }, { id: 2 }, { id: 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
|
|
// @ts-ignore
|
|
|
apiRequestService.get = vi.fn(async (url: string) => collection)
|
|
apiRequestService.get = vi.fn(async (url: string) => collection)
|
|
|
|
|
|
|
@@ -407,37 +433,16 @@ describe('fetchCollection', () => {
|
|
|
},
|
|
},
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
- await entityManager.fetchCollection(DummyApiResource, null, { page: 10 })
|
|
|
|
|
-
|
|
|
|
|
- expect(apiRequestService.get).toHaveBeenCalledWith('api/dummyResource', {
|
|
|
|
|
- page: 10,
|
|
|
|
|
- })
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- test('with pagination', async () => {
|
|
|
|
|
- const collection = {
|
|
|
|
|
- '@type': 'hydra:Collection',
|
|
|
|
|
- 'hydra:totalItems': 1000,
|
|
|
|
|
- 'hydra:member': [],
|
|
|
|
|
- 'hydra:view': {
|
|
|
|
|
- '@id': '/api/subdomains?organization=498&page=50',
|
|
|
|
|
- 'hydra:first': '/api/subdomains?organization=498&page=1',
|
|
|
|
|
- 'hydra:last': '/api/subdomains?organization=498&page=100',
|
|
|
|
|
- 'hydra:next': '/api/subdomains?organization=498&page=51',
|
|
|
|
|
- 'hydra:previous': '/api/subdomains?organization=498&page=49',
|
|
|
|
|
- },
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
- apiRequestService.get = vi.fn(async (url: string) => collection)
|
|
|
|
|
-
|
|
|
|
|
- const result = await entityManager.fetchCollection(DummyApiResource, null)
|
|
|
|
|
|
|
+ await entityManager.fetchCollection(DummyApiResource, null, query)
|
|
|
|
|
|
|
|
- expect(result.totalItems).toEqual(1000)
|
|
|
|
|
- expect(result.pagination.first).toEqual(1)
|
|
|
|
|
- expect(result.pagination.last).toEqual(100)
|
|
|
|
|
- expect(result.pagination.previous).toEqual(49)
|
|
|
|
|
- expect(result.pagination.next).toEqual(51)
|
|
|
|
|
|
|
+ expect(apiRequestService.get).toHaveBeenCalledWith(
|
|
|
|
|
+ 'api/dummyResource?foo=bar',
|
|
|
|
|
+ )
|
|
|
|
|
+ // @ts-ignore
|
|
|
|
|
+ expect(query.getUrlQuery).toHaveBeenCalledWith()
|
|
|
|
|
+ // @ts-ignore
|
|
|
|
|
+ expect(query.applyToPiniaOrmQuery).toHaveBeenCalledWith(piniaOrmQuery)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
|
|
|