Skip to content

Commit ccfbeb9

Browse files
authored
Merge pull request #5524 from marmelab/fix-useDataProvider-no-aguments
Fix useDataProvider throws options is undefined error when called without arguments
2 parents ad910bf + 6b33557 commit ccfbeb9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/ra-core/src/dataProvider/getDataProviderCallArguments.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const OptionsProperties = [
1111
];
1212

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

1617
return Object.keys(options).some(key => OptionsProperties.includes(key));

packages/ra-core/src/dataProvider/useDataProvider.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@ describe('useDataProvider', () => {
154154
expect(customVerb).toHaveBeenCalledWith('posts', { id: 1 });
155155
});
156156

157+
it('should accept calls to custom verbs with no arguments', async () => {
158+
const UseCustomVerbWithNoArgument = ({ onSuccess }) => {
159+
const [data, setData] = useState();
160+
const [error, setError] = useState();
161+
const dataProvider = useDataProvider();
162+
useEffect(() => {
163+
dataProvider
164+
.customVerb()
165+
.then(res => setData(res.data))
166+
.catch(e => setError(e));
167+
}, [dataProvider, onSuccess]);
168+
if (error) return <div data-testid="error">{error.message}</div>;
169+
if (data)
170+
return <div data-testid="data">{JSON.stringify(data)}</div>;
171+
return <div data-testid="loading">loading</div>;
172+
};
173+
const customVerb = jest.fn(() => Promise.resolve({ data: null }));
174+
const dataProvider = { customVerb };
175+
renderWithRedux(
176+
<DataProviderContext.Provider value={dataProvider}>
177+
<UseCustomVerbWithNoArgument />
178+
</DataProviderContext.Provider>
179+
);
180+
// wait for the dataProvider to return
181+
await act(async () => {
182+
await new Promise(resolve => setTimeout(resolve));
183+
});
184+
185+
expect(customVerb).toHaveBeenCalledWith();
186+
});
187+
157188
it('should accept custom arguments for custom verbs', async () => {
158189
const customVerb = jest.fn(() => Promise.resolve({ data: null }));
159190
const dataProvider = { customVerb };

0 commit comments

Comments
 (0)