Browse Source

spec: convert ipc specs to expect (#13273)

* spec: convert ipc specs to expect

* fix buffer value comparison
Shelley Vohr 6 years ago
parent
commit
9a79889692
2 changed files with 63 additions and 51 deletions
  1. 13 8
      spec/api-ipc-main-spec.js
  2. 50 43
      spec/api-ipc-renderer-spec.js

+ 13 - 8
spec/api-ipc-main-spec.js

@@ -1,9 +1,13 @@
 'use strict'
 
-const assert = require('assert')
+const chai = require('chai')
+const dirtyChai = require('dirty-chai')
 const path = require('path')
 const {closeWindow} = require('./window-helpers')
 
+const {expect} = chai
+chai.use(dirtyChai)
+
 const {remote} = require('electron')
 const {ipcMain, BrowserWindow} = remote
 
@@ -45,22 +49,22 @@ describe('ipc main module', () => {
       const listener = () => {}
 
       w.on('test', listener)
-      assert.equal(w.listenerCount('test'), 1)
+      expect(w.listenerCount('test')).to.equal(1)
       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', () => {
     ipcMain.on('test-event', () => {})
-    assert.equal(ipcMain.listenerCount('test-event'), 1)
+    expect(ipcMain.listenerCount('test-event')).to.equal(1)
 
-    assert.throws(() => {
+    expect(() => {
       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')
-    assert.equal(ipcMain.listenerCount('test-event'), 0)
+    expect(ipcMain.listenerCount('test-event')).to.equal(0)
   })
 
   describe('remote objects registry', () => {
@@ -68,7 +72,8 @@ describe('ipc main module', () => {
       w = new BrowserWindow({ show: false })
 
       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()
       })
 

+ 50 - 43
spec/api-ipc-renderer-spec.js

@@ -1,10 +1,14 @@
 'use strict'
 
-const assert = require('assert')
+const chai = require('chai')
+const dirtyChai = require('dirty-chai')
 const http = require('http')
 const path = require('path')
 const {closeWindow} = require('./window-helpers')
 
+const {expect} = chai
+chai.use(dirtyChai)
+
 const {ipcRenderer, remote} = require('electron')
 const {ipcMain, webContents, BrowserWindow} = remote
 
@@ -16,97 +20,97 @@ describe('ipc renderer module', () => {
   afterEach(() => closeWindow(w).then(() => { w = null }))
 
   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 = {
         id: 1,
         name: 'ly'
       }
-      ipcRenderer.once('message', function (event, message) {
-        assert.deepEqual(message, obj)
+      ipcRenderer.once('message', (event, message) => {
+        expect(message).to.deep.equal(obj)
         done()
       })
       ipcRenderer.send('message', obj)
     })
 
-    it('can send instances of Date', (done) => {
+    it('can send instances of Date', done => {
       const currentDate = new Date()
-      ipcRenderer.once('message', function (event, value) {
-        assert.equal(value, currentDate.toISOString())
+      ipcRenderer.once('message', (event, value) => {
+        expect(value).to.equal(currentDate.toISOString())
         done()
       })
       ipcRenderer.send('message', currentDate)
     })
 
-    it('can send instances of Buffer', (done) => {
+    it('can send instances of Buffer', done => {
       const buffer = Buffer.from('hello')
-      ipcRenderer.once('message', function (event, message) {
-        assert.ok(buffer.equals(message))
+      ipcRenderer.once('message', (event, message) => {
+        expect(buffer.equals(message)).to.be.true()
         done()
       })
       ipcRenderer.send('message', buffer)
     })
 
-    it('can send objects with DOM class prototypes', (done) => {
-      ipcRenderer.once('message', function (event, value) {
-        assert.equal(value.protocol, 'file:')
-        assert.equal(value.hostname, '')
+    it('can send objects with DOM class prototypes', done => {
+      ipcRenderer.once('message', (event, value) => {
+        expect(value.protocol).to.equal('file:')
+        expect(value.hostname).to.equal('')
         done()
       })
       ipcRenderer.send('message', document.location)
     })
 
-    it('can send Electron API objects', (done) => {
+    it('can send Electron API objects', done => {
       const webContents = remote.getCurrentWebContents()
-      ipcRenderer.once('message', function (event, value) {
-        assert.deepEqual(value.browserWindowOptions, webContents.browserWindowOptions)
+      ipcRenderer.once('message', (event, value) => {
+        expect(value.browserWindowOptions).to.deep.equal(webContents.browserWindowOptions)
         done()
       })
       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 stream = request.agent.sockets['127.0.0.1:5000:'][0]._handle._externalStream
       request.on('error', () => {})
-      ipcRenderer.once('message', function (event, requestValue, externalStreamValue) {
-        assert.equal(requestValue.method, 'GET')
-        assert.equal(requestValue.path, '/')
-        assert.equal(externalStreamValue, null)
+      ipcRenderer.once('message', (event, requestValue, externalStreamValue) => {
+        expect(requestValue.method).to.equal('GET')
+        expect(requestValue.path).to.equal('/')
+        expect(externalStreamValue).to.be.null()
         done()
       })
 
       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 foo = {name: 'foo', child: child}
       const bar = {name: 'bar', child: child}
       const array = [foo, bar]
 
-      ipcRenderer.once('message', function (event, arrayValue, fooValue, barValue, childValue) {
-        assert.deepEqual(arrayValue, array)
-        assert.deepEqual(fooValue, foo)
-        assert.deepEqual(barValue, bar)
-        assert.deepEqual(childValue, child)
+      ipcRenderer.once('message', (event, arrayValue, fooValue, barValue, childValue) => {
+        expect(arrayValue).to.deep.equal(array)
+        expect(fooValue).to.deep.equal(foo)
+        expect(barValue).to.deep.equal(bar)
+        expect(childValue).to.deep.equal(child)
         done()
       })
       ipcRenderer.send('message', array, foo, bar, child)
     })
 
-    it('inserts null for cyclic references', (done) => {
+    it('inserts null for cyclic references', done => {
       const array = [5]
       array.push(array)
 
       const child = {hello: 'world'}
       child.child = child
 
-      ipcRenderer.once('message', function (event, arrayValue, childValue) {
-        assert.equal(arrayValue[0], 5)
-        assert.equal(arrayValue[1], null)
+      ipcRenderer.once('message', (event, arrayValue, childValue) => {
+        expect(arrayValue[0]).to.equal(5)
+        expect(arrayValue[1]).to.be.null()
 
-        assert.equal(childValue.hello, 'world')
-        assert.equal(childValue.child, null)
+        expect(childValue.hello).to.equal('world')
+        expect(childValue.child).to.be.null()
 
         done()
       })
@@ -121,7 +125,7 @@ describe('ipc renderer module', () => {
 
     it('can be replied by setting event.returnValue', () => {
       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
     })
 
-    it('sends message to WebContents', (done) => {
+    it('sends message to WebContents', done => {
       const webContentsId = remote.getCurrentWebContents().id
 
-      ipcRenderer.once('pong', function (event, id) {
-        assert.equal(webContentsId, id)
+      ipcRenderer.once('pong', (event, id) => {
+        expect(webContentsId).to.equal(id)
         done()
       })
 
@@ -163,14 +167,17 @@ describe('ipc renderer module', () => {
             'Function provided here: remote-event-handler.html:11:33',
             'Remote event names: remote-handler, other-remote-handler'
           ].join('\n')
+
           const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler')
-          assert.deepEqual(results, {
+
+          expect(results).to.deep.equal({
             warningMessage: expectedMessage,
             listenerCountBefore: 2,
             listenerCountAfter: 1
           })
           done()
         })
+
         w.webContents.reload()
       })
       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', () => {
     ipcRenderer.on('test-event', () => {})
-    assert.equal(ipcRenderer.listenerCount('test-event'), 1)
+    expect(ipcRenderer.listenerCount('test-event')).to.equal(1)
 
-    assert.throws(() => {
+    expect(() => {
       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')
-    assert.equal(ipcRenderer.listenerCount('test-event'), 0)
+    expect(ipcRenderer.listenerCount('test-event')).to.equal(0)
   })
 })