test: add coverage for menuItem.badge on macOS

Adds TypeScript smoke tests and functional tests for the MenuItem badge
property, covering all badge types, dynamic updates, and menu integration.
This commit is contained in:
Shelley Vohr
2026-04-10 13:11:11 +02:00
committed by Charles Kerr
parent a51240cadc
commit de31086af9
2 changed files with 77 additions and 0 deletions

View File

@@ -616,4 +616,59 @@ describe('MenuItems', () => {
expect(menu._getAcceleratorTextAt(5)).to.equal('Ctrl+?');
});
});
ifdescribe(process.platform === 'darwin')('MenuItem.badge', () => {
it('should set badge from constructor options', () => {
const item = new MenuItem({
label: 'test',
badge: { type: 'alerts', count: 3 }
});
expect(item.badge).to.deep.equal({ type: 'alerts', count: 3 });
});
it('should support all badge types', () => {
const alerts = new MenuItem({ label: 'a', badge: { type: 'alerts', count: 1 } });
const updates = new MenuItem({ label: 'b', badge: { type: 'updates', count: 2 } });
const newItems = new MenuItem({ label: 'c', badge: { type: 'new-items', count: 5 } });
const custom = new MenuItem({ label: 'd', badge: { type: 'none', content: 'Custom' } });
expect(alerts.badge).to.deep.equal({ type: 'alerts', count: 1 });
expect(updates.badge).to.deep.equal({ type: 'updates', count: 2 });
expect(newItems.badge).to.deep.equal({ type: 'new-items', count: 5 });
expect(custom.badge).to.deep.equal({ type: 'none', content: 'Custom' });
});
it('should allow dynamic badge updates', () => {
const item = new MenuItem({
label: 'test',
badge: { type: 'alerts', count: 1 }
});
item.badge = { type: 'updates', count: 10 };
expect(item.badge).to.deep.equal({ type: 'updates', count: 10 });
});
it('should have undefined badge when not set', () => {
const item = new MenuItem({ label: 'test' });
expect(item.badge).to.be.undefined();
});
it('should set badge on items added to a menu', () => {
const menu = Menu.buildFromTemplate([
{ label: 'test', badge: { type: 'alerts', count: 3 } }
]);
expect(menu.items[0].badge).to.deep.equal({ type: 'alerts', count: 3 });
});
it('should update badge after item is added to a menu', () => {
const menu = Menu.buildFromTemplate([
{ label: 'test', badge: { type: 'alerts', count: 1 } }
]);
menu.items[0].badge = { type: 'updates', count: 5 };
expect(menu.items[0].badge).to.deep.equal({ type: 'updates', count: 5 });
});
});
});

View File

@@ -633,6 +633,28 @@ menuItem.click = (passedMenuItem: Electron.MenuItem, browserWindow: Electron.Bro
console.log('click', passedMenuItem, browserWindow);
};
const badgedItem = new MenuItem({
label: 'Alerts',
badge: { type: 'alerts', count: 3 }
});
console.log(badgedItem.badge);
const customBadgeItem = new MenuItem({
label: 'Custom',
badge: { type: 'none', content: 'New' }
});
customBadgeItem.badge = { type: 'updates', count: 5 };
const updatesItem = new MenuItem({
label: 'Updates',
badge: { type: 'updates', count: 10 }
});
const newItemsBadge = new MenuItem({
label: 'New Items',
badge: { type: 'new-items', count: 1 }
});
// menu
// https://github.com/electron/electron/blob/main/docs/api/menu.md