Skip to content

Commit

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

Fix useConfigureAdminRouterFromChildren
  • Loading branch information
fzaninotto authored May 23, 2022
2 parents e698087 + 3f92a02 commit 3fd39fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ const TestedComponentReturningNull = ({ role }) => {
);
};

const TestedComponentWithAuthProvider = ({ callback }) => {
const history = createMemoryHistory();
const authProvider = {
login: () => Promise.resolve(),
logout: () => Promise.resolve(),
checkAuth: () => Promise.resolve(),
checkError: () => Promise.resolve(),
getPermissions: () => Promise.resolve('admin'),
};

return (
<CoreAdminContext history={history} authProvider={authProvider}>
<CoreAdminRoutes
layout={MyLayout}
catchAll={CatchAll}
loading={Loading}
>
<Resource name="posts" />
<Resource name="comments" />
{callback}
</CoreAdminRoutes>
</CoreAdminContext>
);
};

const ResourceWithPermissions = (props: ResourceProps) => (
<Resource {...props} />
);
Expand Down Expand Up @@ -186,6 +211,16 @@ describe('useConfigureAdminRouterFromChildren', () => {
expectResource('user').toBeNull();
expectResource('admin').toBeNull();
});
it('should not call the children function until the permissions have been retrieved', async () => {
const callback = jest.fn(() =>
Promise.resolve(resolve => setTimeout(resolve, 50))
);

render(<TestedComponentWithAuthProvider callback={callback} />);
await waitFor(() => expect(callback).toHaveBeenCalled());

expect(callback).not.toHaveBeenCalledWith(undefined);
});
it('should load dynamic resource definitions', async () => {
render(<TestedComponent role="admin" />);
await waitFor(() => expect(screen.queryByText('Loading')).toBeNull());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext';
export const useConfigureAdminRouterFromChildren = (
children: AdminChildren
): RoutesAndResources & { status: AdminRouterStatus } => {
const { permissions } = usePermissions();
const { permissions, isLoading } = usePermissions();

// Whenever children are updated, update our custom routes and resources
const [routesAndResources, status] = useRoutesAndResourcesFromChildren(
children,
permissions
permissions,
isLoading
);

// Whenever the resources change, we must ensure they're all registered
Expand All @@ -70,7 +71,8 @@ export const useConfigureAdminRouterFromChildren = (
*/
const useRoutesAndResourcesFromChildren = (
children: ReactNode,
permissions: any
permissions: any,
isLoading: boolean
): [RoutesAndResources, AdminRouterStatus] => {
// Gather custom routes and resources that were declared as direct children of CoreAdminRouter
// e.g. Not returned from the child function (if any)
Expand Down Expand Up @@ -136,11 +138,14 @@ const useRoutesAndResourcesFromChildren = (
resolveChildFunction(functionChild);
}
};
updateFromChildren();
if (!isLoading) {
updateFromChildren();
}
}, [
authenticated,
children,
doLogout,
isLoading,
mergeRoutesAndResources,
permissions,
setRoutesAndResources,
Expand Down
4 changes: 4 additions & 0 deletions packages/ra-no-code/src/Admin.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ describe('Admin', () => {
</ApplicationContext.Provider>
);

await waitFor(() => {
screen.getByLabelText('CSV File');
});

userEvents.upload(screen.getByLabelText('CSV File'), file);

await screen.findByDisplayValue('customers');
Expand Down

0 comments on commit 3fd39fe

Please sign in to comment.