spec: convert menu specs to expect (#13485)

* spec: convert menu-item spec to expect

* spec: convert menu spec to expect
This commit is contained in:
Shelley Vohr 2018-06-30 00:25:28 -07:00 committed by GitHub
parent 6cf68f8247
commit 42d173b343
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 118 additions and 110 deletions

View file

@ -1,18 +1,22 @@
const assert = require('assert')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const {remote} = require('electron')
const {BrowserWindow, app, Menu, MenuItem} = remote
const roles = require('../lib/browser/api/menu-item-roles')
const {closeWindow} = require('./window-helpers')
const {expect} = chai
chai.use(dirtyChai)
describe('MenuItems', () => {
describe('MenuItem.click', () => {
it('should be called with the item object passed', (done) => {
it('should be called with the item object passed', done => {
const menu = Menu.buildFromTemplate([{
label: 'text',
click: (item) => {
assert.equal(item.constructor.name, 'MenuItem')
assert.equal(item.label, 'text')
expect(item.constructor.name).to.equal('MenuItem')
expect(item.label).to.equal('text')
done()
}
}])
@ -27,9 +31,9 @@ describe('MenuItems', () => {
type: 'checkbox'
}])
assert.equal(menu.items[0].checked, false)
expect(menu.items[0].checked).to.be.false()
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
assert.equal(menu.items[0].checked, true)
expect(menu.items[0].checked).to.be.true()
})
it('clicking an radio item should always make checked property true', () => {
@ -39,9 +43,9 @@ describe('MenuItems', () => {
}])
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
assert.equal(menu.items[0].checked, true)
expect(menu.items[0].checked).to.be.true()
menu.delegate.executeCommand(menu, {}, menu.items[0].commandId)
assert.equal(menu.items[0].checked, true)
expect(menu.items[0].checked).to.be.true()
})
describe('MenuItem group properties', () => {
@ -96,7 +100,7 @@ describe('MenuItems', () => {
const groups = findRadioGroups(template)
groups.forEach(g => {
assert.deepEqual(findChecked(menu.items, g.begin, g.end), [g.begin])
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([g.begin])
})
})
@ -109,12 +113,12 @@ describe('MenuItems', () => {
const groupId = menu.items[g.begin].groupId
// groupId should be previously unused
assert(!usedGroupIds.has(groupId))
expect(usedGroupIds.has(groupId)).to.be.false()
usedGroupIds.add(groupId)
// everything in the group should have the same id
for (let i = g.begin; i < g.end; ++i) {
assert.equal(menu.items[i].groupId, groupId)
expect(menu.items[i].groupId).to.equal(groupId)
}
})
})
@ -124,13 +128,13 @@ describe('MenuItems', () => {
const groups = findRadioGroups(template)
groups.forEach(g => {
assert.deepEqual(findChecked(menu.items, g.begin, g.end), [])
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([])
menu.items[g.begin].checked = true
assert.deepEqual(findChecked(menu.items, g.begin, g.end), [g.begin])
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([g.begin])
menu.items[g.end - 1].checked = true
assert.deepEqual(findChecked(menu.items, g.begin, g.end), [g.end - 1])
expect(findChecked(menu.items, g.begin, g.end)).to.deep.equal([g.end - 1])
})
})
})
@ -142,7 +146,7 @@ describe('MenuItems', () => {
let item = new MenuItem({role: 'asdfghjkl'})
const canExecute = roles.execute(item.role, win, win.webContents)
assert.equal(false, canExecute)
expect(canExecute).to.be.false()
closeWindow(win).then(() => { win = null })
})
@ -152,7 +156,7 @@ describe('MenuItems', () => {
let item = new MenuItem({role: 'reload'})
const canExecute = roles.execute(item.role, win, win.webContents)
assert.equal(true, canExecute)
expect(canExecute).to.be.true()
closeWindow(win).then(() => { win = null })
})
@ -162,7 +166,7 @@ describe('MenuItems', () => {
let item = new MenuItem({role: 'resetzoom'})
const canExecute = roles.execute(item.role, win, win.webContents)
assert.equal(true, canExecute)
expect(canExecute).to.be.true()
closeWindow(win).then(() => { win = null })
})
@ -173,31 +177,31 @@ describe('MenuItems', () => {
const item = new MenuItem({label: 'item'})
const commandId = item.commandId
assert(commandId)
expect(commandId).to.not.be.undefined()
item.commandId = `${commandId}-modified`
assert.equal(item.commandId, commandId)
expect(item.commandId).to.equal(commandId)
})
})
describe('MenuItem with invalid type', () => {
it('throws an exception', () => {
assert.throws(() => {
expect(() => {
Menu.buildFromTemplate([{
label: 'text',
type: 'not-a-type'
}])
}, /Unknown menu item type: not-a-type/)
}).to.throw(/Unknown menu item type: not-a-type/)
})
})
describe('MenuItem with submenu type and missing submenu', () => {
it('throws an exception', () => {
assert.throws(() => {
expect(() => {
Menu.buildFromTemplate([{
label: 'text',
type: 'submenu'
}])
}, /Invalid submenu/)
}).to.throw(/Invalid submenu/)
})
})
@ -227,7 +231,7 @@ describe('MenuItems', () => {
for (let role in roleList) {
const item = new MenuItem({role})
assert.equal(item.getDefaultRoleAccelerator(), undefined)
expect(item.getDefaultRoleAccelerator()).to.be.undefined()
}
})
@ -256,7 +260,7 @@ describe('MenuItems', () => {
for (let role in roleList) {
const item = new MenuItem({role})
assert.equal(item.label, roleList[role])
expect(item.label).to.equal(roleList[role])
}
})
@ -285,7 +289,7 @@ describe('MenuItems', () => {
for (let role in roleList) {
const item = new MenuItem({role})
assert.equal(item.getDefaultRoleAccelerator(), roleList[role])
expect(item.getDefaultRoleAccelerator()).to.equal(roleList[role])
}
})
@ -296,9 +300,9 @@ describe('MenuItems', () => {
accelerator: 'D'
})
assert.equal(item.label, 'Custom Close!')
assert.equal(item.accelerator, 'D')
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W')
expect(item.label).to.equal('Custom Close!')
expect(item.accelerator).to.equal('D')
expect(item.getDefaultRoleAccelerator()).to.equal('CommandOrControl+W')
})
})
@ -306,24 +310,24 @@ describe('MenuItems', () => {
it('includes a default submenu layout when submenu is empty', () => {
const item = new MenuItem({role: 'editMenu'})
assert.equal(item.label, 'Edit')
assert.equal(item.submenu.items[0].role, 'undo')
assert.equal(item.submenu.items[1].role, 'redo')
assert.equal(item.submenu.items[2].type, 'separator')
assert.equal(item.submenu.items[3].role, 'cut')
assert.equal(item.submenu.items[4].role, 'copy')
assert.equal(item.submenu.items[5].role, 'paste')
expect(item.label).to.equal('Edit')
expect(item.submenu.items[0].role).to.equal('undo')
expect(item.submenu.items[1].role).to.equal('redo')
expect(item.submenu.items[2].type).to.equal('separator')
expect(item.submenu.items[3].role).to.equal('cut')
expect(item.submenu.items[4].role).to.equal('copy')
expect(item.submenu.items[5].role).to.equal('paste')
if (process.platform === 'darwin') {
assert.equal(item.submenu.items[6].role, 'pasteandmatchstyle')
assert.equal(item.submenu.items[7].role, 'delete')
assert.equal(item.submenu.items[8].role, 'selectall')
expect(item.submenu.items[6].role).to.equal('pasteandmatchstyle')
expect(item.submenu.items[7].role).to.equal('delete')
expect(item.submenu.items[8].role).to.equal('selectall')
}
if (process.platform === 'win32') {
assert.equal(item.submenu.items[6].role, 'delete')
assert.equal(item.submenu.items[7].type, 'separator')
assert.equal(item.submenu.items[8].role, 'selectall')
expect(item.submenu.items[6].role).to.equal('delete')
expect(item.submenu.items[7].type).to.equal('separator')
expect(item.submenu.items[8].role).to.equal('selectall')
}
})
@ -334,8 +338,8 @@ describe('MenuItems', () => {
role: 'close'
}]
})
assert.equal(item.label, 'Edit')
assert.equal(item.submenu.items[0].role, 'close')
expect(item.label).to.equal('Edit')
expect(item.submenu.items[0].role).to.equal('close')
})
})
@ -343,13 +347,13 @@ describe('MenuItems', () => {
it('includes a default submenu layout when submenu is empty', () => {
const item = new MenuItem({role: 'windowMenu'})
assert.equal(item.label, 'Window')
assert.equal(item.submenu.items[0].role, 'minimize')
assert.equal(item.submenu.items[1].role, 'close')
expect(item.label).to.equal('Window')
expect(item.submenu.items[0].role).to.equal('minimize')
expect(item.submenu.items[1].role).to.equal('close')
if (process.platform === 'darwin') {
assert.equal(item.submenu.items[2].type, 'separator')
assert.equal(item.submenu.items[3].role, 'front')
expect(item.submenu.items[2].type).to.equal('separator')
expect(item.submenu.items[3].role).to.equal('front')
}
})
@ -359,8 +363,8 @@ describe('MenuItems', () => {
submenu: [{role: 'copy'}]
})
assert.equal(item.label, 'Window')
assert.equal(item.submenu.items[0].role, 'copy')
expect(item.label).to.equal('Window')
expect(item.submenu.items[0].role).to.equal('copy')
})
})
@ -379,10 +383,10 @@ describe('MenuItems', () => {
overrideProperty: 'oops not allowed'
}))
assert.equal(menu.items[0].customProp, 'foo')
assert.equal(menu.items[0].submenu.items[0].label, 'item 1')
assert.equal(menu.items[0].submenu.items[0].customProp, 'bar')
assert.equal(typeof menu.items[0].submenu.items[0].overrideProperty, 'function')
expect(menu.items[0].customProp).to.equal('foo')
expect(menu.items[0].submenu.items[0].label).to.equal('item 1')
expect(menu.items[0].submenu.items[0].customProp).to.equal('bar')
expect(menu.items[0].submenu.items[0].overrideProperty).to.be.a('function')
})
})
})

