From 2885c192684eb057001ba38c1358c9109dd01756 Mon Sep 17 00:00:00 2001 From: Alex Robinson Date: Tue, 17 Apr 2018 22:36:33 +1000 Subject: [PATCH] Extract out and export onWindowPrompt function and unit test it --- app/browser/tabMessageBox.js | 35 ++++++++-------- test/unit/app/browser/tabMessageBoxTest.js | 48 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/app/browser/tabMessageBox.js b/app/browser/tabMessageBox.js index 32e74eff4de..fe95a5924a4 100644 --- a/app/browser/tabMessageBox.js +++ b/app/browser/tabMessageBox.js @@ -14,6 +14,23 @@ const cleanupCallback = (tabId) => { return false } +const onWindowPrompt = show => (webContents, extraData, title, message, defaultPromptText, + shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, muonCb) => { + const tabId = webContents.getId() + const detail = { + message, + title, + buttons: [locale.translation('messageBoxOk'), locale.translation('messageBoxCancel')], + cancelId: 1, + suppress: false, + allowInput: true, + defaultPromptText, + showSuppress: shouldDisplaySuppressCheckbox + } + + show(tabId, detail, muonCb) +} + const tabMessageBox = { init: (state, action) => { process.on('window-alert', (webContents, extraData, title, message, defaultPromptText, @@ -45,22 +62,7 @@ const tabMessageBox = { tabMessageBox.show(tabId, detail, muonCb) }) - process.on('window-prompt', (webContents, extraData, title, message, defaultPromptText, - shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, muonCb) => { - const tabId = webContents.getId() - const detail = { - message, - title, - buttons: [locale.translation('messageBoxOk'), locale.translation('messageBoxCancel')], - cancelId: 1, - suppress: false, - allowInput: true, - defaultPromptText, - showSuppress: shouldDisplaySuppressCheckbox - } - - tabMessageBox.show(tabId, detail, muonCb) - }) + process.on('window-prompt', onWindowPrompt(tabMessageBox.show)) return state }, @@ -145,3 +147,4 @@ const tabMessageBox = { } module.exports = tabMessageBox +module.exports.onWindowPrompt = onWindowPrompt diff --git a/test/unit/app/browser/tabMessageBoxTest.js b/test/unit/app/browser/tabMessageBoxTest.js index 17d405c8842..d9b704d71ed 100644 --- a/test/unit/app/browser/tabMessageBoxTest.js +++ b/test/unit/app/browser/tabMessageBoxTest.js @@ -38,8 +38,13 @@ describe('tabMessageBox unit tests', function () { useCleanCache: true }) + const fakeLocale = { + translation: (token) => { return token } + } + mockery.registerMock('electron', require('../../lib/fakeElectron')) mockery.registerMock('../common/state/tabMessageBoxState', fakeMessageBoxState) + mockery.registerMock('../../../js/l10n', fakeLocale) tabMessageBox = require('../../../../app/browser/tabMessageBox') appActions = require('../../../../js/actions/appActions') @@ -224,4 +229,47 @@ describe('tabMessageBox unit tests', function () { }) }) }) + + describe('onWindowPrompt', () => { + const tabId = '123' + const webContents = { + getId: () => tabId + } + const extraData = undefined + const title = 'some title' + const message = 'some message' + const defaultPromptText = 'some prompt text' + const shouldDisplaySuppressCheckbox = true + const isBeforeUnloadDialog = undefined + const isReload = undefined + const muonCb = 'muonCb' + + it('calls tabMessageBox.show', () => { + const mockShow = sinon.stub() + const expectecDetail = { + message, + title, + buttons: ['MESSAGEBOXOK', 'MESSAGEBOXCANCEL'], + cancelId: 1, + suppress: false, + allowInput: true, + defaultPromptText, + showSuppress: shouldDisplaySuppressCheckbox + } + + tabMessageBox.onWindowPrompt(mockShow)( + webContents, + extraData, + title, + message, + defaultPromptText, + shouldDisplaySuppressCheckbox, + isBeforeUnloadDialog, + isReload, + muonCb + ) + + assert.equal(mockShow.withArgs(tabId, expectecDetail, muonCb).calledOnce, true) + }) + }) })