@@ -81,7 +81,9 @@ test("useAsync passes finishedAt date when the promise finishes", async () => {
8181
8282test ( "useAsync passes reload function that re-runs the promise" , async ( ) => {
8383 const promiseFn = jest . fn ( ) . mockReturnValue ( resolveTo ( "done" ) )
84- const component = < Async promiseFn = { promiseFn } > { ( { reload } ) => < button onClick = { reload } > reload</ button > } </ Async >
84+ const component = (
85+ < Async promiseFn = { promiseFn } > { ( { reload } ) => < button onClick = { reload } > reload</ button > } </ Async >
86+ )
8587 const { getByText } = render ( component )
8688 flushEffects ( )
8789 expect ( promiseFn ) . toHaveBeenCalledTimes ( 1 )
@@ -134,6 +136,21 @@ test("useAsync runs deferFn only when explicitly invoked, passing arguments and
134136 expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 2 , expect . objectContaining ( { deferFn, foo : "bar" } ) )
135137} )
136138
139+ test ( "useAsync cancel will prevent the resolved promise from propagating" , async ( ) => {
140+ const promiseFn = jest . fn ( ) . mockReturnValue ( Promise . resolve ( "ok" ) )
141+ const onResolve = jest . fn ( )
142+ const component = (
143+ < Async promiseFn = { promiseFn } onResolve = { onResolve } >
144+ { ( { cancel } ) => < button onClick = { cancel } > cancel</ button > }
145+ </ Async >
146+ )
147+ const { getByText } = render ( component )
148+ flushEffects ( )
149+ fireEvent . click ( getByText ( "cancel" ) )
150+ await Promise . resolve ( )
151+ expect ( onResolve ) . not . toHaveBeenCalled ( )
152+ } )
153+
137154test ( "useAsync reload uses the arguments of the previous run" , ( ) => {
138155 let counter = 1
139156 const deferFn = jest . fn ( ) . mockReturnValue ( resolveTo ( ) )
0 commit comments