View file

@ -1,10 +1,14 @@
const assert = require('assert')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const {ipcRenderer, remote} = require('electron')
const {BrowserWindow, Menu, MenuItem} = remote
const {sortMenuItems} = require('../lib/browser/api/menu-utils')
const {closeWindow} = require('./window-helpers')
const {expect} = chai
chai.use(dirtyChai)
describe('Menu module', () => {
describe('Menu.buildFromTemplate', () => {
it('should be able to attach extra fields', () => {
@ -14,17 +18,17 @@ describe('Menu module', () => {
extra: 'field'
}
])
assert.equal(menu.items[0].extra, 'field')
expect(menu.items[0].extra).to.equal('field')
})
it('does not modify the specified template', () => {
const template = [{label: 'text', submenu: [{label: 'sub'}]}]
const result = ipcRenderer.sendSync('eval', `const template = [{label: 'text', submenu: [{label: 'sub'}]}]\nrequire('electron').Menu.buildFromTemplate(template)\ntemplate`)
assert.deepStrictEqual(result, template)
expect(result).to.deep.equal(template)
})
it('does not throw exceptions for undefined/null values', () => {
assert.doesNotThrow(() => {
expect(() => {
Menu.buildFromTemplate([
{
label: 'text',
@ -35,7 +39,7 @@ describe('Menu module', () => {
accelerator: null
}
])
})
}).to.not.throw()
})
describe('Menu sorting and building', () => {
@ -66,7 +70,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('resolves cycles by ignoring things that conflict', () => {
@ -98,7 +102,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('ignores references to commands that do not exist', () => {
@ -128,7 +132,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('only respects the first matching [before|after]GroupContaining rule in a given group', () => {
@ -178,7 +182,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
})
@ -220,7 +224,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it("moves all items in the moving item's group", () => {
@ -268,7 +272,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it("ignores positions relative to commands that don't exist", () => {
@ -318,7 +322,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('can handle recursive group merging', () => {
@ -356,7 +360,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('can merge multiple groups when given a list of before/after commands', () => {
@ -394,7 +398,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
it('can merge multiple groups based on both before/after commands', () => {
@ -434,7 +438,7 @@ describe('Menu module', () => {
}
]
assert.deepEqual(sortMenuItems(items), expected)
expect(sortMenuItems(items)).to.deep.equal(expected)
})
})
@ -453,9 +457,9 @@ describe('Menu module', () => {
}
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
expect(menu.items[0].label).to.equal('one')
expect(menu.items[1].label).to.equal('two')
expect(menu.items[2].label).to.equal('three')
})
it('should position after existing item', () => {
@ -474,9 +478,9 @@ describe('Menu module', () => {
}
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
expect(menu.items[0].label).to.equal('one')
expect(menu.items[1].label).to.equal('two')
expect(menu.items[2].label).to.equal('three')
})
it('should filter excess menu separators', () => {
@ -494,10 +498,10 @@ describe('Menu module', () => {
}
])
assert.equal(menuOne.items.length, 3)
assert.equal(menuOne.items[0].label, 'a')
assert.equal(menuOne.items[1].label, 'b')
assert.equal(menuOne.items[2].label, 'c')
expect(menuOne.items).to.have.length(3)
expect(menuOne.items[0].label).to.equal('a')
expect(menuOne.items[1].label).to.equal('b')
expect(menuOne.items[2].label).to.equal('c')
const menuTwo = Menu.buildFromTemplate([
{
@ -517,10 +521,10 @@ describe('Menu module', () => {
}
])
assert.equal(menuTwo.items.length, 3)
assert.equal(menuTwo.items[0].label, 'a')
assert.equal(menuTwo.items[1].label, 'b')
assert.equal(menuTwo.items[2].label, 'c')
expect(menuTwo.items).to.have.length(3)
expect(menuTwo.items[0].label).to.equal('a')
expect(menuTwo.items[1].label).to.equal('b')
expect(menuTwo.items[2].label).to.equal('c')
})
it('should continue inserting items at next index when no specifier is present', () => {
@ -544,11 +548,11 @@ describe('Menu module', () => {
}
])
assert.equal(menu.items[0].label, 'one')
assert.equal(menu.items[1].label, 'two')
assert.equal(menu.items[2].label, 'three')
assert.equal(menu.items[3].label, 'four')
assert.equal(menu.items[4].label, 'five')
expect(menu.items[0].label).to.equal('one')
expect(menu.items[1].label).to.equal('two')
expect(menu.items[2].label).to.equal('three')
expect(menu.items[3].label).to.equal('four')
expect(menu.items[4].label).to.equal('five')
})
})
})
@ -568,7 +572,7 @@ describe('Menu module', () => {
}
])
const fsc = menu.getMenuItemById('fullScreen')
assert.equal(menu.items[0].submenu.items[0], fsc)
expect(menu.items[0].submenu.items[0]).to.equal(fsc)
})
})
@ -583,10 +587,10 @@ describe('Menu module', () => {
const item = new MenuItem({ label: 'inserted' })
menu.insert(1, item)
assert.equal(menu.items[0].label, '1')
assert.equal(menu.items[1].label, 'inserted')
assert.equal(menu.items[2].label, '2')
assert.equal(menu.items[3].label, '3')
expect(menu.items[0].label).to.equal('1')
expect(menu.items[1].label).to.equal('inserted')
expect(menu.items[2].label).to.equal('2')
expect(menu.items[3].label).to.equal('3')
})
})
@ -601,10 +605,10 @@ describe('Menu module', () => {
const item = new MenuItem({ label: 'inserted' })
menu.append(item)
assert.equal(menu.items[0].label, '1')
assert.equal(menu.items[1].label, '2')
assert.equal(menu.items[2].label, '3')
assert.equal(menu.items[3].label, 'inserted')
expect(menu.items[0].label).to.equal('1')
expect(menu.items[1].label).to.equal('2')
expect(menu.items[2].label).to.equal('3')
expect(menu.items[3].label).to.equal('inserted')
})
})
@ -628,9 +632,9 @@ describe('Menu module', () => {
})
it('throws an error if options is not an object', () => {
assert.throws(() => {
expect(() => {
menu.popup()
}, /Options must be an object/)
}).to.throw(/Options must be an object/)
})
it('should emit menu-will-show event', (done) => {
@ -647,17 +651,17 @@ describe('Menu module', () => {
it('returns immediately', () => {
const input = {window: w, x: 100, y: 101}
const output = menu.popup(input)
assert.equal(output.x, input.x)
assert.equal(output.y, input.y)
assert.equal(output.browserWindow, input.window)
expect(output.x).to.equal(input.x)
expect(output.y).to.equal(input.y)
expect(output.browserWindow).to.equal(input.window)
})
it('works without a given BrowserWindow and options', () => {
const {browserWindow, x, y} = menu.popup({x: 100, y: 101})
assert.equal(browserWindow.constructor.name, 'BrowserWindow')
assert.equal(x, 100)
assert.equal(y, 101)
expect(browserWindow.constructor.name).to.equal('BrowserWindow')
expect(x).to.equal(100)
expect(y).to.equal(101)
})
it('works with a given BrowserWindow, options and callback', (done) => {
@ -668,8 +672,8 @@ describe('Menu module', () => {
callback: () => done()
})
assert.equal(x, 100)
assert.equal(y, 101)
expect(x).to.equal(100)
expect(y).to.equal(101)
menu.closePopup()
})
@ -687,12 +691,12 @@ describe('Menu module', () => {
])
Menu.setApplicationMenu(menu)
assert.notEqual(Menu.getApplicationMenu(), null)
expect(Menu.getApplicationMenu()).to.not.be.null()
})
it('unsets a menu with null', () => {
Menu.setApplicationMenu(null)
assert.equal(Menu.getApplicationMenu(), null)
expect(Menu.getApplicationMenu()).to.be.null()
})
})
})