Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Extract out and export onWindowPrompt function and unit test it
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRobinson- committed Apr 17, 2018
1 parent 81c4262 commit 2885c19
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
35 changes: 19 additions & 16 deletions app/browser/tabMessageBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -145,3 +147,4 @@ const tabMessageBox = {
}

module.exports = tabMessageBox
module.exports.onWindowPrompt = onWindowPrompt
48 changes: 48 additions & 0 deletions test/unit/app/browser/tabMessageBoxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit 2885c19

Please sign in to comment.