upgrade menu spec to ES6

This commit is contained in:
Shelley Vohr 2017-10-24 20:27:26 -04:00
parent e8935232b1
commit 0e6100ae17
No known key found for this signature in database
GPG key ID: F13993A75599653C

View file

@ -4,10 +4,10 @@ const {ipcRenderer, remote} = require('electron')
const {BrowserWindow, Menu, MenuItem} = remote const {BrowserWindow, Menu, MenuItem} = remote
const {closeWindow} = require('./window-helpers') const {closeWindow} = require('./window-helpers')
describe('menu module', function () { describe.only('menu module', function () {
describe('Menu.buildFromTemplate', function () { describe('Menu.buildFromTemplate', function () {
it('should be able to attach extra fields', function () { it('should be able to attach extra fields', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
extra: 'field' extra: 'field'
@ -17,7 +17,7 @@ describe('menu module', function () {
}) })
it('does not modify the specified template', function () { it('does not modify the specified template', function () {
var template = ipcRenderer.sendSync('eval', "var template = [{label: 'text', submenu: [{label: 'sub'}]}];\nrequire('electron').Menu.buildFromTemplate(template);\ntemplate;") const template = ipcRenderer.sendSync('eval', "var template = [{label: 'text', submenu: [{label: 'sub'}]}];\nrequire('electron').Menu.buildFromTemplate(template);\ntemplate;")
assert.deepStrictEqual(template, [ assert.deepStrictEqual(template, [
{ {
label: 'text', label: 'text',
@ -47,7 +47,7 @@ describe('menu module', function () {
describe('Menu.buildFromTemplate should reorder based on item position specifiers', function () { describe('Menu.buildFromTemplate should reorder based on item position specifiers', function () {
it('should position before existing item', function () { it('should position before existing item', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: '2', label: '2',
id: '2' id: '2'
@ -66,7 +66,7 @@ describe('menu module', function () {
}) })
it('should position after existing item', function () { it('should position after existing item', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: '1', label: '1',
id: '1' id: '1'
@ -85,7 +85,7 @@ describe('menu module', function () {
}) })
it('should position at endof existing separator groups', function () { it('should position at endof existing separator groups', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
type: 'separator', type: 'separator',
id: 'numbers' id: 'numbers'
@ -129,7 +129,7 @@ describe('menu module', function () {
}) })
it('should create separator group if endof does not reference existing separator group', function () { it('should create separator group if endof does not reference existing separator group', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'a', label: 'a',
id: 'a', id: 'a',
@ -167,7 +167,7 @@ describe('menu module', function () {
}) })
it('should continue inserting items at next index when no specifier is present', function () { it('should continue inserting items at next index when no specifier is present', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: '4', label: '4',
id: '4' id: '4'
@ -197,7 +197,7 @@ describe('menu module', function () {
describe('Menu.getMenuItemById', function () { describe('Menu.getMenuItemById', function () {
it('should return the item with the given id', function () { it('should return the item with the given id', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'View', label: 'View',
submenu: [ submenu: [
@ -220,7 +220,7 @@ describe('menu module', function () {
describe('Menu.insert', function () { describe('Menu.insert', function () {
it('should store item in @items by its index', function () { it('should store item in @items by its index', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: '1' label: '1'
}, { }, {
@ -229,9 +229,8 @@ describe('menu module', function () {
label: '3' label: '3'
} }
]) ])
var item = new MenuItem({ const item = new MenuItem({ label: 'inserted' })
label: 'inserted'
})
menu.insert(1, item) menu.insert(1, item)
assert.equal(menu.items[0].label, '1') assert.equal(menu.items[0].label, '1')
assert.equal(menu.items[1].label, 'inserted') assert.equal(menu.items[1].label, 'inserted')
@ -242,15 +241,11 @@ describe('menu module', function () {
describe('Menu.popup', function () { describe('Menu.popup', function () {
let w = null let w = null
let menu
afterEach(function () { beforeEach(() => {
return closeWindow(w).then(function () { w = null })
})
describe('when called with async: true', function () {
it('returns immediately', function () {
w = new BrowserWindow({show: false, width: 200, height: 200}) w = new BrowserWindow({show: false, width: 200, height: 200})
const menu = Menu.buildFromTemplate([ menu = Menu.buildFromTemplate([
{ {
label: '1' label: '1'
}, { }, {
@ -259,6 +254,14 @@ describe('menu module', function () {
label: '3' label: '3'
} }
]) ])
})
afterEach(() => {
return closeWindow(w).then(function () { w = null })
})
describe('when called with async: true', function () {
it('returns immediately', function () {
menu.popup(w, {x: 100, y: 100, async: true}) menu.popup(w, {x: 100, y: 100, async: true})
menu.closePopup(w) menu.closePopup(w)
}) })
@ -266,7 +269,7 @@ describe('menu module', function () {
}) })
describe('MenuItem.click', function () { describe('MenuItem.click', function () {
it('should be called with the item object passed', function (done) { it('should be called with the item object passed', function (done) {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
click: function (item) { click: function (item) {
@ -282,7 +285,7 @@ describe('menu module', function () {
describe('MenuItem with checked property', function () { describe('MenuItem with checked property', function () {
it('clicking an checkbox item should flip the checked property', function () { it('clicking an checkbox item should flip the checked property', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
type: 'checkbox' type: 'checkbox'
@ -294,7 +297,7 @@ describe('menu module', function () {
}) })
it('clicking an radio item should always make checked property true', function () { it('clicking an radio item should always make checked property true', function () {
var menu = Menu.buildFromTemplate([ const menu = Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
type: 'radio' type: 'radio'
@ -307,99 +310,91 @@ describe('menu module', function () {
}) })
it('at least have one item checked in each group', function () { it('at least have one item checked in each group', function () {
var i, j, k, menu, template const template = []
template = [] for (let i = 0; i <= 10; i++) {
for (i = j = 0; j <= 10; i = ++j) {
template.push({ template.push({
label: '' + i, label: `${i}`,
type: 'radio' type: 'radio'
}) })
} }
template.push({type: 'separator'})
for (let i = 12; i <= 20; i++) {
template.push({ template.push({
type: 'separator' label: `${i}`,
})
for (i = k = 12; k <= 20; i = ++k) {
template.push({
label: '' + i,
type: 'radio' type: 'radio'
}) })
} }
menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
menu.delegate.menuWillShow() menu.delegate.menuWillShow()
assert.equal(menu.items[0].checked, true) assert.equal(menu.items[0].checked, true)
assert.equal(menu.items[12].checked, true) assert.equal(menu.items[12].checked, true)
}) })
it('should assign groupId automatically', function () { it('should assign groupId automatically', function () {
var groupId, i, j, k, l, m, menu, template const template = []
template = [] for (let i = 0; i <= 10; i++) {
for (i = j = 0; j <= 10; i = ++j) {
template.push({ template.push({
label: '' + i, label: `${i}`,
type: 'radio' type: 'radio'
}) })
} }
template.push({type: 'separator'})
for (let i = 12; i <= 20; i++) {
template.push({ template.push({
type: 'separator' label: `${i}`,
})
for (i = k = 12; k <= 20; i = ++k) {
template.push({
label: '' + i,
type: 'radio' type: 'radio'
}) })
} }
menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
groupId = menu.items[0].groupId const groupId = menu.items[0].groupId
for (i = l = 0; l <= 10; i = ++l) { for (let i = 0; i <= 10; i++) {
assert.equal(menu.items[i].groupId, groupId) assert.equal(menu.items[i].groupId, groupId)
} }
for (i = m = 12; m <= 20; i = ++m) { for (let i = 12; i <= 20; i++) {
assert.equal(menu.items[i].groupId, groupId + 1) assert.equal(menu.items[i].groupId, groupId + 1)
} }
}) })
it("setting 'checked' should flip other items' 'checked' property", function () { it("setting 'checked' should flip other items' 'checked' property", function () {
var i, j, k, l, m, menu, n, o, p, q, template const template = []
template = [] for (let i = 0; i <= 10; i++) {
for (i = j = 0; j <= 10; i = ++j) {
template.push({ template.push({
label: '' + i, label: `${i}`,
type: 'radio' type: 'radio'
}) })
} }
template.push({type: 'separator'})
for (let i = 12; i <= 20; i++) {
template.push({ template.push({
type: 'separator' label: `${i}`,
})
for (i = k = 12; k <= 20; i = ++k) {
template.push({
label: '' + i,
type: 'radio' type: 'radio'
}) })
} }
menu = Menu.buildFromTemplate(template)
for (i = l = 0; l <= 10; i = ++l) { const menu = Menu.buildFromTemplate(template)
for (let i = 0; i <= 10; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
menu.items[0].checked = true menu.items[0].checked = true
assert.equal(menu.items[0].checked, true) assert.equal(menu.items[0].checked, true)
for (i = m = 1; m <= 10; i = ++m) { for (let i = 1; i <= 10; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
menu.items[10].checked = true menu.items[10].checked = true
assert.equal(menu.items[10].checked, true) assert.equal(menu.items[10].checked, true)
for (i = n = 0; n <= 9; i = ++n) { for (let i = 0; i <= 9; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
for (i = o = 12; o <= 20; i = ++o) { for (let i = 12; i <= 20; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
menu.items[12].checked = true menu.items[12].checked = true
assert.equal(menu.items[10].checked, true) assert.equal(menu.items[10].checked, true)
for (i = p = 0; p <= 9; i = ++p) { for (let i = 0; i <= 9; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
assert.equal(menu.items[12].checked, true) assert.equal(menu.items[12].checked, true)
for (i = q = 13; q <= 20; i = ++q) { for (let i = 13; i <= 20; i++) {
assert.equal(menu.items[i].checked, false) assert.equal(menu.items[i].checked, false)
} }
}) })
@ -407,20 +402,18 @@ describe('menu module', function () {
describe('MenuItem command id', function () { describe('MenuItem command id', function () {
it('cannot be overwritten', function () { it('cannot be overwritten', function () {
var item = new MenuItem({ const item = new MenuItem({label: 'item'})
label: 'item'
})
var commandId = item.commandId const commandId = item.commandId
assert(commandId != null) assert(commandId)
item.commandId = '' + commandId + '-modified' item.commandId = `${commandId}-modified`
assert.equal(item.commandId, commandId) assert.equal(item.commandId, commandId)
}) })
}) })
describe('MenuItem with invalid type', function () { describe('MenuItem with invalid type', function () {
it('throws an exception', function () { it('throws an exception', function () {
assert.throws(function () { assert.throws(() => {
Menu.buildFromTemplate([ Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
@ -433,7 +426,7 @@ describe('menu module', function () {
describe('MenuItem with submenu type and missing submenu', function () { describe('MenuItem with submenu type and missing submenu', function () {
it('throws an exception', function () { it('throws an exception', function () {
assert.throws(function () { assert.throws(() => {
Menu.buildFromTemplate([ Menu.buildFromTemplate([
{ {
label: 'text', label: 'text',
@ -446,7 +439,7 @@ describe('menu module', function () {
describe('MenuItem role', function () { describe('MenuItem role', function () {
it('includes a default label and accelerator', function () { it('includes a default label and accelerator', function () {
var item = new MenuItem({role: 'close'}) let item = new MenuItem({role: 'close'})
assert.equal(item.label, process.platform === 'darwin' ? 'Close Window' : 'Close') assert.equal(item.label, process.platform === 'darwin' ? 'Close Window' : 'Close')
assert.equal(item.accelerator, undefined) assert.equal(item.accelerator, undefined)
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W') assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W')
@ -480,7 +473,7 @@ describe('menu module', function () {
describe('MenuItem editMenu', function () { describe('MenuItem editMenu', function () {
it('includes a default submenu layout when submenu is empty', function () { it('includes a default submenu layout when submenu is empty', function () {
var item = new MenuItem({role: 'editMenu'}) const item = new MenuItem({role: 'editMenu'})
assert.equal(item.label, 'Edit') assert.equal(item.label, 'Edit')
assert.equal(item.submenu.items[0].role, 'undo') assert.equal(item.submenu.items[0].role, 'undo')
assert.equal(item.submenu.items[1].role, 'redo') assert.equal(item.submenu.items[1].role, 'redo')
@ -503,7 +496,7 @@ describe('menu module', function () {
}) })
it('overrides default layout when submenu is specified', function () { it('overrides default layout when submenu is specified', function () {
var item = new MenuItem({role: 'editMenu', submenu: [{role: 'close'}]}) const item = new MenuItem({role: 'editMenu', submenu: [{role: 'close'}]})
assert.equal(item.label, 'Edit') assert.equal(item.label, 'Edit')
assert.equal(item.submenu.items[0].role, 'close') assert.equal(item.submenu.items[0].role, 'close')
}) })
@ -511,7 +504,7 @@ describe('menu module', function () {
describe('MenuItem windowMenu', function () { describe('MenuItem windowMenu', function () {
it('includes a default submenu layout when submenu is empty', function () { it('includes a default submenu layout when submenu is empty', function () {
var item = new MenuItem({role: 'windowMenu'}) const item = new MenuItem({role: 'windowMenu'})
assert.equal(item.label, 'Window') assert.equal(item.label, 'Window')
assert.equal(item.submenu.items[0].role, 'minimize') assert.equal(item.submenu.items[0].role, 'minimize')
assert.equal(item.submenu.items[1].role, 'close') assert.equal(item.submenu.items[1].role, 'close')
@ -523,7 +516,7 @@ describe('menu module', function () {
}) })
it('overrides default layout when submenu is specified', function () { it('overrides default layout when submenu is specified', function () {
var item = new MenuItem({role: 'windowMenu', submenu: [{role: 'copy'}]}) const item = new MenuItem({role: 'windowMenu', submenu: [{role: 'copy'}]})
assert.equal(item.label, 'Window') assert.equal(item.label, 'Window')
assert.equal(item.submenu.items[0].role, 'copy') assert.equal(item.submenu.items[0].role, 'copy')
}) })
@ -531,13 +524,13 @@ describe('menu module', function () {
describe('MenuItem with custom properties in constructor', function () { describe('MenuItem with custom properties in constructor', function () {
it('preserves the custom properties', function () { it('preserves the custom properties', function () {
var template = [{ const template = [{
label: 'menu 1', label: 'menu 1',
customProp: 'foo', customProp: 'foo',
submenu: [] submenu: []
}] }]
var menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
menu.items[0].submenu.append(new MenuItem({ menu.items[0].submenu.append(new MenuItem({
label: 'item 1', label: 'item 1',
customProp: 'bar', customProp: 'bar',