Skip to content

Commit 708792b

Browse files
authoredOct 26, 2023
Merge pull request #9395 from KislyakovDS/issues/9388
Fix total in `useInfiniteGetList` for empty results
2 parents dd38211 + dc2c7e5 commit 708792b

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

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

+30
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,34 @@ describe('useInfiniteListController', () => {
515515
});
516516
});
517517
});
518+
519+
it('should return correct total value for empty data', async () => {
520+
const getList = jest.fn().mockImplementation(() =>
521+
Promise.resolve({
522+
data: [],
523+
total: 0,
524+
})
525+
);
526+
const dataProvider = testDataProvider({ getList });
527+
528+
render(
529+
<CoreAdminContext dataProvider={dataProvider}>
530+
<InfiniteListController resource="posts">
531+
{({ total }) => (
532+
<div aria-label="Total value">{String(total)}</div>
533+
)}
534+
</InfiniteListController>
535+
</CoreAdminContext>
536+
);
537+
538+
await waitFor(() => {
539+
const totalDivNode = screen.getByLabelText('Total value');
540+
const totalInnerHTML = totalDivNode.innerHTML;
541+
const totalValue = Number(totalInnerHTML);
542+
543+
expect(totalInnerHTML).not.toEqual('undefined');
544+
expect(totalValue).not.toBeNaN();
545+
expect(totalValue).toEqual(0);
546+
});
547+
});
518548
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const useInfiniteGetList = <RecordType extends RaRecord = any>(
146146
? {
147147
...result,
148148
data: result.data,
149-
total: result.data?.pages[0]?.total || undefined,
149+
total: result.data?.pages[0]?.total ?? undefined,
150150
}
151151
: result) as UseInfiniteQueryResult<
152152
GetInfiniteListResult<RecordType>,

0 commit comments

Comments
 (0)
Please sign in to comment.