spec: add helper functions for async/await tests
This commit is contained in:
parent
bcbcb4c643
commit
bbc8d50799
1 changed files with 29 additions and 0 deletions
29
spec/events-helpers.js
Normal file
29
spec/events-helpers.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/**
|
||||||
|
* @fileoverview A set of helper functions to make it easier to work
|
||||||
|
* with events in async/await manner.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {!EventTarget} target
|
||||||
|
* @param {string} eventName
|
||||||
|
* @return {!Promise<!Event>}
|
||||||
|
*/
|
||||||
|
const waitForEvent = (target, eventName) => {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
target.addEventListener(eventName, resolve, {once: true})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {!EventEmitter} emitter
|
||||||
|
* @param {string} eventName
|
||||||
|
* @return {!Promise<!Array>} With Event as the first item.
|
||||||
|
*/
|
||||||
|
const emittedOnce = (emitter, eventName) => {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
emitter.once(eventName, (...args) => resolve(args))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.emittedOnce = emittedOnce
|
||||||
|
exports.waitForEvent = waitForEvent
|
Loading…
Add table
Add a link
Reference in a new issue