Browse Source

Update some tests (#12917)

* Use Chai for webview tests

* Slightly rewrite one of the <webview> tests

"dom-ready event" > "throws a custom error..."

* Use Chai for BrowserWindow tests

* Rewrite BrowserWindow.addDevToolsExtension tests
Alexey Kuzmin 7 years ago
parent
commit
d5dfb19508
2 changed files with 94 additions and 50 deletions
  1. 88 47
      spec/api-browser-window-spec.js
  2. 6 3
      spec/webview-spec.js

+ 88 - 47
spec/api-browser-window-spec.js

@@ -1,6 +1,7 @@
 'use strict'
 
 const assert = require('assert')
+const {expect} = require('chai')
 const fs = require('fs')
 const path = require('path')
 const os = require('os')
@@ -2740,64 +2741,104 @@ describe('BrowserWindow module', () => {
     })
 
     describe('BrowserWindow.addDevToolsExtension', () => {
-      beforeEach(() => {
-        BrowserWindow.removeDevToolsExtension('foo')
-        assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false)
+      describe('for invalid extensions', () => {
+        it('throws errors for missing manifest.json files', () => {
+          const nonexistentExtensionPath = path.join(__dirname, 'does-not-exist')
+          expect(() => {
+            BrowserWindow.addDevToolsExtension(nonexistentExtensionPath)
+          }).to.throw(/ENOENT: no such file or directory/)
+        })
 
-        var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
-        BrowserWindow.addDevToolsExtension(extensionPath)
-        assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true)
+        it('throws errors for invalid manifest.json files', () => {
+          const badManifestExtensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest')
+          expect(() => {
+            BrowserWindow.addDevToolsExtension(badManifestExtensionPath)
+          }).to.throw(/Unexpected token }/)
+        })
+      })
 
-        showLastDevToolsPanel()
+      describe('for a valid extension', () => {
+        const extensionName = 'foo'
 
-        w.loadURL('about:blank')
-      })
+        const removeExtension = () => {
+          BrowserWindow.removeDevToolsExtension('foo')
+          expect(BrowserWindow.getDevToolsExtensions().hasOwnProperty(extensionName)).to.equal(false)
+        }
 
-      it('throws errors for missing manifest.json files', () => {
-        assert.throws(() => {
-          BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist'))
-        }, /ENOENT: no such file or directory/)
-      })
+        const addExtension = () => {
+          const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
+          BrowserWindow.addDevToolsExtension(extensionPath)
+          expect(BrowserWindow.getDevToolsExtensions().hasOwnProperty(extensionName)).to.equal(true)
 
-      it('throws errors for invalid manifest.json files', () => {
-        assert.throws(() => {
-          BrowserWindow.addDevToolsExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
-        }, /Unexpected token }/)
-      })
+          showLastDevToolsPanel()
 
-      describe('when the devtools is docked', () => {
-        it('creates the extension', (done) => {
-          w.webContents.openDevTools({mode: 'bottom'})
-
-          ipcMain.once('answer', function (event, message) {
-            assert.equal(message.runtimeId, 'foo')
-            assert.equal(message.tabId, w.webContents.id)
-            assert.equal(message.i18nString, 'foo - bar (baz)')
-            assert.deepEqual(message.storageItems, {
-              local: {
-                set: {hello: 'world', world: 'hello'},
-                remove: {world: 'hello'},
-                clear: {}
-              },
-              sync: {
-                set: {foo: 'bar', bar: 'foo'},
-                remove: {foo: 'bar'},
-                clear: {}
-              }
+          w.loadURL('about:blank')
+        }
+
+        // After* hooks won't be called if a test fail.
+        // So let's make a clean-up in the before hook.
+        beforeEach(removeExtension)
+
+        describe('when the devtools is docked', () => {
+          beforeEach(function (done) {
+            addExtension()
+            w.webContents.openDevTools({mode: 'bottom'})
+            ipcMain.once('answer', (event, message) => {
+              this.message = message
+              done()
+            })
+          })
+
+          describe('created extension info', function () {
+            it('has proper "runtimeId"', function () {
+              expect(this.message).to.have.own.property('runtimeId')
+              expect(this.message.runtimeId).to.equal(extensionName)
+            })
+            it('has "tabId" matching webContents id', function () {
+              expect(this.message).to.have.own.property('tabId')
+              expect(this.message.tabId).to.equal(w.webContents.id)
+            })
+            it('has "i18nString" with proper contents', function () {
+              expect(this.message).to.have.own.property('i18nString')
+              expect(this.message.i18nString).to.equal('foo - bar (baz)')
+            })
+            it('has "storageItems" with proper contents', function () {
+              expect(this.message).to.have.own.property('storageItems')
+              expect(this.message.storageItems).to.deep.equal({
+                local: {
+                  set: {hello: 'world', world: 'hello'},
+                  remove: {world: 'hello'},
+                  clear: {}
+                },
+                sync: {
+                  set: {foo: 'bar', bar: 'foo'},
+                  remove: {foo: 'bar'},
+                  clear: {}
+                }
+              })
             })
-            done()
           })
         })
-      })
 
-      describe('when the devtools is undocked', () => {
-        it('creates the extension', (done) => {
-          w.webContents.openDevTools({mode: 'undocked'})
+        describe('when the devtools is undocked', () => {
+          beforeEach(function (done) {
+            addExtension()
+            w.webContents.openDevTools({mode: 'undocked'})
+            ipcMain.once('answer', (event, message, extensionId) => {
+              this.message = message
+              done()
+            })
+          })
 
-          ipcMain.once('answer', function (event, message, extensionId) {
-            assert.equal(message.runtimeId, 'foo')
-            assert.equal(message.tabId, w.webContents.id)
-            done()
+          describe('created extension info', function () {
+            it('has proper "runtimeId"', function () {
+              expect(this.message).to.have.own.property('runtimeId')
+              expect(this.message.runtimeId).to.equal(extensionName)
+            })
+            it('has "tabId" matching webContents id', function () {
+              expect(this.message).to.have.own.property('tabId')
+              expect(this.message.tabId).to.equal(w.webContents.id)
+            })
           })
         })
       })

+ 6 - 3
spec/webview-spec.js

@@ -1,4 +1,5 @@
 const assert = require('assert')
+const {expect} = require('chai')
 const path = require('path')
 const http = require('http')
 const url = require('url')
@@ -846,9 +847,11 @@ describe('<webview> tag', function () {
     })
 
     it('throws a custom error when an API method is called before the event is emitted', () => {
-      assert.throws(() => {
-        webview.stop()
-      }, 'Cannot call stop because the webContents is unavailable. The WebView must be attached to the DOM and the dom-ready event emitted before this method can be called.')
+      const expectedErrorMessage =
+          'Cannot call stop because the webContents is unavailable. ' +
+          'The WebView must be attached to the DOM ' +
+          'and the dom-ready event emitted before this method can be called.'
+      expect(() => { webview.stop() }).to.throw(expectedErrorMessage)
     })
   })