Skip to content

Commit 58e7ba3

Browse files
authored
Merge pull request #6886 from marmelab/fix-usemutation-options
Fix useMutation not considering returnPromise option
2 parents 0b49b94 + 37f8476 commit 58e7ba3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/ra-core/src/dataProvider/useMutation.spec.tsx

+43
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Mutation from './Mutation';
66
import { CoreAdmin, Resource } from '../core';
77
import { renderWithRedux } from 'ra-test';
88
import { DataProviderContext } from '.';
9+
import useMutation from './useMutation';
910

1011
describe('useMutation', () => {
1112
it('should pass a callback to trigger the mutation', () => {
@@ -264,4 +265,46 @@ describe('useMutation', () => {
264265
const result = await promise;
265266
expect(result).toMatchObject({ data: { foo: 'bar' } });
266267
});
268+
269+
it('should return a response when returnPromise option is set at definition and the query is passed al callTime', async () => {
270+
const MutationComponent = ({ query = undefined, options, children }) =>
271+
children(...useMutation(query, options));
272+
const dataProvider = {
273+
mytype: jest.fn(() => Promise.resolve({ data: { foo: 'bar' } })),
274+
};
275+
276+
let response = null;
277+
const myPayload = {};
278+
const { getByText, dispatch } = renderWithRedux(
279+
<DataProviderContext.Provider value={dataProvider}>
280+
<MutationComponent options={{ returnPromise: true }}>
281+
{(mutate, { loading }) => (
282+
<button
283+
className={loading ? 'loading' : 'idle'}
284+
onClick={async () =>
285+
(response = await mutate({
286+
type: 'mytype',
287+
resource: 'myresource',
288+
payload: myPayload,
289+
}))
290+
}
291+
>
292+
Hello
293+
</button>
294+
)}
295+
</MutationComponent>
296+
</DataProviderContext.Provider>
297+
);
298+
const buttonElement = getByText('Hello');
299+
fireEvent.click(buttonElement);
300+
const action = dispatch.mock.calls[0][0];
301+
expect(action.type).toEqual('CUSTOM_FETCH');
302+
expect(action.payload).toEqual(myPayload);
303+
expect(action.meta.resource).toEqual('myresource');
304+
await waitFor(() => {
305+
expect(buttonElement.className).toEqual('idle');
306+
});
307+
308+
expect(response).toMatchObject({ data: { foo: 'bar' } });
309+
});
267310
});

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,13 @@ const mergeDefinitionAndCallTimeParameters = (
320320
type: callTimeQuery.type,
321321
resource: callTimeQuery.resource,
322322
payload: callTimeQuery.payload,
323-
options: sanitizeOptions(callTimeOptions),
323+
options: options
324+
? merge(
325+
{},
326+
sanitizeOptions(options),
327+
sanitizeOptions(callTimeOptions)
328+
)
329+
: sanitizeOptions(callTimeOptions),
324330
};
325331
};
326332

0 commit comments

Comments
 (0)