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 useGetMany loaded state #6319

Merged
merged 3 commits into from
Jun 1, 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 @@ -190,14 +190,17 @@ describe('<ReferenceArrayInputController />', () => {
expect(queryByText('ra.input.references.all_missing')).toBeNull();
});

it('should set warning if references fetch fails but selected references are not empty', () => {
it('should set warning if references fetch fails but selected references are not empty', async () => {
const children = jest.fn(({ warning }) => <div>{warning}</div>);
const { queryByText } = renderWithRedux(
<Form
onSubmit={jest.fn()}
render={() => (
<ReferenceArrayInputController
{...defaultProps}
// Avoid global collision in useGetMany with queriesToCall
basePath="/articles"
resource="articles"
input={{ value: [1, 2] }}
>
{children}
Expand All @@ -220,23 +223,30 @@ describe('<ReferenceArrayInputController />', () => {
},
references: {
possibleValues: {
'posts@tag_ids': { error: 'boom' },
'articles@tag_ids': { error: 'boom' },
},
},
},
}
);
expect(queryByText('ra.input.references.many_missing')).not.toBeNull();
await waitFor(() => {
expect(
queryByText('ra.input.references.many_missing')
).not.toBeNull();
});
});

it('should set warning if references were found but selected references are not complete', () => {
it('should set warning if references were found but selected references are not complete', async () => {
const children = jest.fn(({ warning }) => <div>{warning}</div>);
const { queryByText } = renderWithRedux(
<Form
onSubmit={jest.fn()}
render={() => (
<ReferenceArrayInputController
{...defaultProps}
// Avoid global collision in useGetMany with queriesToCall
basePath="/products"
resource="products"
input={{ value: [1, 2] }}
>
{children}
Expand All @@ -259,13 +269,17 @@ describe('<ReferenceArrayInputController />', () => {
},
references: {
possibleValues: {
'posts@tag_ids': [],
'products@tag_ids': [],
},
},
},
}
);
expect(queryByText('ra.input.references.many_missing')).not.toBeNull();
await waitFor(() => {
expect(
queryByText('ra.input.references.many_missing')
).not.toBeNull();
});
});

it('should set warning if references were found but selected references are empty', () => {
Expand All @@ -276,6 +290,9 @@ describe('<ReferenceArrayInputController />', () => {
render={() => (
<ReferenceArrayInputController
{...defaultProps}
// Avoid global collision in useGetMany with queriesToCall
basePath="/posters"
resource="posters"
input={{ value: [1, 2] }}
>
{children}
Expand All @@ -287,7 +304,7 @@ describe('<ReferenceArrayInputController />', () => {
resources: { tags: { data: { 5: {}, 6: {} } } },
references: {
possibleValues: {
'posts@tag_ids': [],
'posters@tag_ids': [],
},
},
},
Expand All @@ -296,7 +313,7 @@ describe('<ReferenceArrayInputController />', () => {
expect(queryByText('ra.input.references.many_missing')).not.toBeNull();
});

it('should not set warning if all references were found', () => {
it('should not set warning if all references were found', async () => {
const children = jest.fn(({ warning }) => <div>{warning}</div>);
const { queryByText } = renderWithRedux(
<Form
Expand Down Expand Up @@ -335,7 +352,9 @@ describe('<ReferenceArrayInputController />', () => {
},
}
);
expect(queryByText('ra.input.references.many_missing')).toBeNull();
await waitFor(() => {
expect(queryByText('ra.input.references.many_missing')).toBeNull();
});
});

it('should call crudGetMatching on mount with default fetch values', async () => {
Expand Down
22 changes: 12 additions & 10 deletions packages/ra-core/src/dataProvider/useGetMany.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,18 @@ describe('useGetMany', () => {
}
);
await waitFor(() => {
expect(hookValue.mock.calls.pop()[0]).toEqual({
data: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
loading: false,
loaded: true,
error: null,
refetch: expect.any(Function),
});
if (hookValue.mock.calls.length > 0) {
expect(hookValue.mock.calls.pop()[0]).toEqual({
data: [
{ id: 1, title: 'foo' },
{ id: 2, title: 'bar' },
],
loading: false,
loaded: true,
error: null,
refetch: expect.any(Function),
});
}
});
});

Expand Down
5 changes: 1 addition & 4 deletions packages/ra-core/src/dataProvider/useGetMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,13 @@ const useGetMany = (
data,
error: null,
loading: ids.length !== 0,
loaded:
ids.length === 0 ||
(data.length !== 0 && !data.includes(undefined)),
loaded: data.length !== 0 && !data.includes(undefined),
refetch,
});
if (!isEqual(state.data, data)) {
setState({
...state,
data,
loaded: true,
});
}
dataProvider = useDataProvider(); // not the best way to pass the dataProvider to a function outside the hook, but I couldn't find a better one
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import expect from 'expect';
import { render, act } from '@testing-library/react';
import { render, act, waitFor } from '@testing-library/react';
import { renderWithRedux } from 'ra-test';
import { MemoryRouter } from 'react-router-dom';
import { ListContextProvider, DataProviderContext } from 'ra-core';
Expand Down Expand Up @@ -241,7 +241,8 @@ describe('<ReferenceArrayField />', () => {
);
expect(queryByText('bar1')).toBeNull();
act(() => resolve());
await new Promise(resolve => setTimeout(resolve)); // wait for loaded to be true
expect(queryByText('bar1')).not.toBeNull();
await waitFor(() => {
expect(queryByText('bar1')).not.toBeNull();
});
});
});
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe('<ReferenceField />', () => {
const dataProvider = {
getMany: jest.fn(() => Promise.reject('boo')),
};
const { getByRole } = renderWithRedux(
const { queryByRole } = renderWithRedux(
// @ts-ignore-line
<DataProviderContext.Provider value={dataProvider}>
<ReferenceField
Expand All @@ -289,8 +289,8 @@ describe('<ReferenceField />', () => {
</DataProviderContext.Provider>
);
await waitFor(() => {
const ErrorIcon = getByRole('presentation', { hidden: true });
expect(ErrorIcon).toBeDefined();
const ErrorIcon = queryByRole('presentation', { hidden: true });
expect(ErrorIcon).not.toBeNull();
expect(ErrorIcon.getAttribute('aria-errormessage')).toBe('boo');
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/field/ReferenceField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ export const ReferenceFieldView: FC<ReferenceFieldViewProps> = props => {
} = props;
const classes = useStyles(props);

if (!loaded) {
return <LinearProgress />;
}
if (error) {
return (
/* eslint-disable jsx-a11y/role-supports-aria-props */
Expand All @@ -211,6 +208,9 @@ export const ReferenceFieldView: FC<ReferenceFieldViewProps> = props => {
/* eslint-enable */
);
}
if (!loaded) {
return <LinearProgress />;
}
if (!referenceRecord) {
return null;
}
Expand Down