test: use node helpers for events.once and setTimeout promise (#37374)
This commit is contained in:
parent
46c8b9c728
commit
a3e3efe4c4
47 changed files with 932 additions and 927 deletions
|
@ -3,53 +3,21 @@
|
|||
* with events in async/await manner.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {!EventTarget} target
|
||||
* @param {string} eventName
|
||||
* @return {!Promise<!Event>}
|
||||
*/
|
||||
export const waitForEvent = (target: EventTarget, eventName: string) => {
|
||||
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.
|
||||
*/
|
||||
export const emittedOnce = (emitter: NodeJS.EventEmitter, eventName: string, trigger?: () => void) => {
|
||||
return emittedNTimes(emitter, eventName, 1, trigger).then(([result]) => result);
|
||||
};
|
||||
import { on } from 'events';
|
||||
|
||||
export const emittedNTimes = async (emitter: NodeJS.EventEmitter, eventName: string, times: number, trigger?: () => void) => {
|
||||
const events: any[][] = [];
|
||||
const p = new Promise<any[][]>(resolve => {
|
||||
const handler = (...args: any[]) => {
|
||||
events.push(args);
|
||||
if (events.length === times) {
|
||||
emitter.removeListener(eventName, handler);
|
||||
resolve(events);
|
||||
}
|
||||
};
|
||||
emitter.on(eventName, handler);
|
||||
});
|
||||
if (trigger) {
|
||||
await Promise.resolve(trigger());
|
||||
const iter = on(emitter, eventName);
|
||||
if (trigger) await Promise.resolve(trigger());
|
||||
for await (const args of iter) {
|
||||
events.push(args);
|
||||
if (events.length === times) { break; }
|
||||
}
|
||||
return p;
|
||||
return events;
|
||||
};
|
||||
|
||||
export const emittedUntil = async (emitter: NodeJS.EventEmitter, eventName: string, untilFn: Function) => {
|
||||
const p = new Promise<any[]>(resolve => {
|
||||
const handler = (...args: any[]) => {
|
||||
if (untilFn(...args)) {
|
||||
emitter.removeListener(eventName, handler);
|
||||
resolve(args);
|
||||
}
|
||||
};
|
||||
emitter.on(eventName, handler);
|
||||
});
|
||||
return p;
|
||||
for await (const args of on(emitter, eventName)) {
|
||||
if (untilFn(...args)) { return args; }
|
||||
}
|
||||
};
|
||||
|
|
|
@ -21,8 +21,6 @@ const addOnly = <T>(fn: Function): T => {
|
|||
export const ifit = (condition: boolean) => (condition ? it : addOnly<TestFunction>(it.skip));
|
||||
export const ifdescribe = (condition: boolean) => (condition ? describe : addOnly<SuiteFunction>(describe.skip));
|
||||
|
||||
export const delay = (time: number = 0) => new Promise(resolve => setTimeout(resolve, time));
|
||||
|
||||
type CleanupFunction = (() => void) | (() => Promise<void>)
|
||||
const cleanupFunctions: CleanupFunction[] = [];
|
||||
export async function runCleanupFunctions () {
|
||||
|
@ -183,6 +181,7 @@ export async function itremote (name: string, fn: Function, args?: any[]) {
|
|||
const { ok, message } = await w.webContents.executeJavaScript(`(async () => {
|
||||
try {
|
||||
const chai_1 = require('chai')
|
||||
const promises_1 = require('timers/promises')
|
||||
chai_1.use(require('chai-as-promised'))
|
||||
chai_1.use(require('dirty-chai'))
|
||||
await (${fn})(...${JSON.stringify(args ?? [])})
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { expect } from 'chai';
|
||||
import { BrowserWindow } from 'electron/main';
|
||||
import { emittedOnce } from './events-helpers';
|
||||
import { once } from 'events';
|
||||
|
||||
async function ensureWindowIsClosed (window: BrowserWindow | null) {
|
||||
if (window && !window.isDestroyed()) {
|
||||
|
@ -10,7 +10,7 @@ async function ensureWindowIsClosed (window: BrowserWindow | null) {
|
|||
// <webview> children which need to be destroyed first. In that case, we
|
||||
// await the 'closed' event which signals the complete shutdown of the
|
||||
// window.
|
||||
const isClosed = emittedOnce(window, 'closed');
|
||||
const isClosed = once(window, 'closed');
|
||||
window.destroy();
|
||||
await isClosed;
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue