Skip to content

Commit 25e5fc4

Browse files
committed
Add tests for ListBase render prop and emptyWhileLoading
1 parent 6a92151 commit 25e5fc4

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
AccessControl,
55
DefaultTitle,
66
EmptyWhileLoading,
7+
EmptyWhileLoadingRender,
78
NoAuthProvider,
89
Offline,
910
WithAuthProviderNoAccessControl,
@@ -171,4 +172,10 @@ describe('ListBase', () => {
171172
fireEvent.click(screen.getByText('Resolve books loading'));
172173
await screen.findByText('War and Peace');
173174
});
175+
it('should render nothing while loading if emptyWhileLoading is set to true and using the render prop', async () => {
176+
render(<EmptyWhileLoadingRender />);
177+
expect(screen.queryByText('War and Peace')).toBeNull();
178+
fireEvent.click(screen.getByText('Resolve books loading'));
179+
await screen.findByText('War and Peace');
180+
});
174181
});

packages/ra-core/src/controller/list/ListBase.stories.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,46 @@ export const EmptyWhileLoading = () => {
467467
);
468468
};
469469

470+
export const EmptyWhileLoadingRender = () => {
471+
let resolveGetList: (() => void) | null = null;
472+
const baseProvider = defaultDataProvider(0);
473+
const dataProvider = {
474+
...baseProvider,
475+
getList: (resource, params) => {
476+
return new Promise<GetListResult>(resolve => {
477+
resolveGetList = () =>
478+
resolve(baseProvider.getList(resource, params));
479+
});
480+
},
481+
};
482+
483+
return (
484+
<CoreAdminContext dataProvider={dataProvider}>
485+
<button
486+
onClick={() => {
487+
resolveGetList && resolveGetList();
488+
}}
489+
>
490+
Resolve books loading
491+
</button>
492+
<ListBase
493+
resource="books"
494+
perPage={5}
495+
emptyWhileLoading
496+
render={({ data }) => {
497+
return (
498+
<ul>
499+
{data.map((record: any) => (
500+
<li key={record.id}>{record.title}</li>
501+
))}
502+
</ul>
503+
);
504+
}}
505+
/>
506+
</CoreAdminContext>
507+
);
508+
};
509+
470510
const Title = () => {
471511
const { defaultTitle } = useListContext();
472512
const [locale, setLocale] = useLocaleState();

packages/ra-core/src/controller/list/ListBase.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const ListBase = <RecordType extends RaRecord = any>({
7979
offline !== undefined &&
8080
offline !== false;
8181

82-
const showEmpty = isPending && !showOffline && emptyWhileLoading;
82+
const showEmpty = isPending && !showOffline && emptyWhileLoading === true;
8383

8484
return (
8585
// We pass props.resource here as we don't need to create a new ResourceContext if the props is not provided

0 commit comments

Comments
 (0)