Skip to content

Commit

Permalink
fix(backend/menu): pass plugin menu item enabled fn to Boolean filter
Browse files Browse the repository at this point in the history
This will allow for convenient usage of `&&` within menuItem#enabled
method, e.g. `return menuState.bpmn && menuState.inactiveInput`.
Previously, `menuState.bpmn === undefined` would make menu item enabled.
  • Loading branch information
barmac authored and philippfromme committed Mar 18, 2019
1 parent 4c2a55d commit 04db4d5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/lib/menu/menu-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ class MenuBuilder {
return new MenuItem({
label,
accelerator,
enabled: isFunction(enabled) ? enabled() : enabled,
enabled: isFunction(enabled) ? Boolean(enabled()) : enabled,
click: action && wrapPluginAction(action, name),
submenu
});
Expand Down
47 changes: 47 additions & 0 deletions app/test/spec/menu/menu-builder-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('MenuBuilder', () => {
describe('plugins menu', function() {

it('should accept callable values for enabled', () => {

// given
const pluginName = 'test';
const menuStub = sinon.stub().returns([
Expand Down Expand Up @@ -89,7 +90,50 @@ describe('MenuBuilder', () => {
});


it('should disable menu item if enable function returns falsy value', () => {

// given
const falsyValues = [
false,
0,
'',
null,
undefined,
NaN
];
const pluginName = 'test';
const menuStub = sinon.stub().returns(falsyValues.map(value => ({
label: 'label',
enabled: () => value
})));

const options = getOptionsWithPlugins([
{
name: pluginName,
menu: menuStub
}
]);

const menuBuilder = new MenuBuilder(options);

// when
const { menu } = menuBuilder.build();

// then
const plugins = menu.find(item => item.label === 'Plugins');
const pluginMenu = plugins.submenu.find(plugin => plugin.label === pluginName);

expect(pluginMenu).to.exist;
expect(pluginMenu.submenu).to.be.an('Array').and.have.lengthOf(falsyValues.length);

for (const entry of pluginMenu.submenu) {
expect(entry).to.have.property('enabled', false);
}
});


it('should properly label plugin with error', function() {

// given
const pluginName = 'test';
const expectedLabel = `${pluginName} <error>`;
Expand Down Expand Up @@ -117,6 +161,7 @@ describe('MenuBuilder', () => {


it('should accept non-callable values for enabled', () => {

// given
const pluginName = 'test';
const menuStub = sinon.stub().returns([
Expand Down Expand Up @@ -149,6 +194,7 @@ describe('MenuBuilder', () => {


it('should properly handle plugin menu error', function() {

// given
const pluginName = 'test';
const menuStub = sinon.stub().throwsException();
Expand All @@ -168,6 +214,7 @@ describe('MenuBuilder', () => {


it('should properly handle plugin menu item action error', function() {

// given
const pluginName = 'test';
const actionStub = sinon.stub().throwsException();
Expand Down

0 comments on commit 04db4d5

Please sign in to comment.