electron/spec/api-ipc-main-spec.js

84 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-12-01 20:57:41 +00:00
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
2017-12-01 20:57:41 +00:00
const path = require('path')
const {closeWindow} = require('./window-helpers')
const {expect} = chai
chai.use(dirtyChai)
2017-12-01 21:11:54 +00:00
const {remote} = require('electron')
const {ipcMain, BrowserWindow} = remote
2017-12-01 20:57:41 +00:00
2017-12-01 21:01:03 +00:00
describe('ipc main module', () => {
2017-12-01 20:57:41 +00:00
const fixtures = path.join(__dirname, 'fixtures')
let w = null
afterEach(() => closeWindow(w).then(() => { w = null }))
describe('ipc.sendSync', () => {
afterEach(() => { ipcMain.removeAllListeners('send-sync-message') })
it('does not crash when reply is not sent and browser is destroyed', (done) => {
w = new BrowserWindow({ show: false })
ipcMain.once('send-sync-message', (event) => {
event.returnValue = null
done()
})
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
2017-12-01 20:57:41 +00:00
})
it('does not crash when reply is sent by multiple listeners', (done) => {
w = new BrowserWindow({ show: false })
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null
})
ipcMain.on('send-sync-message', (event) => {
event.returnValue = null
done()
})
w.loadFile(path.join(fixtures, 'api', 'send-sync-message.html'))
2017-12-01 20:57:41 +00:00
})
})
describe('remote listeners', () => {
it('can be added and removed correctly', () => {
w = new BrowserWindow({ show: false })
const listener = () => {}
w.on('test', listener)
expect(w.listenerCount('test')).to.equal(1)
2017-12-01 20:57:41 +00:00
w.removeListener('test', listener)
expect(w.listenerCount('test')).to.equal(0)
2017-12-01 20:57:41 +00:00
})
})
it('throws an error when removing all the listeners', () => {
ipcMain.on('test-event', () => {})
expect(ipcMain.listenerCount('test-event')).to.equal(1)
2017-12-01 20:57:41 +00:00
expect(() => {
2017-12-01 20:57:41 +00:00
ipcMain.removeAllListeners()
}).to.throw(/Removing all listeners from ipcMain will make Electron internals stop working/)
2017-12-01 20:57:41 +00:00
ipcMain.removeAllListeners('test-event')
expect(ipcMain.listenerCount('test-event')).to.equal(0)
2017-12-01 20:57:41 +00:00
})
describe('remote objects registry', () => {
it('does not dereference until the render view is deleted (regression)', (done) => {
w = new BrowserWindow({ show: false })
ipcMain.once('error-message', (event, message) => {
const correctMsgStart = message.startsWith('Cannot call function \'getURL\' on missing remote object')
expect(correctMsgStart).to.be.true()
2017-12-01 20:57:41 +00:00
done()
})
w.loadFile(path.join(fixtures, 'api', 'render-view-deleted.html'))
2017-12-01 20:57:41 +00:00
})
})
})