1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00
metamask-extension/test/lib/wait-until-called.js
2020-11-02 17:41:28 -06:00

27 lines
919 B
JavaScript

/**
* A function that wraps a sinon stubbed function and returns a Promise
* when this stub was called.
*
* The stub that has been passed in will be setup to call the wrapped function
* directly, then trigger the returned Promise to resolve.
*
* WARNING: Any existing `callsFake` behavior will be overwritten.
*
* @param {import('sinon').stub} stub - A sinon stub of a function
* @param {unknown} [wrappedThis] - The object the stubbed function was called on, if any (i.e. the `this` value)
* @returns {Promise} A Promise that resolves when the stub has been called
*/
export default function waitUntilCalled(stub, wrappedThis = null) {
let wasCalled
const stubHasBeenCalled = new Promise((resolve) => {
wasCalled = resolve
})
stub.callsFake((...args) => {
if (stub.wrappedMethod) {
stub.wrappedMethod.call(wrappedThis, ...args)
}
wasCalled()
})
return stubHasBeenCalled
}