зеркало из https://github.com/electron/electron.git
upgrade menu spec to ES6
This commit is contained in:
Родитель
e8935232b1
Коммит
0e6100ae17
|
@ -4,10 +4,10 @@ const {ipcRenderer, remote} = require('electron')
|
|||
const {BrowserWindow, Menu, MenuItem} = remote
|
||||
const {closeWindow} = require('./window-helpers')
|
||||
|
||||
describe('menu module', function () {
|
||||
describe.only('menu module', function () {
|
||||
describe('Menu.buildFromTemplate', function () {
|
||||
it('should be able to attach extra fields', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
extra: 'field'
|
||||
|
@ -17,7 +17,7 @@ describe('menu module', 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, [
|
||||
{
|
||||
label: 'text',
|
||||
|
@ -47,7 +47,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('Menu.buildFromTemplate should reorder based on item position specifiers', function () {
|
||||
it('should position before existing item', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '2',
|
||||
id: '2'
|
||||
|
@ -66,7 +66,7 @@ describe('menu module', function () {
|
|||
})
|
||||
|
||||
it('should position after existing item', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '1',
|
||||
id: '1'
|
||||
|
@ -85,7 +85,7 @@ describe('menu module', function () {
|
|||
})
|
||||
|
||||
it('should position at endof existing separator groups', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
type: 'separator',
|
||||
id: 'numbers'
|
||||
|
@ -129,7 +129,7 @@ describe('menu module', 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',
|
||||
id: 'a',
|
||||
|
@ -167,7 +167,7 @@ describe('menu module', 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',
|
||||
id: '4'
|
||||
|
@ -197,7 +197,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('Menu.getMenuItemById', function () {
|
||||
it('should return the item with the given id', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'View',
|
||||
submenu: [
|
||||
|
@ -220,7 +220,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('Menu.insert', function () {
|
||||
it('should store item in @items by its index', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '1'
|
||||
}, {
|
||||
|
@ -229,9 +229,8 @@ describe('menu module', function () {
|
|||
label: '3'
|
||||
}
|
||||
])
|
||||
var item = new MenuItem({
|
||||
label: 'inserted'
|
||||
})
|
||||
const item = new MenuItem({ label: 'inserted' })
|
||||
|
||||
menu.insert(1, item)
|
||||
assert.equal(menu.items[0].label, '1')
|
||||
assert.equal(menu.items[1].label, 'inserted')
|
||||
|
@ -242,23 +241,27 @@ describe('menu module', function () {
|
|||
|
||||
describe('Menu.popup', function () {
|
||||
let w = null
|
||||
let menu
|
||||
|
||||
afterEach(function () {
|
||||
beforeEach(() => {
|
||||
w = new BrowserWindow({show: false, width: 200, height: 200})
|
||||
menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '1'
|
||||
}, {
|
||||
label: '2'
|
||||
}, {
|
||||
label: '3'
|
||||
}
|
||||
])
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
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})
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '1'
|
||||
}, {
|
||||
label: '2'
|
||||
}, {
|
||||
label: '3'
|
||||
}
|
||||
])
|
||||
menu.popup(w, {x: 100, y: 100, async: true})
|
||||
menu.closePopup(w)
|
||||
})
|
||||
|
@ -266,7 +269,7 @@ describe('menu module', function () {
|
|||
})
|
||||
describe('MenuItem.click', function () {
|
||||
it('should be called with the item object passed', function (done) {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
click: function (item) {
|
||||
|
@ -282,7 +285,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem with checked property', function () {
|
||||
it('clicking an checkbox item should flip the checked property', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
type: 'checkbox'
|
||||
|
@ -294,7 +297,7 @@ describe('menu module', function () {
|
|||
})
|
||||
|
||||
it('clicking an radio item should always make checked property true', function () {
|
||||
var menu = Menu.buildFromTemplate([
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
type: 'radio'
|
||||
|
@ -307,99 +310,91 @@ describe('menu module', function () {
|
|||
})
|
||||
|
||||
it('at least have one item checked in each group', function () {
|
||||
var i, j, k, menu, template
|
||||
template = []
|
||||
for (i = j = 0; j <= 10; i = ++j) {
|
||||
const template = []
|
||||
for (let i = 0; i <= 10; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
type: 'radio'
|
||||
})
|
||||
}
|
||||
template.push({
|
||||
type: 'separator'
|
||||
})
|
||||
for (i = k = 12; k <= 20; i = ++k) {
|
||||
template.push({type: 'separator'})
|
||||
for (let i = 12; i <= 20; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
type: 'radio'
|
||||
})
|
||||
}
|
||||
menu = Menu.buildFromTemplate(template)
|
||||
const menu = Menu.buildFromTemplate(template)
|
||||
menu.delegate.menuWillShow()
|
||||
assert.equal(menu.items[0].checked, true)
|
||||
assert.equal(menu.items[12].checked, true)
|
||||
})
|
||||
|
||||
it('should assign groupId automatically', function () {
|
||||
var groupId, i, j, k, l, m, menu, template
|
||||
template = []
|
||||
for (i = j = 0; j <= 10; i = ++j) {
|
||||
const template = []
|
||||
for (let i = 0; i <= 10; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
type: 'radio'
|
||||
})
|
||||
}
|
||||
template.push({
|
||||
type: 'separator'
|
||||
})
|
||||
for (i = k = 12; k <= 20; i = ++k) {
|
||||
template.push({type: 'separator'})
|
||||
for (let i = 12; i <= 20; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
type: 'radio'
|
||||
})
|
||||
}
|
||||
menu = Menu.buildFromTemplate(template)
|
||||
groupId = menu.items[0].groupId
|
||||
for (i = l = 0; l <= 10; i = ++l) {
|
||||
const menu = Menu.buildFromTemplate(template)
|
||||
const groupId = menu.items[0].groupId
|
||||
for (let i = 0; i <= 10; i++) {
|
||||
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)
|
||||
}
|
||||
})
|
||||
|
||||
it("setting 'checked' should flip other items' 'checked' property", function () {
|
||||
var i, j, k, l, m, menu, n, o, p, q, template
|
||||
template = []
|
||||
for (i = j = 0; j <= 10; i = ++j) {
|
||||
const template = []
|
||||
for (let i = 0; i <= 10; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
type: 'radio'
|
||||
})
|
||||
}
|
||||
template.push({
|
||||
type: 'separator'
|
||||
})
|
||||
for (i = k = 12; k <= 20; i = ++k) {
|
||||
template.push({type: 'separator'})
|
||||
for (let i = 12; i <= 20; i++) {
|
||||
template.push({
|
||||
label: '' + i,
|
||||
label: `${i}`,
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
for (i = o = 12; o <= 20; i = ++o) {
|
||||
for (let i = 12; i <= 20; i++) {
|
||||
assert.equal(menu.items[i].checked, false)
|
||||
}
|
||||
menu.items[12].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[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)
|
||||
}
|
||||
})
|
||||
|
@ -407,20 +402,18 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem command id', function () {
|
||||
it('cannot be overwritten', function () {
|
||||
var item = new MenuItem({
|
||||
label: 'item'
|
||||
})
|
||||
const item = new MenuItem({label: 'item'})
|
||||
|
||||
var commandId = item.commandId
|
||||
assert(commandId != null)
|
||||
item.commandId = '' + commandId + '-modified'
|
||||
const commandId = item.commandId
|
||||
assert(commandId)
|
||||
item.commandId = `${commandId}-modified`
|
||||
assert.equal(item.commandId, commandId)
|
||||
})
|
||||
})
|
||||
|
||||
describe('MenuItem with invalid type', function () {
|
||||
it('throws an exception', function () {
|
||||
assert.throws(function () {
|
||||
assert.throws(() => {
|
||||
Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
|
@ -433,7 +426,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem with submenu type and missing submenu', function () {
|
||||
it('throws an exception', function () {
|
||||
assert.throws(function () {
|
||||
assert.throws(() => {
|
||||
Menu.buildFromTemplate([
|
||||
{
|
||||
label: 'text',
|
||||
|
@ -446,7 +439,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem role', 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.accelerator, undefined)
|
||||
assert.equal(item.getDefaultRoleAccelerator(), 'CommandOrControl+W')
|
||||
|
@ -480,7 +473,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem editMenu', 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.submenu.items[0].role, 'undo')
|
||||
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 () {
|
||||
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.submenu.items[0].role, 'close')
|
||||
})
|
||||
|
@ -511,7 +504,7 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem windowMenu', 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.submenu.items[0].role, 'minimize')
|
||||
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 () {
|
||||
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.submenu.items[0].role, 'copy')
|
||||
})
|
||||
|
@ -531,13 +524,13 @@ describe('menu module', function () {
|
|||
|
||||
describe('MenuItem with custom properties in constructor', function () {
|
||||
it('preserves the custom properties', function () {
|
||||
var template = [{
|
||||
const template = [{
|
||||
label: 'menu 1',
|
||||
customProp: 'foo',
|
||||
submenu: []
|
||||
}]
|
||||
|
||||
var menu = Menu.buildFromTemplate(template)
|
||||
const menu = Menu.buildFromTemplate(template)
|
||||
menu.items[0].submenu.append(new MenuItem({
|
||||
label: 'item 1',
|
||||
customProp: 'bar',
|
||||
|
|
Загрузка…
Ссылка в новой задаче