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 useDataProvider throws options is undefined error when called without arguments #5524

Merged
merged 1 commit into from
Nov 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const OptionsProperties = [
];

const isDataProviderOptions = (value: any) => {
if (typeof value === 'undefined') return [];
let options = value as UseDataProviderOptions;

return Object.keys(options).some(key => OptionsProperties.includes(key));
Expand Down
31 changes: 31 additions & 0 deletions packages/ra-core/src/dataProvider/useDataProvider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ describe('useDataProvider', () => {
expect(customVerb).toHaveBeenCalledWith('posts', { id: 1 });
});

it('should accept calls to custom verbs with no arguments', async () => {
const UseCustomVerbWithNoArgument = ({ onSuccess }) => {
const [data, setData] = useState();
const [error, setError] = useState();
const dataProvider = useDataProvider();
useEffect(() => {
dataProvider
.customVerb()
.then(res => setData(res.data))
.catch(e => setError(e));
}, [dataProvider, onSuccess]);
if (error) return <div data-testid="error">{error.message}</div>;
if (data)
return <div data-testid="data">{JSON.stringify(data)}</div>;
return <div data-testid="loading">loading</div>;
};
const customVerb = jest.fn(() => Promise.resolve({ data: null }));
const dataProvider = { customVerb };
renderWithRedux(
<DataProviderContext.Provider value={dataProvider}>
<UseCustomVerbWithNoArgument />
</DataProviderContext.Provider>
);
// wait for the dataProvider to return
await act(async () => {
await new Promise(resolve => setTimeout(resolve));
});

expect(customVerb).toHaveBeenCalledWith();
});

it('should accept custom arguments for custom verbs', async () => {
const customVerb = jest.fn(() => Promise.resolve({ data: null }));
const dataProvider = { customVerb };
Expand Down