11import * as React from 'react' ;
2- import { act , render , screen , waitFor } from '@testing-library/react' ;
2+ import {
3+ act ,
4+ fireEvent ,
5+ render ,
6+ screen ,
7+ waitFor ,
8+ } from '@testing-library/react' ;
39import expect from 'expect' ;
410
511import { CoreAdminContext } from '../core' ;
@@ -25,6 +31,7 @@ import {
2531 WithMiddlewaresError as WithMiddlewaresErrorUndoable ,
2632} from './useUpdate.undoable.stories' ;
2733import { QueryClient } from '@tanstack/react-query' ;
34+ import { Basic } from './useUpdate.stories' ;
2835
2936describe ( 'useUpdate' , ( ) => {
3037 describe ( 'mutate' , ( ) => {
@@ -368,6 +375,23 @@ describe('useUpdate', () => {
368375 expect ( screen . queryByText ( 'mutating' ) ) . toBeNull ( ) ;
369376 } ) ;
370377 } ) ;
378+ it ( 'allows to control the mutation mode' , async ( ) => {
379+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
380+ render ( < Basic timeout = { 10 } /> ) ;
381+ await screen . findByText ( 'Hello' ) ;
382+ // Update the post in pessimistic mode
383+ fireEvent . click ( await screen . findByText ( 'Update title' ) ) ;
384+ await screen . findByText ( 'Hello World 0' ) ;
385+ fireEvent . click ( await screen . findByText ( 'undoable' ) ) ;
386+ fireEvent . click ( await screen . findByText ( 'Increment counter' ) ) ;
387+ // Update a post in undoable mode
388+ fireEvent . click ( await screen . findByText ( 'Update title' ) ) ;
389+ // Check the optimistic result
390+ await screen . findByText ( 'Hello World 1' ) ;
391+ // As we haven't confirmed the undoable mutation, refetching the post should return nothing
392+ fireEvent . click ( await screen . findByText ( 'Refetch' ) ) ;
393+ await screen . findByText ( 'Hello World 0' ) ;
394+ } ) ;
371395 } ) ;
372396 describe ( 'query cache' , ( ) => {
373397 it ( 'updates getList query cache when dataProvider promise resolves' , async ( ) => {
0 commit comments