Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix <Admin> calls its child function without permissions while loading them #7737

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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