Skip to content

Commit

Permalink
test(menu): add basic tests with proxyquire
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac committed Jan 17, 2019
1 parent ece5853 commit 78357e3
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 68 deletions.
10 changes: 5 additions & 5 deletions app/lib/menu/mac-os/mac-menu-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MacMenuBuilder extends MenuBuilder {
appendAppMenu() {
const subMenu = new MacMenuBuilder({
template: [{
label: 'About ' + this.opts.appName,
label: 'About ' + this.options.appName,
role: 'about'
}, {
type: 'separator'
Expand All @@ -27,7 +27,7 @@ class MacMenuBuilder extends MenuBuilder {
}, {
type: 'separator'
}, {
label: 'Hide ' + this.opts.appName,
label: 'Hide ' + this.options.appName,
accelerator: 'Command+H',
role: 'hide'
}, {
Expand All @@ -43,7 +43,7 @@ class MacMenuBuilder extends MenuBuilder {
}).appendQuit().get();

this.menu.append(new MenuItem({
label: this.opts.appName,
label: this.options.appName,
submenu: subMenu
}));

Expand All @@ -53,7 +53,7 @@ class MacMenuBuilder extends MenuBuilder {
appendRedo() {
this.menu.append(new MenuItem({
label: 'Redo',
enabled: this.opts.state.redo,
enabled: this.options.state.redo,
accelerator: 'Command+Shift+Z',
click: function(menuItem, browserWindow) {
app.emit('menu:action', 'redo');
Expand All @@ -63,7 +63,7 @@ class MacMenuBuilder extends MenuBuilder {

build() {
this.appendAppMenu()
.appendFileMenu(new this.constructor(this.opts)
.appendFileMenu(new this.constructor(this.options)
.appendNewFile()
.appendOpen()
.appendSeparator()
Expand Down
64 changes: 28 additions & 36 deletions app/lib/menu/menu-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,27 @@ const {


class MenuBuilder {
constructor(opts) {
this.opts = merge({
constructor(options) {
this.options = merge({
appName: app.name,
state: {
dmn: false,
activeEditor: null,
cmmn: false,
bpmn: false,
undo: false,
redo: false,
editable: false,
copy: false,
paste: false,
searchable: false,
zoom: false,
save: false,
close: false,
elementsSelected: false,
dmnRuleEditing: false,
dmnClauseEditing: false,
exportAs: false,
development: app.developmentMode,
devtools: false
},
plugins: []
}, opts);
}, options);

if (this.opts.template) {
this.menu = Menu.buildFromTemplate(this.opts.template);
if (this.options.template) {
this.menu = Menu.buildFromTemplate(this.options.template);
} else {
this.menu = new Menu();
}
Expand Down Expand Up @@ -121,7 +113,7 @@ class MenuBuilder {
appendSaveFile() {
this.menu.append(new MenuItem({
label: 'Save File',
enabled: this.opts.state.save,
enabled: this.options.state.save,
accelerator: 'CommandOrControl+S',
click: function() {
app.emit('menu:action', 'save');
Expand All @@ -135,7 +127,7 @@ class MenuBuilder {
this.menu.append(new MenuItem({
label: 'Save File As..',
accelerator: 'CommandOrControl+Shift+S',
enabled: this.opts.state.save,
enabled: this.options.state.save,
click: function() {
app.emit('menu:action', 'save-as');
}
Expand All @@ -148,7 +140,7 @@ class MenuBuilder {
this.menu.append(new MenuItem({
label: 'Save All Files',
accelerator: 'CommandOrControl+Alt+S',
enabled: this.opts.state.save,
enabled: this.options.state.save,
click: function() {
app.emit('menu:action', 'save-all');
}
Expand All @@ -158,7 +150,7 @@ class MenuBuilder {
}

appendExportAs() {
const exportState = this.opts.state.exportAs;
const exportState = this.options.state.exportAs;
const enabled = exportState && exportState.length > 0;

this.menu.append(new MenuItem({
Expand All @@ -177,7 +169,7 @@ class MenuBuilder {
appendCloseTab() {
this.menu.append(new MenuItem({
label: 'Close Tab',
enabled: this.opts.state.close,
enabled: this.options.state.close,
accelerator: 'CommandOrControl+W',
click: function() {
app.emit('menu:action', 'close-active-tab');
Expand All @@ -186,15 +178,15 @@ class MenuBuilder {

this.menu.append(new MenuItem({
label: 'Close All Tabs',
enabled: this.opts.state.close,
enabled: this.options.state.close,
click: function() {
app.emit('menu:action', 'close-all-tabs');
}
}));

this.menu.append(new MenuItem({
label: 'Close Other Tabs',
enabled: this.opts.state.close,
enabled: this.options.state.close,
click: function() {
app.emit('menu:action', 'close-other-tabs');
}
Expand All @@ -209,15 +201,15 @@ class MenuBuilder {
label: 'Switch Tab..',
submenu: submenu || Menu.buildFromTemplate([{
label: 'Select Next Tab',
enabled: this.opts.state.close,
enabled: this.options.state.close,
accelerator: 'Control+TAB',
click: function() {
app.emit('menu:action', 'select-tab', 'next');
}
},
{
label: 'Select Previous Tab',
enabled: this.opts.state.close,
enabled: this.options.state.close,
accelerator: 'Control+SHIFT+TAB',
click: function() {
app.emit('menu:action', 'select-tab', 'previous');
Expand Down Expand Up @@ -267,7 +259,7 @@ class MenuBuilder {
}

getEditMenu(menuItems) {
const builder = new this.constructor(this.opts);
const builder = new this.constructor(this.options);

menuItems.forEach((menuItem, index) => {
if (Array.isArray(menuItem)) {
Expand All @@ -289,8 +281,8 @@ class MenuBuilder {
appendEditMenu() {
let subMenu;

if (this.opts.state.editMenu) {
subMenu = this.getEditMenu(this.opts.state.editMenu, this.opts);
if (this.options.state.editMenu) {
subMenu = this.getEditMenu(this.options.state.editMenu, this.options);
}

this.menu.append(new MenuItem({
Expand All @@ -304,7 +296,7 @@ class MenuBuilder {
appendWindowMenu() {
const submenu = [];

if (this.opts.state.zoom) {
if (this.options.state.zoom) {
submenu.push({
label: 'Zoom In',
accelerator: 'CommandOrControl+=',
Expand Down Expand Up @@ -350,7 +342,7 @@ class MenuBuilder {
type: 'separator'
});

if (this.opts.state.development || this.opts.state.devtools) {
if (this.options.state.development || this.options.state.devtools) {
submenu.push({
label: 'Reload',
accelerator: 'CommandOrControl+R',
Expand All @@ -367,15 +359,15 @@ class MenuBuilder {
const isDevToolsOpened = browserWindow.isDevToolsOpened();
if (isDevToolsOpened) {
app.mainWindow.once('devtools-closed', () => {
app.emit('menu:update', assign({}, this.opts.state, {
app.emit('menu:update', assign({}, this.options.state, {
devtools: false
}));
});
browserWindow.closeDevTools();
}
else {
app.mainWindow.once('devtools-opened', () => {
app.emit('menu:update', assign({}, this.opts.state, {
app.emit('menu:update', assign({}, this.options.state, {
devtools: true
}));
});
Expand Down Expand Up @@ -404,11 +396,11 @@ class MenuBuilder {
}

appendPluginsMenu() {
if (this.opts.plugins.length === 0) {
if (this.options.plugins.length === 0) {
return this;
}

const submenu = this.opts.plugins
const submenu = this.options.plugins
.map(p => {
let label = p.name;

Expand All @@ -423,7 +415,7 @@ class MenuBuilder {

if (p.menu) {
try {
const menuEntries = p.menu(app, this.opts.state);
const menuEntries = p.menu(app, this.options.state);
menuItemDescriptor.enabled = true;
menuItemDescriptor.submenu = Menu.buildFromTemplate(menuEntries.map(menuDescriptor => {
return new MenuItem({
Expand Down Expand Up @@ -540,7 +532,7 @@ class MenuBuilder {
}

build() {
return this.appendFileMenu(new this.constructor(this.opts)
return this.appendFileMenu(new this.constructor(this.options)
.appendNewFile()
.appendOpen()
.appendSeparator()
Expand Down Expand Up @@ -569,7 +561,7 @@ class MenuBuilder {
appendContextCloseTab(attrs) {
this.menu.append(new MenuItem({
label: 'Close Tab',
enabled: this.opts.state.close,
enabled: this.options.state.close,
accelerator: 'CommandOrControl+W',
click: function() {
app.emit('menu:action', 'close-tab', attrs);
Expand All @@ -578,15 +570,15 @@ class MenuBuilder {

this.menu.append(new MenuItem({
label: 'Close All Tabs',
enabled: this.opts.state.close,
enabled: this.options.state.close,
click: function() {
app.emit('menu:action', 'close-all-tabs');
}
}));

this.menu.append(new MenuItem({
label: 'Close Other Tabs',
enabled: this.opts.state.close,
enabled: this.options.state.close,
click: function() {
app.emit('menu:action', 'close-other-tabs', attrs);
}
Expand Down
3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"devDependencies": {
"electron-devtools-installer": "^2.2.4",
"electron-reload": "^1.2.2"
"electron-reload": "^1.2.2",
"proxyquire": "^2.1.0"
},
"dependencies": {
"form-data": "^2.3.1",
Expand Down
10 changes: 10 additions & 0 deletions app/test/helper/mock/electron-app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

/**
* Simple mock of electron's <app> module
*/
const ElectronApp = {
name: 'app'
};

module.exports = ElectronApp;
11 changes: 11 additions & 0 deletions app/test/helper/mock/electron-menu-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';


/**
* Simple mock of electron's <MenuItem> module
*/
class ElectronMenuItem {
constructor() {}
}

module.exports = ElectronMenuItem;
17 changes: 17 additions & 0 deletions app/test/helper/mock/electron-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';


/**
* Simple mock of electron's <Menu> module
*/
class ElectronMenu {
static buildFromTemplate() {}

static setApplicationMenu() {}

constructor() {}

append() {}
}

module.exports = ElectronMenu;
37 changes: 37 additions & 0 deletions app/test/spec/menu/menu-builder-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const proxyquire = require('proxyquire');

const ElectronApp = require('../../helper/mock/electron-app');
const ElectronMenu = require('../../helper/mock/electron-menu');
const ElectronMenuItem = require('../../helper/mock/electron-menu-item');

const Electron = {
app: ElectronApp,
Menu: ElectronMenu,
MenuItem: ElectronMenuItem
};

const MenuBuilder = proxyquire('../../../lib/menu/menu-builder.js', {
electron: Electron
});


describe('MenuBuilder', () => {

it('should instantiate', () => {
const menuBuilder = new MenuBuilder();

expect(menuBuilder).to.exist;
});


it('should build menu', () => {
const menuBuilder = new MenuBuilder();

const menu = menuBuilder.build();

expect(menu).to.exist;
});

});
Loading

0 comments on commit 78357e3

Please sign in to comment.