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

Commit 2885c19

Browse files
author
Alex Robinson
committed
Extract out and export onWindowPrompt function and unit test it
1 parent 81c4262 commit 2885c19

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

app/browser/tabMessageBox.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ const cleanupCallback = (tabId) => {
1414
return false
1515
}
1616

17+
const onWindowPrompt = show => (webContents, extraData, title, message, defaultPromptText,
18+
shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, muonCb) => {
19+
const tabId = webContents.getId()
20+
const detail = {
21+
message,
22+
title,
23+
buttons: [locale.translation('messageBoxOk'), locale.translation('messageBoxCancel')],
24+
cancelId: 1,
25+
suppress: false,
26+
allowInput: true,
27+
defaultPromptText,
28+
showSuppress: shouldDisplaySuppressCheckbox
29+
}
30+
31+
show(tabId, detail, muonCb)
32+
}
33+
1734
const tabMessageBox = {
1835
init: (state, action) => {
1936
process.on('window-alert', (webContents, extraData, title, message, defaultPromptText,
@@ -45,22 +62,7 @@ const tabMessageBox = {
4562
tabMessageBox.show(tabId, detail, muonCb)
4663
})
4764

48-
process.on('window-prompt', (webContents, extraData, title, message, defaultPromptText,
49-
shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, muonCb) => {
50-
const tabId = webContents.getId()
51-
const detail = {
52-
message,
53-
title,
54-
buttons: [locale.translation('messageBoxOk'), locale.translation('messageBoxCancel')],
55-
cancelId: 1,
56-
suppress: false,
57-
allowInput: true,
58-
defaultPromptText,
59-
showSuppress: shouldDisplaySuppressCheckbox
60-
}
61-
62-
tabMessageBox.show(tabId, detail, muonCb)
63-
})
65+
process.on('window-prompt', onWindowPrompt(tabMessageBox.show))
6466

6567
return state
6668
},
@@ -145,3 +147,4 @@ const tabMessageBox = {
145147
}
146148

147149
module.exports = tabMessageBox
150+
module.exports.onWindowPrompt = onWindowPrompt

test/unit/app/browser/tabMessageBoxTest.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ describe('tabMessageBox unit tests', function () {
3838
useCleanCache: true
3939
})
4040

41+
const fakeLocale = {
42+
translation: (token) => { return token }
43+
}
44+
4145
mockery.registerMock('electron', require('../../lib/fakeElectron'))
4246
mockery.registerMock('../common/state/tabMessageBoxState', fakeMessageBoxState)
47+
mockery.registerMock('../../../js/l10n', fakeLocale)
4348
tabMessageBox = require('../../../../app/browser/tabMessageBox')
4449
appActions = require('../../../../js/actions/appActions')
4550

@@ -224,4 +229,47 @@ describe('tabMessageBox unit tests', function () {
224229
})
225230
})
226231
})
232+
233+
describe('onWindowPrompt', () => {
234+
const tabId = '123'
235+
const webContents = {
236+
getId: () => tabId
237+
}
238+
const extraData = undefined
239+
const title = 'some title'
240+
const message = 'some message'
241+
const defaultPromptText = 'some prompt text'
242+
const shouldDisplaySuppressCheckbox = true
243+
const isBeforeUnloadDialog = undefined
244+
const isReload = undefined
245+
const muonCb = 'muonCb'
246+
247+
it('calls tabMessageBox.show', () => {
248+
const mockShow = sinon.stub()
249+
const expectecDetail = {
250+
message,
251+
title,
252+
buttons: ['MESSAGEBOXOK', 'MESSAGEBOXCANCEL'],
253+
cancelId: 1,
254+
suppress: false,
255+
allowInput: true,
256+
defaultPromptText,
257+
showSuppress: shouldDisplaySuppressCheckbox
258+
}
259+
260+
tabMessageBox.onWindowPrompt(mockShow)(
261+
webContents,
262+
extraData,
263+
title,
264+
message,
265+
defaultPromptText,
266+
shouldDisplaySuppressCheckbox,
267+
isBeforeUnloadDialog,
268+
isReload,
269+
muonCb
270+
)
271+
272+
assert.equal(mockShow.withArgs(tabId, expectecDetail, muonCb).calledOnce, true)
273+
})
274+
})
227275
})

0 commit comments

Comments
 (0)