From 10a174fc6ac68e2b1980d39094f9b72f2876277c Mon Sep 17 00:00:00 2001 From: Alexey Kuzmin Date: Thu, 21 Jun 2018 16:43:15 +0200 Subject: [PATCH] spec: refactor helper closeWindow() function (#13337) --- spec/window-helpers.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/spec/window-helpers.js b/spec/window-helpers.js index 5a63100f9d0e..808f646f3cf5 100644 --- a/spec/window-helpers.js +++ b/spec/window-helpers.js @@ -1,22 +1,19 @@ -const assert = require('assert') +const {expect} = require('chai') const {BrowserWindow} = require('electron').remote -exports.closeWindow = (window, {assertSingleWindow} = {assertSingleWindow: true}) => { - if (window == null || window.isDestroyed()) { - if (assertSingleWindow) { - assert.equal(BrowserWindow.getAllWindows().length, 1) - } - return Promise.resolve() - } else { - return new Promise((resolve, reject) => { - window.once('closed', () => { - if (assertSingleWindow) { - assert.equal(BrowserWindow.getAllWindows().length, 1) - } - resolve() - }) - window.setClosable(true) - window.close() - }) +const {emittedOnce} = require('./events-helpers') + +exports.closeWindow = async (window = null, + {assertSingleWindow} = {assertSingleWindow: true}) => { + const windowExists = (window !== null) && !window.isDestroyed() + if (windowExists) { + const isClosed = emittedOnce(window, 'closed') + window.setClosable(true) + window.close() + await isClosed + } + + if (assertSingleWindow) { + expect(BrowserWindow.getAllWindows()).to.have.lengthOf(1) } }