@@ -81,7 +81,9 @@ test("useAsync passes finishedAt date when the promise finishes", async () => {
81
81
82
82
test ( "useAsync passes reload function that re-runs the promise" , async ( ) => {
83
83
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
+ )
85
87
const { getByText } = render ( component )
86
88
flushEffects ( )
87
89
expect ( promiseFn ) . toHaveBeenCalledTimes ( 1 )
@@ -134,6 +136,21 @@ test("useAsync runs deferFn only when explicitly invoked, passing arguments and
134
136
expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 2 , expect . objectContaining ( { deferFn, foo : "bar" } ) )
135
137
} )
136
138
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
+
137
154
test ( "useAsync reload uses the arguments of the previous run" , ( ) => {
138
155
let counter = 1
139
156
const deferFn = jest . fn ( ) . mockReturnValue ( resolveTo ( ) )
0 commit comments