@@ -6,6 +6,7 @@ import Mutation from './Mutation';
6
6
import { CoreAdmin , Resource } from '../core' ;
7
7
import { renderWithRedux } from 'ra-test' ;
8
8
import { DataProviderContext } from '.' ;
9
+ import useMutation from './useMutation' ;
9
10
10
11
describe ( 'useMutation' , ( ) => {
11
12
it ( 'should pass a callback to trigger the mutation' , ( ) => {
@@ -264,4 +265,46 @@ describe('useMutation', () => {
264
265
const result = await promise ;
265
266
expect ( result ) . toMatchObject ( { data : { foo : 'bar' } } ) ;
266
267
} ) ;
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
+ } ) ;
267
310
} ) ;
0 commit comments