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
This commit is contained in:
parent
fe7947da90
commit
d5dfb19508
2 changed files with 100 additions and 56 deletions
|
@ -1,6 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
|
const {expect} = require('chai')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const os = require('os')
|
const os = require('os')
|
||||||
|
@ -2740,64 +2741,104 @@ describe('BrowserWindow module', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('BrowserWindow.addDevToolsExtension', () => {
|
describe('BrowserWindow.addDevToolsExtension', () => {
|
||||||
beforeEach(() => {
|
describe('for invalid extensions', () => {
|
||||||
BrowserWindow.removeDevToolsExtension('foo')
|
it('throws errors for missing manifest.json files', () => {
|
||||||
assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false)
|
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')
|
it('throws errors for invalid manifest.json files', () => {
|
||||||
BrowserWindow.addDevToolsExtension(extensionPath)
|
const badManifestExtensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest')
|
||||||
assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true)
|
expect(() => {
|
||||||
|
BrowserWindow.addDevToolsExtension(badManifestExtensionPath)
|
||||||
showLastDevToolsPanel()
|
}).to.throw(/Unexpected token }/)
|
||||||
|
|
||||||
w.loadURL('about:blank')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws errors for missing manifest.json files', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist'))
|
|
||||||
}, /ENOENT: no such file or directory/)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('throws errors for invalid manifest.json files', () => {
|
|
||||||
assert.throws(() => {
|
|
||||||
BrowserWindow.addDevToolsExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
|
|
||||||
}, /Unexpected token }/)
|
|
||||||
})
|
|
||||||
|
|
||||||
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: {}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when the devtools is undocked', () => {
|
describe('for a valid extension', () => {
|
||||||
it('creates the extension', (done) => {
|
const extensionName = 'foo'
|
||||||
w.webContents.openDevTools({mode: 'undocked'})
|
|
||||||
|
|
||||||
ipcMain.once('answer', function (event, message, extensionId) {
|
const removeExtension = () => {
|
||||||
assert.equal(message.runtimeId, 'foo')
|
BrowserWindow.removeDevToolsExtension('foo')
|
||||||
assert.equal(message.tabId, w.webContents.id)
|
expect(BrowserWindow.getDevToolsExtensions().hasOwnProperty(extensionName)).to.equal(false)
|
||||||
done()
|
}
|
||||||
|
|
||||||
|
const addExtension = () => {
|
||||||
|
const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
|
||||||
|
BrowserWindow.addDevToolsExtension(extensionPath)
|
||||||
|
expect(BrowserWindow.getDevToolsExtensions().hasOwnProperty(extensionName)).to.equal(true)
|
||||||
|
|
||||||
|
showLastDevToolsPanel()
|
||||||
|
|
||||||
|
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: {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
|
const {expect} = require('chai')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
const url = require('url')
|
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', () => {
|
it('throws a custom error when an API method is called before the event is emitted', () => {
|
||||||
assert.throws(() => {
|
const expectedErrorMessage =
|
||||||
webview.stop()
|
'Cannot call stop because the webContents is unavailable. ' +
|
||||||
}, '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.')
|
'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)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue