mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
59aab93560
Unit tests have been added to the incoming transactions controller to ensure that block updates are correctly resulting in state updates when incoming transactions are enabled. All other events that trigger state updates are tested as well. The tests were written to be minimally dependent upon implementation details of the controller itself. `nock` was used to mock the API response from Etherscan. Each event is triggered asynchronously by `sinon`, as in production they are likely only triggered asynchronously. This was extracted from #9583 This PR includes a new `wait-until-called` module meant to help with writing asynchronous tests. It allows you to wait until a stub has been called.
27 lines
920 B
JavaScript
27 lines
920 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
|
|
}
|