Skip to content

Commit

Permalink
Return empty string for label if menu item is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperSprite committed Sep 14, 2018
1 parent a072040 commit e14b90d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Find menu item by labels and return that with following properties:
}
```

> If the target is not found, label is returned as an empty string, all other properties are undefined.
If the target is nested, it can be specified with variable length arguments.

ex) File -> Open:
Expand Down
18 changes: 12 additions & 6 deletions src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ function findItem(menuItems: Array<MenuItem>, labels: string[]) {

ipcMain.on('SPECTRON_MENU_ADDON/GET_MENU_ITEM', (e: Event, labels) => {
const menuItem: MenuItem = findItem(Menu.getApplicationMenu().items, labels)
e.returnValue = new MenuItem({
checked: menuItem.checked,
enabled: menuItem.enabled,
label: menuItem.label,
visible: menuItem.visible
})
if (menuItem) {
e.returnValue = new MenuItem({
checked: menuItem.checked,
enabled: menuItem.enabled,
label: menuItem.label,
visible: menuItem.visible
})
} else {
e.returnValue = ({
label: ''
})
}
})

ipcMain.on('SPECTRON_MENU_ADDON/CLICK_MENU_ITEM', (e: Event, labels) => {
Expand Down
9 changes: 9 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ describe('Verify menu item status', () => {
const menuItem = await menuAddon.getMenuItem('File', 'Open')
expect(menuItem.enabled).to.equal(false)
})

it('should return empty string if menu item does not exist', async () => {
expect(await app.client.getWindowCount()).to.equal(1)
const menuItem = await menuAddon.getMenuItem('File', 'Close')
expect(menuItem.label).to.equal('')
expect(menuItem.checked).to.equal(undefined)
expect(menuItem.enabled).to.equal(undefined)
expect(menuItem.visible).to.equal(undefined)
})
})

0 comments on commit e14b90d

Please sign in to comment.