@@ -6,7 +6,7 @@ import type { SubscribePayload, Client as SubscriptionClient, Sink as Subscripti
66import type { FunctionLike } from "jest-mock" ;
77import { defaults , find , findLast } from "lodash-es" ;
88import pRetry from "p-retry" ;
9- import { act } from "react-dom/test-utils " ;
9+ import { act } from "react" ;
1010import type { Sink , Source , Subject } from "wonka" ;
1111import { filter , makeSubject , pipe , subscribe , take , toPromise } from "wonka" ;
1212import { $gadgetConnection } from "../src/GadgetConnection.js" ;
@@ -49,7 +49,7 @@ export type MockOperationFn<F extends FunctionLike> = jest.Mock<(...args: any[])
4949export type MockFetchFn = jest . Mock & {
5050 requests : { args : any [ ] ; resolve : ( response : Response ) => void ; reject : ( error : Error ) => void } [ ] ;
5151 pushResponse : ( response : Response ) => Promise < void > ;
52- reportAbort : ( ) => Promise < void > ;
52+ waitForRequest : ( options ?: pRetry . Options ) => Promise < void > ;
5353} ;
5454
5555export interface MockUrqlClient extends Client {
@@ -150,11 +150,25 @@ const newMockFetchFn = () => {
150150
151151 const fn = jest . fn ( ( ...args ) => {
152152 return new Promise < Response > ( ( resolve , reject ) => {
153- requests . push ( {
153+ const signal = ( args [ 1 ] as any ) ?. signal ;
154+
155+ const request = {
154156 args,
155157 resolve,
156158 reject,
157- } ) ;
159+ } ;
160+
161+ if ( signal ) {
162+ signal . addEventListener ( "abort" , ( ) => {
163+ const idx = requests . findIndex ( ( r ) => r === request ) ;
164+ if ( idx !== - 1 ) {
165+ request . reject ( new Error ( "AbortError: The user aborted a request." ) ) ;
166+ requests . splice ( idx , 1 ) ;
167+ }
168+ } ) ;
169+ }
170+
171+ requests . push ( request ) ;
158172 } ) ;
159173 } ) as unknown as MockFetchFn ;
160174
@@ -173,20 +187,23 @@ const newMockFetchFn = () => {
173187 await request . resolve ( response ) ;
174188 } ) ;
175189 } ;
176- fn . reportAbort = async ( ) => {
190+
191+ fn . waitForRequest = async ( options ?: pRetry . Options ) => {
192+ const requestCount = requests . length ;
177193 await act ( async ( ) => {
178- const request = requests . shift ( ) ;
179- if ( ! request ) {
180- throw new Error ( "no requests started for response pushing" ) ;
181- }
182- const signal = request . args [ 1 ] ?. signal ;
183- if ( ! signal ) {
184- throw new Error ( "no signal on request, can't report an abort that has happened" ) ;
185- }
186- if ( ! signal . aborted ) {
187- throw new Error ( "signal on request has not been aborted, can't report an abort that has happened" ) ;
188- }
189- await request . reject ( new Error ( "AbortError: The user aborted a request." ) ) ;
194+ await pRetry (
195+ async ( ) => {
196+ if ( requests . length > requestCount ) {
197+ return ;
198+ }
199+ throw new Error ( "request not found" ) ;
200+ } ,
201+ defaults ( options , {
202+ attempts : 20 ,
203+ minTimeout : 10 ,
204+ maxTimeout : 250 ,
205+ } )
206+ ) ;
190207 } ) ;
191208 } ;
192209
0 commit comments