@@ -3,6 +3,9 @@ import React from "react"
3
3
import { render , fireEvent , cleanup , waitForElement } from "react-testing-library"
4
4
import Async , { createInstance } from "./"
5
5
6
+ const abortController = { abort : ( ) => { } }
7
+ window . AbortController = jest . fn ( ) . mockImplementation ( ( ) => abortController )
8
+
6
9
afterEach ( cleanup )
7
10
8
11
const resolveIn = ms => value => new Promise ( resolve => setTimeout ( resolve , ms , value ) )
@@ -20,7 +23,7 @@ describe("Async", () => {
20
23
test ( "calls promiseFn with props" , ( ) => {
21
24
const promiseFn = jest . fn ( ) . mockReturnValue ( Promise . resolve ( ) )
22
25
render ( < Async promiseFn = { promiseFn } anotherProp = "123" /> )
23
- expect ( promiseFn ) . toHaveBeenCalledWith ( { promiseFn, anotherProp : "123" } )
26
+ expect ( promiseFn ) . toHaveBeenCalledWith ( { promiseFn, anotherProp : "123" } , abortController )
24
27
} )
25
28
26
29
test ( "passes resolved data to children as render prop" , async ( ) => {
@@ -134,9 +137,19 @@ describe("Async", () => {
134
137
)
135
138
expect ( deferFn ) . not . toHaveBeenCalled ( )
136
139
fireEvent . click ( getByText ( "run" ) )
137
- expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 1 , expect . objectContaining ( { deferFn, foo : "bar" } ) )
140
+ expect ( deferFn ) . toHaveBeenCalledWith (
141
+ "go" ,
142
+ 1 ,
143
+ expect . objectContaining ( { deferFn, foo : "bar" } ) ,
144
+ abortController
145
+ )
138
146
fireEvent . click ( getByText ( "run" ) )
139
- expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 2 , expect . objectContaining ( { deferFn, foo : "bar" } ) )
147
+ expect ( deferFn ) . toHaveBeenCalledWith (
148
+ "go" ,
149
+ 2 ,
150
+ expect . objectContaining ( { deferFn, foo : "bar" } ) ,
151
+ abortController
152
+ )
140
153
} )
141
154
142
155
test ( "reload uses the arguments of the previous run" , ( ) => {
@@ -156,11 +169,26 @@ describe("Async", () => {
156
169
)
157
170
expect ( deferFn ) . not . toHaveBeenCalled ( )
158
171
fireEvent . click ( getByText ( "run" ) )
159
- expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 1 , expect . objectContaining ( { deferFn } ) )
172
+ expect ( deferFn ) . toHaveBeenCalledWith (
173
+ "go" ,
174
+ 1 ,
175
+ expect . objectContaining ( { deferFn } ) ,
176
+ abortController
177
+ )
160
178
fireEvent . click ( getByText ( "run" ) )
161
- expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 2 , expect . objectContaining ( { deferFn } ) )
179
+ expect ( deferFn ) . toHaveBeenCalledWith (
180
+ "go" ,
181
+ 2 ,
182
+ expect . objectContaining ( { deferFn } ) ,
183
+ abortController
184
+ )
162
185
fireEvent . click ( getByText ( "reload" ) )
163
- expect ( deferFn ) . toHaveBeenCalledWith ( "go" , 2 , expect . objectContaining ( { deferFn } ) )
186
+ expect ( deferFn ) . toHaveBeenCalledWith (
187
+ "go" ,
188
+ 2 ,
189
+ expect . objectContaining ( { deferFn } ) ,
190
+ abortController
191
+ )
164
192
} )
165
193
166
194
test ( "only accepts the last invocation of the promise" , async ( ) => {
0 commit comments