11import * as React from 'react' ;
2- import { screen , render , waitFor } from '@testing-library/react' ;
2+ import { screen , render , waitFor , fireEvent } from '@testing-library/react' ;
33import expect from 'expect' ;
4+ import { QueryClient , useMutationState } from '@tanstack/react-query' ;
45
56import { CoreAdminContext } from '../core' ;
67import { RaRecord } from '../types' ;
@@ -19,7 +20,7 @@ import {
1920 ErrorCase as ErrorCaseUndoable ,
2021 SuccessCase as SuccessCaseUndoable ,
2122} from './useDelete.undoable.stories' ;
22- import { QueryClient , useMutationState } from '@tanstack/react-query ' ;
23+ import { MutationMode , Params } from './useDelete.stories ' ;
2324
2425describe ( 'useDelete' , ( ) => {
2526 it ( 'returns a callback that can be used with deleteOne arguments' , async ( ) => {
@@ -75,6 +76,79 @@ describe('useDelete', () => {
7576 } ) ;
7677 } ) ;
7778
79+ it ( 'uses the latest declaration time mutationMode' , async ( ) => {
80+ jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
81+ // This story uses the pessimistic mode by default
82+ render ( < MutationMode /> ) ;
83+ await waitFor ( ( ) => new Promise ( resolve => setTimeout ( resolve , 0 ) ) ) ;
84+ fireEvent . click ( screen . getByText ( 'Change mutation mode to optimistic' ) ) ;
85+ fireEvent . click ( screen . getByText ( 'Delete first post' ) ) ;
86+ // Should display the optimistic result right away if the change was handled
87+ await waitFor ( ( ) => {
88+ expect ( screen . queryByText ( 'success' ) ) . not . toBeNull ( ) ;
89+ expect ( screen . queryByText ( 'Hello' ) ) . toBeNull ( ) ;
90+ expect ( screen . queryByText ( 'World' ) ) . not . toBeNull ( ) ;
91+ expect ( screen . queryByText ( 'mutating' ) ) . not . toBeNull ( ) ;
92+ } ) ;
93+ await waitFor ( ( ) => {
94+ expect ( screen . queryByText ( 'success' ) ) . not . toBeNull ( ) ;
95+ expect ( screen . queryByText ( 'Hello' ) ) . toBeNull ( ) ;
96+ expect ( screen . queryByText ( 'World' ) ) . not . toBeNull ( ) ;
97+ expect ( screen . queryByText ( 'mutating' ) ) . toBeNull ( ) ;
98+ } ) ;
99+ } ) ;
100+
101+ it ( 'uses the latest declaration time params' , async ( ) => {
102+ jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
103+ const posts = [
104+ { id : 1 , title : 'Hello' } ,
105+ { id : 2 , title : 'World' } ,
106+ ] ;
107+ const dataProvider = {
108+ getList : ( resource , params ) => {
109+ console . log ( 'getList' , resource , params ) ;
110+ return Promise . resolve ( {
111+ data : posts ,
112+ total : posts . length ,
113+ } ) ;
114+ } ,
115+ delete : jest . fn ( ( resource , params ) => {
116+ console . log ( 'delete' , resource , params ) ;
117+ return new Promise ( resolve => {
118+ setTimeout ( ( ) => {
119+ const index = posts . findIndex ( p => p . id === params . id ) ;
120+ posts . splice ( index , 1 ) ;
121+ resolve ( { data : params . previousData } ) ;
122+ } , 1000 ) ;
123+ } ) ;
124+ } ) ,
125+ } as any ;
126+ // This story has no meta by default
127+ render ( < Params dataProvider = { dataProvider } /> ) ;
128+ await waitFor ( ( ) => new Promise ( resolve => setTimeout ( resolve , 0 ) ) ) ;
129+ fireEvent . click ( screen . getByText ( 'Change params' ) ) ;
130+ fireEvent . click ( screen . getByText ( 'Delete first post' ) ) ;
131+ // Should display the optimistic result right away if the change was handled
132+ await waitFor ( ( ) => {
133+ expect ( screen . queryByText ( 'success' ) ) . not . toBeNull ( ) ;
134+ expect ( screen . queryByText ( 'Hello' ) ) . toBeNull ( ) ;
135+ expect ( screen . queryByText ( 'World' ) ) . not . toBeNull ( ) ;
136+ expect ( screen . queryByText ( 'mutating' ) ) . not . toBeNull ( ) ;
137+ } ) ;
138+ await waitFor ( ( ) => {
139+ expect ( screen . queryByText ( 'success' ) ) . not . toBeNull ( ) ;
140+ expect ( screen . queryByText ( 'Hello' ) ) . toBeNull ( ) ;
141+ expect ( screen . queryByText ( 'World' ) ) . not . toBeNull ( ) ;
142+ expect ( screen . queryByText ( 'mutating' ) ) . toBeNull ( ) ;
143+ } ) ;
144+
145+ expect ( dataProvider . delete ) . toHaveBeenCalledWith ( 'posts' , {
146+ id : 1 ,
147+ previousData : { id : 1 , title : 'Hello' } ,
148+ meta : 'test' ,
149+ } ) ;
150+ } ) ;
151+
78152 it ( 'uses call time params over hook time params' , async ( ) => {
79153 const dataProvider = testDataProvider ( {
80154 delete : jest . fn ( ( ) => Promise . resolve ( { data : { id : 1 } } as any ) ) ,
0 commit comments