Assert remote event names

This commit is contained in:
Kevin Sawicki 2017-01-12 16:31:02 -08:00
parent b04db2e546
commit 45986405b8
4 changed files with 28 additions and 9 deletions

View file

@ -161,7 +161,7 @@ const rendererMissingErrorMessage = (meta, args, callIntoRenderer) => {
}) })
if (remoteEvents.length > 0) { if (remoteEvents.length > 0) {
message += `\nRemote event names: ${remoteEvents.join(' ')}` message += `\nRemote event names: ${remoteEvents.join(', ')}`
} }
return message return message

View file

@ -499,12 +499,20 @@ describe('ipc module', function () {
w = new BrowserWindow({ w = new BrowserWindow({
show: false show: false
}) })
w.loadURL('file://' + path.join(fixtures, 'api', 'reload-page.html')) w.webContents.once('did-finish-load', () => {
setTimeout(() => { w.webContents.once('did-finish-load', () => {
assert.throws(() => w.webContents.emit('remote-handler'), const expectedMessage = [
/Attempting to call a function in a renderer window that has been closed or released./) 'Attempting to call a function in a renderer window that has been closed or released.',
'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler'
].join('\n')
const errorMessage = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.equal(errorMessage, expectedMessage)
done() done()
}, 400) })
w.webContents.reload()
})
w.loadURL('file://' + path.join(fixtures, 'api', 'remote-event-handler.html'))
}) })
}) })

View file

@ -7,8 +7,9 @@
const {remote} = require('electron') const {remote} = require('electron')
const browserWindow = remote.getCurrentWindow() const browserWindow = remote.getCurrentWindow()
browserWindow.webContents.on('remote-handler', () => { }) const handler = () => {}
browserWindow.reload() browserWindow.webContents.on('remote-handler', handler)
browserWindow.webContents.on('other-remote-handler', handler)
</script> </script>
</head> </head>
<body> <body>

View file

@ -249,3 +249,13 @@ ipcMain.on('create-window-with-options-cycle', (event) => {
ipcMain.on('prevent-next-new-window', (event, id) => { ipcMain.on('prevent-next-new-window', (event, id) => {
webContents.fromId(id).once('new-window', event => event.preventDefault()) webContents.fromId(id).once('new-window', event => event.preventDefault())
}) })
ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => {
const contents = webContents.fromId(id)
try {
contents.emit(eventName, {sender: contents})
event.returnValue = null
} catch (error) {
event.returnValue = error.message
}
})