test: refactor utils.waitForEvents into utils.waitEvent (#2336)

This simplifies waiting for events in tests.
This commit is contained in:
Andrey Lushnikov
2018-04-09 15:46:05 -07:00
committed by GitHub
parent c86c12e605
commit 06d61919ef
6 changed files with 30 additions and 45 deletions

View File

@@ -76,22 +76,10 @@ const utils = module.exports = {
/**
* @param {!EventEmitter} emitter
* @param {string} eventName
* @param {number=} eventCount
* @return {!Promise<!Object>}
*/
waitForEvents: function(emitter, eventName, eventCount = 1) {
let fulfill;
const promise = new Promise(x => fulfill = x);
emitter.on(eventName, onEvent);
return promise;
function onEvent(event) {
--eventCount;
if (eventCount)
return;
emitter.removeListener(eventName, onEvent);
fulfill(event);
}
waitEvent: function(emitter, eventName) {
return new Promise(fulfill => emitter.once(eventName, fulfill));
},
/**