Skip to content

Menu

Mock of Obsidian’s Menu.

Nothing is shown: added items and separators are collected in Menu.items so a test can inspect them and invoke their click handlers, and the show and configuration methods only return the menu for chaining.

Import:

import { Menu } from 'obsidian-test-mocks/obsidian';

Signature:

export class Menu extends Component

Extends: Component

Constructor

new Menu()

Creates an empty menu. Use Menu.create2__ from tests.

Properties

PropertyTypeDescription
_childrenComponent[]The child components, unloaded when this component unloads.
(Inherited from Component)
_eventsEventRefOriginal[]The event refs registered with Component.registerEvent, detached when this component unloads.
(Inherited from Component)
_loadedbooleanWhether this component is currently loaded.
(Inherited from Component)
cleanups__(() => unknown)[]Mock-only: the callbacks registered with Component.register, run in reverse order on unload.
(Inherited from Component)
domHTMLElementThe menu's root element; detached in the mock.
intervals__number[]Mock-only: the interval ids registered with Component.registerInterval, cleared on unload.
(Inherited from Component)
items(MenuItem | MenuSeparator)[]The menu's items and separators, in the order they were added.
sectionSubmenus__Map<string, SectionSubmenu__>Mock-only: the submenu configuration recorded for each section by Menu.setSectionSubmenu, keyed by section name.

Methods

MethodReturnsDescription
addChild(component)TAdds a child component, loading it if this component is already loaded.
(Inherited from Component)
addItem(callback)thisAdds a menu item. Obsidian only allows this before the menu is shown; the mock allows it at any time.
addSeparator()thisAdds a separator. Obsidian only allows this before the menu is shown; the mock allows it at any time.
asOriginalType__()ComponentOriginalMock-only: views this mock as Obsidian's Component type.
(Inherited from Component)
asOriginalType2__()MenuOriginalMock-only: views this mock as Obsidian's Menu type.
close()voidCloses the menu, through Menu.hide.
constructor__()voidMock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(Component.prototype, 'constructor__').
(Inherited from Component)
constructor2__()voidMock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(Menu.prototype, 'constructor2__').
hide()thisHides the menu. As in Obsidian, the callback registered with Menu.onHide is cleared and then called, so it runs once however many times the menu is hidden.
load()voidLoads this component and then its children; does nothing if it is already loaded.
(Inherited from Component)
onHide(callback)voidRegisters a callback to run the next time the menu is hidden, by Menu.hide or Menu.close. Only the latest callback is kept.
onload()voidCalled when the component loads; override it to set the component up. A no-op by default.
(Inherited from Component)
onunload()voidCalled when the component unloads; override it to tear the component down. A no-op by default.
(Inherited from Component)
register(callback)voidRegisters a callback to run when this component unloads.
(Inherited from Component)
registerDomEvent(el, type, callback, options?)voidAdds a DOM event listener, and registers a cleanup that removes it when this component unloads.
(Inherited from Component)
registerEvent(ref)voidRegisters an event ref to be detached, through its owning Events object's offref, when this component unloads.
(Inherited from Component)
registerInterval(id)numberRegisters an interval to be cleared with clearInterval when this component unloads.
(Inherited from Component)
removeChild(component)TRemoves a child component, if present, and unloads it.
(Inherited from Component)
setNoIcon()thisMakes the menu reserve no space for item icons. A no-op in the mock.
setParentElement(_el)thisSets the element the menu is attached to. A no-op in the mock.
setSectionSubmenu(section, config)thisRecords a section's submenu configuration.

Obsidian keys these by section name and renders the section as a submenu; the mock only has to remember what was asked for, which is what callers such as obsidian-dev-utils' AbstractFileCommandHandler assert on. Read the recorded configs back through sectionSubmenus__.
setUseNativeMenu(_useNativeMenu)thisForces the menu to use the native OS menu or the DOM menu (desktop only). A no-op in the mock.
showAtMouseEvent(_event)thisShows the menu at a mouse event's position. A no-op in the mock.
showAtPosition(_position, _doc?)thisShows the menu at a position. A no-op in the mock.
unload()voidUnloads this component: unloads and forgets its children, runs its registered cleanups, clears the tracked events and intervals, then calls Component.onunload. Does nothing if it is not loaded.
(Inherited from Component)
static create__()ComponentMock-only factory: creates a component, spyable via vi.spyOn(Component, 'create__').
(Inherited from Component)
static create2__()MenuMock-only factory: creates a menu, spyable via vi.spyOn(Menu, 'create2__'). The subclass variant of Component.create__.
static forEvent(_event)MenuCreates a menu for a mouse or pointer event, choosing the native or DOM menu to suit how it was triggered.

The mock ignores the event and returns a new menu.
static fromOriginalType__(value)ComponentMock-only: views a value typed as Obsidian's Component as this mock.
(Inherited from Component)
static fromOriginalType2__(value)MenuMock-only: views a value typed as Obsidian's Menu as this mock.

Links to this page: