Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Refactor menu and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gina Contrino committed Nov 14, 2017
1 parent 9b2b0e2 commit bd483d1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 137 deletions.
215 changes: 105 additions & 110 deletions app/src/menu.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,5 @@
import i18n from './i18n';

const buildTemplate = electron =>
[
{
label: i18n.t('Edit'),
submenu: [
{
role: 'undo',
label: i18n.t('Undo'),
},
{
role: 'redo',
label: i18n.t('Redo'),
},
{
type: 'separator',
},
{
role: 'cut',
label: i18n.t('Cut'),
},
{
role: 'copy',
label: i18n.t('Copy'),
},
{
role: 'paste',
label: i18n.t('Paste'),
},
{
role: 'selectall',
label: i18n.t('Select all'),
},
],
},
{
label: i18n.t('View'),
submenu: [
{
role: 'reload',
label: i18n.t('Reload'),
},
{
role: 'togglefullscreen',
label: i18n.t('Toggle full screen'),
},
],
},
{
label: i18n.t('Window'),
submenu: [
{
role: 'minimize',
label: i18n.t('Minimize'),
},
],
},
{
label: i18n.t('Help'),
submenu: [
{
label: i18n.t('Lisk Website'),
click() {
electron.shell.openExternal('https://lisk.io');
},
},
{
label: i18n.t('Lisk Chat'),
click() {
electron.shell.openExternal('https://lisk.chat');
},
},
{
label: i18n.t('Lisk Explorer'),
click() {
electron.shell.openExternal('https://explorer.lisk.io');
},
},
{
label: i18n.t('Lisk Forum'),
click() {
electron.shell.openExternal('https://forum.lisk.io');
},
},
{
type: 'separator',
},
{
label: i18n.t('Report Issue...'),
click() {
electron.shell.openExternal('https://lisk.zendesk.com/hc/en-us/requests/new');
},
},
{
label: i18n.t('What\'s New...'),
click() {
electron.shell.openExternal('https://github.com/LiskHQ/lisk-nano/releases');
},
},
],
},
];

const addAboutMenuForMac = ({ template, name }) => {
template.unshift({
label: name,
Expand Down Expand Up @@ -135,12 +33,109 @@ const addAboutMenuForNonMac = ({ template, electron }) => {
});
};

module.exports = ({ electron }) => {
const template = buildTemplate(i18n);
if (process.platform === 'darwin') {
addAboutMenuForMac({ template, name: electron.app.getName() });
} else {
addAboutMenuForNonMac({ template, electron });
}
return electron.Menu.buildFromTemplate(template);
const menu = {
build: (electron) => {
const template = menu.buildTemplate(electron);
if (process.platform === 'darwin') {
addAboutMenuForMac({ template, name: electron.app.getName() });
} else {
addAboutMenuForNonMac({ template, electron });
}
return electron.Menu.buildFromTemplate(template);
},
onClickLink: (electron, url) => {
electron.shell.openExternal(url);
},
buildTemplate: electron =>
([
{
label: i18n.t('Edit'),
submenu: [
{
role: 'undo',
label: i18n.t('Undo'),
},
{
role: 'redo',
label: i18n.t('Redo'),
},
{
type: 'separator',
},
{
role: 'cut',
label: i18n.t('Cut'),
},
{
role: 'copy',
label: i18n.t('Copy'),
},
{
role: 'paste',
label: i18n.t('Paste'),
},
{
role: 'selectall',
label: i18n.t('Select all'),
},
],
},
{
label: i18n.t('View'),
submenu: [
{
role: 'reload',
label: i18n.t('Reload'),
},
{
role: 'togglefullscreen',
label: i18n.t('Toggle full screen'),
},
],
},
{
label: i18n.t('Window'),
submenu: [
{
role: 'minimize',
label: i18n.t('Minimize'),
},
],
},
{
label: i18n.t('Help'),
submenu: [
{
label: i18n.t('Lisk Website'),
click: menu.onClickLink.bind(null, electron, 'https://lisk.io'),
},
{
label: i18n.t('Lisk Chat'),
click: menu.onClickLink.bind(null, electron, 'https://lisk.chat'),
},
{
label: i18n.t('Lisk Explorer'),
click: menu.onClickLink.bind(null, electron, 'https://explorer.lisk.io'),
},
{
label: i18n.t('Lisk Forum'),
click: menu.onClickLink.bind(null, electron, 'https://forum.lisk.io'),
},
{
type: 'separator',
},
{
label: i18n.t('Report Issue...'),
click: menu.onClickLink.bind(null, electron, 'https://lisk.zendesk.com/hc/en-us/requests/new'),
},
{
label: i18n.t('What\'s New...'),
click: menu.onClickLink.bind(null, electron, 'https://github.com/LiskHQ/lisk-nano/releases'),
},
],
},
]),

};

export default menu;
13 changes: 9 additions & 4 deletions app/src/menu.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { expect } from 'chai'; // eslint-disable-line import/no-extraneous-dependencies
import { spy } from 'sinon'; // eslint-disable-line import/no-extraneous-dependencies
import buildMenu from './menu';
import menu from './menu';

describe('MenuBuilder', () => {
let electron;

beforeEach(() => {
electron = {
shell: { openExternal: () => {} },
shell: { openExternal: spy() },
Menu: {
setApplicationMenu: spy(),
buildFromTemplate: template => (template),
Expand All @@ -18,7 +18,7 @@ describe('MenuBuilder', () => {

it('Builds the electron about menu when os is mac', () => {
process.platform = 'darwin';
const template = buildMenu({ electron });
const template = menu.build(electron);
expect(template[0].label).to.equal('Lisk Nano');
expect(template[0].submenu[0].role).to.equal('about');
expect(template[0].submenu[0].label).to.equal('About');
Expand All @@ -32,11 +32,16 @@ describe('MenuBuilder', () => {

it('Builds the electron about menu when os is not mac', () => {
process.platform = 'not darwin';
const template = buildMenu({ electron });
const template = menu.build(electron);
const submenu = template[template.length - 1].submenu;
expect(submenu[submenu.length - 1].label).to.equal('About');

// make sure the mac about menu was not added
expect(template[0].label).to.not.equal('Lisk Nano');
});

it('Should open link on click', () => {
menu.onClickLink(electron, 'https://lisk.io');
expect(electron.shell.openExternal).to.have.been.calledWith('https://lisk.io');
});
});
19 changes: 7 additions & 12 deletions app/src/modules/localeHandler.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import i18n from './../i18n';
import buildMenu from './../menu';
import menu from './../menu';
import win from './win';

const handler = {
rebuildMenu: ({ electron, event }) => {
const { Menu } = electron;
Menu.setApplicationMenu(buildMenu({ electron }));
event.returnValue = 'Rebuilt electron menu.';
},

changeLocale: ({ langCode, storage }) => {
update: ({ electron, event, langCode, storage }) => {
// change locale
i18n.changeLanguage(langCode);
// write selected lang on JSON file
storage.set('config', { lang: langCode }, (error) => {
if (error) throw error;
});
},

update: ({ electron, event, langCode, storage }) => {
handler.changeLocale({ langCode, storage });
handler.rebuildMenu({ electron, event });
// rebuild menu
const { Menu } = electron;
Menu.setApplicationMenu(menu.build(electron));
event.returnValue = 'Rebuilt electron menu.';
},

send: ({ storage }) => {
Expand Down
14 changes: 5 additions & 9 deletions app/src/modules/localeHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ describe('localeHandler', () => {
eventStack.length = 0;
});

it('Changes the locale', () => {
localeHandler.changeLocale({ langCode: 'de', storage });
it('Changes the locale and rebuilds the menu', () => {
const event = {};
localeHandler.update({ electron, event, langCode: 'de', storage });
expect(i18n.language).to.equal('de');
expect(options[0].lang).to.equal('de');
expect(electron.Menu.setApplicationMenu).to.have.been.calledWith(electron.Menu);
expect(event.returnValue).to.equal('Rebuilt electron menu.');
});

it('Sends the detected language', () => {
Expand All @@ -52,11 +55,4 @@ describe('localeHandler', () => {

sendSpy.restore();
});

it('Rebuilds electron menu', () => {
const event = {};
localeHandler.rebuildMenu({ electron, event });
expect(electron.Menu.setApplicationMenu).to.have.been.calledWith(electron.Menu);
expect(event.returnValue).to.equal('Rebuilt electron menu.');
});
});
4 changes: 2 additions & 2 deletions app/src/modules/win.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import localeHandler from './localeHandler';
import eventStack from './eventStack';
import buildMenu from './../menu';
import menu from './../menu';

const win = {
browser: null,
Expand Down Expand Up @@ -42,7 +42,7 @@ const win = {
win.send({ event: 'openUrl', value: process.argv[1] || '/' });
}

Menu.setApplicationMenu(buildMenu({ electron }));
Menu.setApplicationMenu(menu.build(electron));

const selectionMenu = Menu.buildFromTemplate([
{ role: 'copy' },
Expand Down

0 comments on commit bd483d1

Please sign in to comment.