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 useGetManyReference loading state detection #5931

Merged
merged 3 commits into from
Feb 18, 2021
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 @@ -7,6 +7,138 @@ import renderWithRedux from '../../util/renderWithRedux';

describe('<ReferenceManyFieldController />', () => {
it('should set loaded to false when related records are not yet fetched', async () => {
const ComponentToTest = ({ loaded }: { loaded?: boolean }) => {
return <div>loaded: {loaded.toString()}</div>;
};

const { queryByText } = renderWithRedux(
<ReferenceManyFieldController
resource="foo"
source="items"
reference="bar"
target="foo_id"
basePath=""
>
{props => <ComponentToTest {...props} />}
</ReferenceManyFieldController>,
{
admin: {
resources: {
bar: {
data: {
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
},
},
},
references: {
oneToMany: {
'foo_bar@fooId_barId': {},
},
},
},
}
);

expect(queryByText('loaded: false')).not.toBeNull();
await waitFor(() => {
expect(queryByText('loaded: true')).not.toBeNull();
});
});

it('should set loaded to true when related records have been fetched and there are no results', async () => {
const ComponentToTest = ({ loaded }: { loaded?: boolean }) => {
return <div>loaded: {loaded.toString()}</div>;
};

const { queryByText } = renderWithRedux(
<ReferenceManyFieldController
resource="foo"
source="items"
reference="bar"
target="foo_id"
basePath=""
record={{
id: 'fooId',
source: 'barId',
}}
>
{props => <ComponentToTest {...props} />}
</ReferenceManyFieldController>,
{
admin: {
resources: {
bar: {
data: {
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
},
},
},
references: {
oneToMany: {
'foo_bar@fooId_barId': {
ids: [],
},
},
},
},
}
);

expect(queryByText('loaded: false')).not.toBeNull();
await waitFor(() => {
expect(queryByText('loaded: true')).not.toBeNull();
});
});

it('should set loaded to true when related records have been fetched and there are results', async () => {
const ComponentToTest = ({ loaded }: { loaded?: boolean }) => {
return <div>loaded: {loaded.toString()}</div>;
};

const { queryByText } = renderWithRedux(
<ReferenceManyFieldController
resource="foo"
source="items"
reference="bar"
target="foo_id"
basePath=""
record={{
id: 'fooId',
source: 'barId',
}}
>
{props => <ComponentToTest {...props} />}
</ReferenceManyFieldController>,
{
admin: {
resources: {
bar: {
data: {
1: { id: 1, title: 'hello' },
2: { id: 2, title: 'world' },
},
},
},
references: {
oneToMany: {
'foo_bar@fooId_barId': {
ids: [1, 2],
},
},
},
},
}
);

expect(queryByText('loaded: false')).not.toBeNull();
await waitFor(() => {
expect(queryByText('loaded: true')).not.toBeNull();
});
});

it('should call dataProvider.getManyReferences on mount', async () => {
const children = jest.fn().mockReturnValue('children');
const { dispatch } = renderWithRedux(
<ReferenceManyFieldController
Expand All @@ -15,6 +147,10 @@ describe('<ReferenceManyFieldController />', () => {
reference="bar"
target="foo_id"
basePath=""
record={{
id: 'fooId',
source: 'barId',
}}
>
{children}
</ReferenceManyFieldController>,
Expand Down
18 changes: 14 additions & 4 deletions packages/ra-core/src/dataProvider/useGetManyReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
SortPayload,
Identifier,
ReduxState,
Record,
RecordMap,
} from '../types';
import useQueryWithStore from './useQueryWithStore';
import {
Expand Down Expand Up @@ -93,19 +95,20 @@ const useGetManyReference = (
{ ...options, relatedTo, action: CRUD_GET_MANY_REFERENCE },
// ids and data selector
(state: ReduxState) => ({
ids: getIds(state, relatedTo) || defaultIds,
ids: getIds(state, relatedTo),
allRecords: get(
state.admin.resources,
[resource, 'data'],
defaultData
),
}),
(state: ReduxState) => getTotal(state, relatedTo)
(state: ReduxState) => getTotal(state, relatedTo),
isDataLoaded
);

const data = useMemo(
() =>
ids === null
ids == null
? defaultData
: ids
.map(id => allRecords[id])
Expand All @@ -117,7 +120,14 @@ const useGetManyReference = (
[ids, allRecords]
);

return { data, ids, total, error, loading, loaded };
return { data, ids: ids || defaultIds, total, error, loading, loaded };
};

interface DataSelectorResult<RecordType extends Record = Record> {
ids: Identifier[];
allRecords: RecordMap<RecordType>;
}

const isDataLoaded = (data: DataSelectorResult) => data.ids != null;

export default useGetManyReference;