Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,28 @@ function mockDispatch (opts, handler) {
}

// Handle the request with a delay if necessary
const signal = opts.signal

if (typeof delay === 'number' && delay > 0) {
setTimeout(() => {
const timeout = setTimeout(() => {
if (signal?.aborted) return
handleReply(this[kDispatches])
}, delay)

if (signal) {
if (signal.aborted) {
clearTimeout(timeout)
return true
}

signal.addEventListener('abort', () => {
clearTimeout(timeout)
}, { once: true })
}
} else {
handleReply(this[kDispatches])
if (!signal?.aborted) {
handleReply(this[kDispatches])
}
}

function handleReply (mockDispatches, _data = data) {
Expand Down
Loading