spec: convert ipc specs to expect (#13273)

* spec: convert ipc specs to expect

* fix buffer value comparison
This commit is contained in:
Shelley Vohr 2018-06-19 08:21:50 -07:00 committed by Charles Kerr
parent 0219ef0feb
commit 9a79889692
2 changed files with 63 additions and 51 deletions

View file

@ -1,9 +1,13 @@
'use strict' 'use strict'
const assert = require('assert') const chai = require('chai')
const dirtyChai = require('dirty-chai')
const path = require('path') const path = require('path')
const {closeWindow} = require('./window-helpers') const {closeWindow} = require('./window-helpers')
const {expect} = chai
chai.use(dirtyChai)
const {remote} = require('electron') const {remote} = require('electron')
const {ipcMain, BrowserWindow} = remote const {ipcMain, BrowserWindow} = remote
@ -45,22 +49,22 @@ describe('ipc main module', () => {
const listener = () => {} const listener = () => {}
w.on('test', listener) w.on('test', listener)
assert.equal(w.listenerCount('test'), 1) expect(w.listenerCount('test')).to.equal(1)
w.removeListener('test', listener) w.removeListener('test', listener)
assert.equal(w.listenerCount('test'), 0) expect(w.listenerCount('test')).to.equal(0)
}) })
}) })
it('throws an error when removing all the listeners', () => { it('throws an error when removing all the listeners', () => {
ipcMain.on('test-event', () => {}) ipcMain.on('test-event', () => {})
assert.equal(ipcMain.listenerCount('test-event'), 1) expect(ipcMain.listenerCount('test-event')).to.equal(1)
assert.throws(() => { expect(() => {
ipcMain.removeAllListeners() ipcMain.removeAllListeners()
}, /Removing all listeners from ipcMain will make Electron internals stop working/) }).to.throw(/Removing all listeners from ipcMain will make Electron internals stop working/)
ipcMain.removeAllListeners('test-event') ipcMain.removeAllListeners('test-event')
assert.equal(ipcMain.listenerCount('test-event'), 0) expect(ipcMain.listenerCount('test-event')).to.equal(0)
}) })
describe('remote objects registry', () => { describe('remote objects registry', () => {
@ -68,7 +72,8 @@ describe('ipc main module', () => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({ show: false })
ipcMain.once('error-message', (event, message) => { ipcMain.once('error-message', (event, message) => {
assert(message.startsWith('Cannot call function \'getURL\' on missing remote object'), message) const correctMsgStart = message.startsWith('Cannot call function \'getURL\' on missing remote object')
expect(correctMsgStart).to.be.true()
done() done()
}) })

View file

@ -1,10 +1,14 @@
'use strict' 'use strict'
const assert = require('assert') const chai = require('chai')
const dirtyChai = require('dirty-chai')
const http = require('http') const http = require('http')
const path = require('path') const path = require('path')
const {closeWindow} = require('./window-helpers') const {closeWindow} = require('./window-helpers')
const {expect} = chai
chai.use(dirtyChai)
const {ipcRenderer, remote} = require('electron') const {ipcRenderer, remote} = require('electron')
const {ipcMain, webContents, BrowserWindow} = remote const {ipcMain, webContents, BrowserWindow} = remote
@ -16,97 +20,97 @@ describe('ipc renderer module', () => {
afterEach(() => closeWindow(w).then(() => { w = null })) afterEach(() => closeWindow(w).then(() => { w = null }))
describe('ipc.sender.send', () => { describe('ipc.sender.send', () => {
it('should work when sending an object containing id property', (done) => { it('should work when sending an object containing id property', done => {
const obj = { const obj = {
id: 1, id: 1,
name: 'ly' name: 'ly'
} }
ipcRenderer.once('message', function (event, message) { ipcRenderer.once('message', (event, message) => {
assert.deepEqual(message, obj) expect(message).to.deep.equal(obj)
done() done()
}) })
ipcRenderer.send('message', obj) ipcRenderer.send('message', obj)
}) })
it('can send instances of Date', (done) => { it('can send instances of Date', done => {
const currentDate = new Date() const currentDate = new Date()
ipcRenderer.once('message', function (event, value) { ipcRenderer.once('message', (event, value) => {
assert.equal(value, currentDate.toISOString()) expect(value).to.equal(currentDate.toISOString())
done() done()
}) })
ipcRenderer.send('message', currentDate) ipcRenderer.send('message', currentDate)
}) })
it('can send instances of Buffer', (done) => { it('can send instances of Buffer', done => {
const buffer = Buffer.from('hello') const buffer = Buffer.from('hello')
ipcRenderer.once('message', function (event, message) { ipcRenderer.once('message', (event, message) => {
assert.ok(buffer.equals(message)) expect(buffer.equals(message)).to.be.true()
done() done()
}) })
ipcRenderer.send('message', buffer) ipcRenderer.send('message', buffer)
}) })
it('can send objects with DOM class prototypes', (done) => { it('can send objects with DOM class prototypes', done => {
ipcRenderer.once('message', function (event, value) { ipcRenderer.once('message', (event, value) => {
assert.equal(value.protocol, 'file:') expect(value.protocol).to.equal('file:')
assert.equal(value.hostname, '') expect(value.hostname).to.equal('')
done() done()
}) })
ipcRenderer.send('message', document.location) ipcRenderer.send('message', document.location)
}) })
it('can send Electron API objects', (done) => { it('can send Electron API objects', done => {
const webContents = remote.getCurrentWebContents() const webContents = remote.getCurrentWebContents()
ipcRenderer.once('message', function (event, value) { ipcRenderer.once('message', (event, value) => {
assert.deepEqual(value.browserWindowOptions, webContents.browserWindowOptions) expect(value.browserWindowOptions).to.deep.equal(webContents.browserWindowOptions)
done() done()
}) })
ipcRenderer.send('message', webContents) ipcRenderer.send('message', webContents)
}) })
it('does not crash on external objects (regression)', (done) => { it('does not crash on external objects (regression)', done => {
const request = http.request({port: 5000, hostname: '127.0.0.1', method: 'GET', path: '/'}) const request = http.request({port: 5000, hostname: '127.0.0.1', method: 'GET', path: '/'})
const stream = request.agent.sockets['127.0.0.1:5000:'][0]._handle._externalStream const stream = request.agent.sockets['127.0.0.1:5000:'][0]._handle._externalStream
request.on('error', () => {}) request.on('error', () => {})
ipcRenderer.once('message', function (event, requestValue, externalStreamValue) { ipcRenderer.once('message', (event, requestValue, externalStreamValue) => {
assert.equal(requestValue.method, 'GET') expect(requestValue.method).to.equal('GET')
assert.equal(requestValue.path, '/') expect(requestValue.path).to.equal('/')
assert.equal(externalStreamValue, null) expect(externalStreamValue).to.be.null()
done() done()
}) })
ipcRenderer.send('message', request, stream) ipcRenderer.send('message', request, stream)
}) })
it('can send objects that both reference the same object', (done) => { it('can send objects that both reference the same object', done => {
const child = {hello: 'world'} const child = {hello: 'world'}
const foo = {name: 'foo', child: child} const foo = {name: 'foo', child: child}
const bar = {name: 'bar', child: child} const bar = {name: 'bar', child: child}
const array = [foo, bar] const array = [foo, bar]
ipcRenderer.once('message', function (event, arrayValue, fooValue, barValue, childValue) { ipcRenderer.once('message', (event, arrayValue, fooValue, barValue, childValue) => {
assert.deepEqual(arrayValue, array) expect(arrayValue).to.deep.equal(array)
assert.deepEqual(fooValue, foo) expect(fooValue).to.deep.equal(foo)
assert.deepEqual(barValue, bar) expect(barValue).to.deep.equal(bar)
assert.deepEqual(childValue, child) expect(childValue).to.deep.equal(child)
done() done()
}) })
ipcRenderer.send('message', array, foo, bar, child) ipcRenderer.send('message', array, foo, bar, child)
}) })
it('inserts null for cyclic references', (done) => { it('inserts null for cyclic references', done => {
const array = [5] const array = [5]
array.push(array) array.push(array)
const child = {hello: 'world'} const child = {hello: 'world'}
child.child = child child.child = child
ipcRenderer.once('message', function (event, arrayValue, childValue) { ipcRenderer.once('message', (event, arrayValue, childValue) => {
assert.equal(arrayValue[0], 5) expect(arrayValue[0]).to.equal(5)
assert.equal(arrayValue[1], null) expect(arrayValue[1]).to.be.null()
assert.equal(childValue.hello, 'world') expect(childValue.hello).to.equal('world')
assert.equal(childValue.child, null) expect(childValue.child).to.be.null()
done() done()
}) })
@ -121,7 +125,7 @@ describe('ipc renderer module', () => {
it('can be replied by setting event.returnValue', () => { it('can be replied by setting event.returnValue', () => {
const msg = ipcRenderer.sendSync('echo', 'test') const msg = ipcRenderer.sendSync('echo', 'test')
assert.equal(msg, 'test') expect(msg).to.equal('test')
}) })
}) })
@ -136,11 +140,11 @@ describe('ipc renderer module', () => {
contents = null contents = null
}) })
it('sends message to WebContents', (done) => { it('sends message to WebContents', done => {
const webContentsId = remote.getCurrentWebContents().id const webContentsId = remote.getCurrentWebContents().id
ipcRenderer.once('pong', function (event, id) { ipcRenderer.once('pong', (event, id) => {
assert.equal(webContentsId, id) expect(webContentsId).to.equal(id)
done() done()
}) })
@ -163,14 +167,17 @@ describe('ipc renderer module', () => {
'Function provided here: remote-event-handler.html:11:33', 'Function provided here: remote-event-handler.html:11:33',
'Remote event names: remote-handler, other-remote-handler' 'Remote event names: remote-handler, other-remote-handler'
].join('\n') ].join('\n')
const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
assert.deepEqual(results, {
expect(results).to.deep.equal({
warningMessage: expectedMessage, warningMessage: expectedMessage,
listenerCountBefore: 2, listenerCountBefore: 2,
listenerCountAfter: 1 listenerCountAfter: 1
}) })
done() done()
}) })
w.webContents.reload() w.webContents.reload()
}) })
w.loadURL(`file://${path.join(fixtures, 'api', 'remote-event-handler.html')}`) w.loadURL(`file://${path.join(fixtures, 'api', 'remote-event-handler.html')}`)
@ -179,13 +186,13 @@ describe('ipc renderer module', () => {
it('throws an error when removing all the listeners', () => { it('throws an error when removing all the listeners', () => {
ipcRenderer.on('test-event', () => {}) ipcRenderer.on('test-event', () => {})
assert.equal(ipcRenderer.listenerCount('test-event'), 1) expect(ipcRenderer.listenerCount('test-event')).to.equal(1)
assert.throws(() => { expect(() => {
ipcRenderer.removeAllListeners() ipcRenderer.removeAllListeners()
}, /Removing all listeners from ipcRenderer will make Electron internals stop working/) }).to.throw(/Removing all listeners from ipcRenderer will make Electron internals stop working/)
ipcRenderer.removeAllListeners('test-event') ipcRenderer.removeAllListeners('test-event')
assert.equal(ipcRenderer.listenerCount('test-event'), 0) expect(ipcRenderer.listenerCount('test-event')).to.equal(0)
}) })
}) })