Skip to content

Commit 2d21bf4

Browse files
authored
Merge pull request #10746 from marmelab/fix-useInfiniteListController-meta
Fix `useInfiniteListController` does not return response `meta`
2 parents 955bf60 + 1b80acc commit 2d21bf4

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

packages/ra-core/src/controller/list/useInfiniteListController.spec.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,38 @@ describe('useInfiniteListController', () => {
740740
expect(authProvider.canAccess).not.toHaveBeenCalled();
741741
});
742742
});
743+
744+
describe('response metadata', () => {
745+
it('should return response metadata as meta', async () => {
746+
const getList = jest.fn().mockImplementation(() =>
747+
Promise.resolve({
748+
data: [],
749+
total: 0,
750+
meta: { foo: 'bar' },
751+
})
752+
);
753+
const dataProvider = testDataProvider({ getList });
754+
const children = jest.fn().mockReturnValue(<span>children</span>);
755+
const props = {
756+
...defaultProps,
757+
children,
758+
};
759+
render(
760+
<CoreAdminContext dataProvider={dataProvider}>
761+
<InfiniteListController {...props} />
762+
</CoreAdminContext>
763+
);
764+
await waitFor(() => {
765+
expect(children).toHaveBeenCalledWith(
766+
expect.objectContaining({
767+
page: 1,
768+
total: 0,
769+
hasNextPage: false,
770+
hasPreviousPage: false,
771+
meta: { foo: 'bar' },
772+
})
773+
);
774+
});
775+
});
776+
});
743777
});

packages/ra-core/src/controller/list/useInfiniteListController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const useInfiniteListController = <
111111
fetchPreviousPage,
112112
isFetchingPreviousPage,
113113
refetch,
114+
meta: responseMeta,
114115
} = useInfiniteGetList<RecordType, ErrorType>(
115116
resource,
116117
{
@@ -223,6 +224,7 @@ export const useInfiniteListController = <
223224
isFetchingNextPage,
224225
fetchPreviousPage,
225226
isFetchingPreviousPage,
227+
meta: responseMeta,
226228
} as InfiniteListControllerResult<RecordType, ErrorType>;
227229
};
228230

packages/ra-core/src/controller/list/useListController.spec.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
screen,
88
act,
99
} from '@testing-library/react';
10+
import { onlineManager } from '@tanstack/react-query';
1011
import { testDataProvider } from '../../dataProvider';
1112
import { memoryStore } from '../../store';
1213
import { CoreAdminContext } from '../../core';
@@ -35,6 +36,10 @@ describe('useListController', () => {
3536
debounce: 200,
3637
};
3738

39+
beforeEach(() => {
40+
onlineManager.setOnline(true);
41+
});
42+
3843
describe('queryOptions', () => {
3944
it('should accept custom client query options', async () => {
4045
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
@@ -719,4 +724,38 @@ describe('useListController', () => {
719724
).toBeNull();
720725
});
721726
});
727+
728+
describe('response metadata', () => {
729+
it('should return response metadata as meta', async () => {
730+
const getList = jest.fn().mockImplementation(() =>
731+
Promise.resolve({
732+
data: [],
733+
total: 0,
734+
meta: { foo: 'bar' },
735+
})
736+
);
737+
const dataProvider = testDataProvider({ getList });
738+
const children = jest.fn().mockReturnValue(<span>children</span>);
739+
const props = {
740+
...defaultProps,
741+
children,
742+
};
743+
render(
744+
<CoreAdminContext dataProvider={dataProvider}>
745+
<ListController {...props} />
746+
</CoreAdminContext>
747+
);
748+
await waitFor(() => {
749+
expect(children).toHaveBeenCalledWith(
750+
expect.objectContaining({
751+
page: 1,
752+
total: 0,
753+
hasNextPage: false,
754+
hasPreviousPage: false,
755+
meta: { foo: 'bar' },
756+
})
757+
);
758+
});
759+
});
760+
});
722761
});

packages/ra-core/src/dataProvider/useInfiniteGetList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,5 @@ export type UseInfiniteGetListHookValue<
275275
> & {
276276
total?: number;
277277
pageParam?: number;
278+
meta?: any;
278279
};

0 commit comments

Comments
 (0)