Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix Dashboard components cannot use useGetOne hook #4988

Merged
merged 2 commits into from
Jun 29, 2020
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
89 changes: 50 additions & 39 deletions packages/ra-core/src/core/RoutesWithLayout.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as React from 'react';
import expect from 'expect';
import { Route, MemoryRouter } from 'react-router-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import { render, cleanup } from '@testing-library/react';
import { useDispatch } from 'react-redux';
import { waitForDomChange, cleanup } from '@testing-library/react';

import RoutesWithLayout from './RoutesWithLayout';
import renderWithRedux from '../util/renderWithRedux';
import { registerResource } from '../actions';

describe('<RoutesWithLayout>', () => {
afterEach(cleanup);
Expand All @@ -15,52 +17,62 @@ describe('<RoutesWithLayout>', () => {
const Resource = ({ name }) => <div>Resource</div>;

// the Provider is required because the dashboard is wrapped by <Authenticated>, which is a connected component
const store = createStore(() => ({
admin: {},
const initialState = {
admin: { resources: {} },
router: { location: { pathname: '/' } },
}));
};

it('should show dashboard on / when provided', () => {
const { queryByText } = render(
<Provider store={store}>
it('should show dashboard on / when provided', async () => {
const App = () => {
const dispatch = useDispatch();
React.useEffect(() => {
setTimeout(
() => dispatch(registerResource({ name: 'foo' })),
10
);
}, [dispatch]);
return (
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout dashboard={Dashboard}>
<FirstResource name="default" />
<Resource name="another" />
<Resource name="yetanother" />
</RoutesWithLayout>
</MemoryRouter>
</Provider>
);
);
};
const { queryByText } = renderWithRedux(<App />, initialState);

// dashboard should be hidden on first render, until the resources register
expect(queryByText('Dashboard')).toBeNull();
// then it shows once the resources have registered
await waitForDomChange();
expect(queryByText('Dashboard')).not.toBeNull();
});

it('should show the first resource on / when there is only one resource and no dashboard', () => {
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
<FirstResource name="default" />
</RoutesWithLayout>
</MemoryRouter>
</Provider>
const { queryByText } = renderWithRedux(
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
<FirstResource name="default" />
</RoutesWithLayout>
</MemoryRouter>,
initialState
);

expect(queryByText('Default')).not.toBeNull();
});

it('should show the first resource on / when there are multiple resource and no dashboard', () => {
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
<FirstResource name="default" />
<Resource name="another" />
<Resource name="yetanother" />
</RoutesWithLayout>
</MemoryRouter>
</Provider>
const { queryByText } = renderWithRedux(
<MemoryRouter initialEntries={['/']}>
<RoutesWithLayout>
<FirstResource name="default" />
<Resource name="another" />
<Resource name="yetanother" />
</RoutesWithLayout>
</MemoryRouter>,
initialState
);

expect(queryByText('Default')).not.toBeNull();
Expand All @@ -71,16 +83,15 @@ describe('<RoutesWithLayout>', () => {
const customRoutes = [
<Route key="custom" path="/custom" component={Custom} />,
]; // eslint-disable-line react/jsx-key
const { queryByText } = render(
<Provider store={store}>
<MemoryRouter initialEntries={['/custom']}>
<RoutesWithLayout customRoutes={customRoutes}>
<FirstResource name="default" />
<Resource name="another" />
<Resource name="yetanother" />
</RoutesWithLayout>
</MemoryRouter>
</Provider>
const { queryByText } = renderWithRedux(
<MemoryRouter initialEntries={['/custom']}>
<RoutesWithLayout customRoutes={customRoutes}>
<FirstResource name="default" />
<Resource name="another" />
<Resource name="yetanother" />
</RoutesWithLayout>
</MemoryRouter>,
initialState
);

expect(queryByText('Custom')).not.toBeNull();
Expand Down
38 changes: 25 additions & 13 deletions packages/ra-core/src/core/RoutesWithLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React, {
FunctionComponent,
} from 'react';
import { Redirect, Route, Switch } from 'react-router-dom';
import { useSelector } from 'react-redux';

import WithPermissions from '../auth/WithPermissions';
import {
Expand All @@ -13,6 +14,7 @@ import {
CatchAllComponent,
TitleComponent,
DashboardComponent,
ReduxState,
} from '../types';

interface Props {
Expand All @@ -35,6 +37,12 @@ const RoutesWithLayout: FunctionComponent<Props> = ({
childrenAsArray.length > 0
? (childrenAsArray[0] as React.ReactElement<any>)
: null;
// In order to let the Dashboard component use Redux-based dataProvider
// hooks like useGetOne, we must wait for the resource registration before
// displaying the dashboard.
const resourcesAreRegistered = useSelector(
(state: ReduxState) => Object.keys(state.admin.resources).length > 0
);

return (
<Switch>
Expand All @@ -55,19 +63,23 @@ const RoutesWithLayout: FunctionComponent<Props> = ({
/>
))}
{dashboard ? (
<Route
exact
path="/"
render={routeProps => (
<WithPermissions
authParams={{
route: 'dashboard',
}}
component={dashboard}
{...routeProps}
/>
)}
/>
resourcesAreRegistered ? (
<Route
exact
path="/"
render={routeProps => (
<WithPermissions
authParams={{
route: 'dashboard',
}}
component={dashboard}
{...routeProps}
/>
)}
/>
) : (
<></>
)
) : firstChild ? (
<Route
exact
Expand Down