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

Commit

Permalink
Add unit tests for rendering PromptTextBox in MessageBox when input i…
Browse files Browse the repository at this point in the history
…s allowed

Extract out and export onWindowPrompt function and unit test it
  • Loading branch information
AlexRobinson- authored and bsclifton committed May 2, 2018
1 parent 1a91cc6 commit a2873fa
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 17 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)
})
})
})
56 changes: 55 additions & 1 deletion test/unit/app/renderer/components/common/messageBoxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ let appState = Immutable.fromJS({
}
})

const createAppState = detail => Immutable.fromJS({
windows: [{
windowId: 1,
windowUUID: 'uuid'
}],
tabs: [{
tabId: tabId,
windowId: 1,
windowUUID: 'uuid',
url: 'https://brave.com',
messageBoxDetail: detail
}],
tabsInternal: {
index: {
1: 0
}
}
})

describe('MessageBox component unit tests', function () {
before(function () {
mockery.enable({
Expand All @@ -60,7 +79,7 @@ describe('MessageBox component unit tests', function () {

describe('Rendering', function () {
before(function () {
appStoreRenderer.state = Immutable.fromJS(appState)
appStoreRenderer.state = createAppState(detail1)
})
it('renders itself inside a dialog component', function () {
const wrapper = mount(
Expand Down Expand Up @@ -98,6 +117,19 @@ describe('MessageBox component unit tests', function () {
assert.equal(wrapper.find('button[data-l10n-id="Cancel"][data-test-id="secondaryColor"]').length, 1)
})

it('renders the PromptTextBox when input is allowed', function () {
appStoreRenderer.state = createAppState(Object.assign({}, detail1, {
allowInput: true
}))
const wrapper = mount(
<MessageBox
tabId={tabId}
allowInput
/>
)
assert.equal(wrapper.find('PromptTextBox').length, 1)
})

it('hides the suppress checkbox if showSuppress is false', function () {
const appState2 = appState.setIn(['tabs', 0, 'messageBoxDetail', 'showSuppress'], false)
appStoreRenderer.state = Immutable.fromJS(appState2)
Expand Down Expand Up @@ -158,5 +190,27 @@ describe('MessageBox component unit tests', function () {
assert.equal(spy.withArgs(tabId, response).calledOnce, true)
appActions.tabMessageBoxDismissed.restore()
})

it('calls appActions.tabMessageBoxDismissed with input input is allowed', function () {
const expectedInput = 'some input'
appStoreRenderer.state = createAppState(Object.assign({}, detail1, {
allowInput: true,
defaultPromptText: expectedInput
}))
const spy = sinon.spy(appActions, 'tabMessageBoxDismissed')
const wrapper = mount(
<MessageBox
tabId={tabId}
/>
)
const response = {
suppress: detail1.suppress,
result: false,
input: expectedInput
}
wrapper.find('button[data-l10n-id="Cancel"][data-test-id="secondaryColor"]').simulate('click')
assert.equal(spy.withArgs(tabId, response).calledOnce, true)
appActions.tabMessageBoxDismissed.restore()
})
})
})

0 comments on commit a2873fa

Please sign in to comment.