Skip to content

Commit

Permalink
Merge pull request #7731 from marmelab/fix-use-configure-admin-router…
Browse files Browse the repository at this point in the history
…-from-children

Fix useConfigureAdminRouterFromChildren when child function return null
  • Loading branch information
fzaninotto committed May 23, 2022
2 parents b1c1236 + 8799e72 commit a8bea15
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ const TestedComponent = ({ role }) => {
);
};

const TestedComponentReturningNull = ({ role }) => {
const history = createMemoryHistory();

return (
<CoreAdminContext history={history}>
<CoreAdminRoutes
layout={MyLayout}
catchAll={CatchAll}
loading={Loading}
>
<Resource name="posts" />
<Resource name="comments" />
{() =>
role === 'admin'
? [<Resource name="user" />, <Resource name="admin" />]
: role === 'user'
? [<Resource name="user" />]
: null
}
</CoreAdminRoutes>
</CoreAdminContext>
);
};

const ResourceWithPermissions = (props: ResourceProps) => (
<Resource {...props} />
);
Expand Down Expand Up @@ -168,6 +192,12 @@ describe('useConfigureAdminRouterFromChildren', () => {
expectResource('user').not.toBeNull();
expectResource('admin').not.toBeNull();
});
it('should accept function returning null', async () => {
render(<TestedComponentReturningNull role="admin" />);
await waitFor(() => expect(screen.queryByText('Loading')).toBeNull());
expectResource('user').not.toBeNull();
expectResource('admin').not.toBeNull();
});
it('should call registerResource with the permissions', async () => {
render(<TestedComponentWithPermissions />);
await waitFor(() => expect(screen.queryByText('Loading')).toBeNull());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const useRoutesAndResourcesFromChildren = (
) => {
try {
const childrenFuncResult = childFunc(permissions);
if ((childrenFuncResult as Promise<ReactNode>).then) {
if ((childrenFuncResult as Promise<ReactNode>)?.then) {
(childrenFuncResult as Promise<ReactNode>).then(
resolvedChildren => {
mergeRoutesAndResources(
Expand Down

0 comments on commit a8bea15

Please sign in to comment.