@@ -295,13 +295,9 @@ function mockDispatch (opts, handler) {
295295
296296 mockDispatch . timesInvoked ++
297297
298- // Here's where we resolve a callback if a callback is present for the dispatch data.
299- if ( mockDispatch . data . callback ) {
300- mockDispatch . data = { ...mockDispatch . data , ...mockDispatch . data . callback ( opts ) }
301- }
302-
303298 // Parse mockDispatch data
304- const { data : { statusCode, data, headers, trailers, error } , delay, persist } = mockDispatch
299+ const { data : response , delay, persist } = mockDispatch
300+ const { error } = response
305301 const { timesInvoked, times } = mockDispatch
306302
307303 // If it's used up and not persistent, mark as consumed
@@ -392,19 +388,27 @@ function mockDispatch (opts, handler) {
392388 }
393389 }
394390
395- function handleReply ( mockDispatches , _data = data ) {
391+ function handleReply ( mockDispatches , _response = response ) {
396392 // Don't send response if the request was aborted
397393 if ( aborted ) {
398394 return
399395 }
400396
397+ if ( _response . callback ) {
398+ const { callback, ...responseDefaults } = _response
399+ mockDispatch . data = { ...responseDefaults , ...callback ( replyOpts ) }
400+ return handleReply ( mockDispatches , mockDispatch . data )
401+ }
402+
403+ const { statusCode, data, headers, trailers } = _response
404+
401405 // fetch's HeadersList is a 1D string array
402406 const optsHeaders = Array . isArray ( opts . headers )
403407 ? buildHeadersFromArray ( opts . headers )
404408 : opts . headers
405- const body = typeof _data === 'function'
406- ? _data ( { ...replyOpts , headers : optsHeaders } )
407- : _data
409+ const body = typeof data === 'function'
410+ ? data ( { ...replyOpts , headers : optsHeaders } )
411+ : data
408412
409413 // util.types.isPromise is likely needed for jest.
410414 if ( isPromise ( body ) ) {
@@ -413,7 +417,7 @@ function mockDispatch (opts, handler) {
413417 // synchronously throw the error, which breaks some tests.
414418 // Rather, we wait for the callback to resolve if it is a
415419 // promise, and then re-run handleReply with the new body.
416- return body . then ( ( newData ) => handleReply ( mockDispatches , newData ) )
420+ return body . then ( ( newData ) => handleReply ( mockDispatches , { ... _response , data : newData } ) )
417421 }
418422
419423 // Check again if aborted after async body resolution
@@ -444,7 +448,7 @@ function dispatchRequestBody (body, handler, controller, isAborted) {
444448 }
445449
446450 if ( body == null ) {
447- return callOnRequestSent ( handler , controller ) ? body : null
451+ return callOnRequestSent ( handler , controller , isAborted ) ? body : null
448452 }
449453
450454 if ( body && typeof body [ Symbol . asyncIterator ] === 'function' ) {
@@ -459,12 +463,12 @@ function dispatchRequestBody (body, handler, controller, isAborted) {
459463 return null
460464 }
461465 chunks . push ( chunk )
462- if ( ! callOnBodySent ( handler , controller , chunk ) ) {
466+ if ( ! callOnBodySent ( handler , controller , chunk ) || isAborted ( ) ) {
463467 return null
464468 }
465469 }
466470
467- return ! isAborted ( ) && callOnRequestSent ( handler , controller ) ? chunks : null
471+ return callOnRequestSent ( handler , controller , isAborted ) ? chunks : null
468472 }
469473
470474 if ( isAborted ( ) ) {
@@ -474,7 +478,7 @@ function dispatchRequestBody (body, handler, controller, isAborted) {
474478 return null
475479 }
476480
477- return ! isAborted ( ) && callOnRequestSent ( handler , controller ) ? body : null
481+ return callOnRequestSent ( handler , controller , isAborted ) ? body : null
478482}
479483
480484async function dispatchAsyncIterableBody ( body , handler , controller , isAborted ) {
@@ -485,12 +489,12 @@ async function dispatchAsyncIterableBody (body, handler, controller, isAborted)
485489 return null
486490 }
487491 chunks . push ( chunk )
488- if ( ! callOnBodySent ( handler , controller , chunk ) ) {
492+ if ( ! callOnBodySent ( handler , controller , chunk ) || isAborted ( ) ) {
489493 return null
490494 }
491495 }
492496
493- if ( isAborted ( ) || ! callOnRequestSent ( handler , controller ) ) {
497+ if ( ! callOnRequestSent ( handler , controller , isAborted ) ) {
494498 return null
495499 }
496500
@@ -511,10 +515,10 @@ function callOnBodySent (handler, controller, chunk) {
511515 }
512516}
513517
514- function callOnRequestSent ( handler , controller ) {
518+ function callOnRequestSent ( handler , controller , isAborted ) {
515519 try {
516520 handler . onRequestSent ?. ( )
517- return true
521+ return ! isAborted ( )
518522 } catch ( error ) {
519523 controller . abort ( error )
520524 return false
0 commit comments