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 ComponentExtends: Component
Properties
| Property | Type | Description |
|---|---|---|
| _children | Component[] | The child components, unloaded when this component unloads. (Inherited from Component) |
| _events | EventRefOriginal[] | The event refs registered with Component.registerEvent, detached when this component unloads. (Inherited from Component) |
| _loaded | boolean | Whether 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) |
| dom | HTMLElement | The 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
| Method | Returns | Description |
|---|---|---|
| addChild(component) | T | Adds a child component, loading it if this component is already loaded. (Inherited from Component) |
| addItem(callback) | this | Adds a menu item. Obsidian only allows this before the menu is shown; the mock allows it at any time. |
| addSeparator() | this | Adds a separator. Obsidian only allows this before the menu is shown; the mock allows it at any time. |
| asOriginalType__() | ComponentOriginal | Mock-only: views this mock as Obsidian's Component type.(Inherited from Component) |
| asOriginalType2__() | MenuOriginal | Mock-only: views this mock as Obsidian's Menu type. |
| close() | void | Closes the menu, through Menu.hide. |
| constructor__() | void | Mock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(Component.prototype, 'constructor__').(Inherited from Component) |
| constructor2__() | void | Mock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(Menu.prototype, 'constructor2__'). |
| hide() | this | Hides 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() | void | Loads this component and then its children; does nothing if it is already loaded. (Inherited from Component) |
| onHide(callback) | void | Registers a callback to run the next time the menu is hidden, by Menu.hide or Menu.close. Only the latest callback is kept. |
| onload() | void | Called when the component loads; override it to set the component up. A no-op by default. (Inherited from Component) |
| onunload() | void | Called when the component unloads; override it to tear the component down. A no-op by default. (Inherited from Component) |
| register(callback) | void | Registers a callback to run when this component unloads. (Inherited from Component) |
| registerDomEvent(el, type, callback, options?) | void | Adds a DOM event listener, and registers a cleanup that removes it when this component unloads. (Inherited from Component) |
| registerEvent(ref) | void | Registers an event ref to be detached, through its owning Events object's offref, when this component unloads.(Inherited from Component) |
| registerInterval(id) | number | Registers an interval to be cleared with clearInterval when this component unloads.(Inherited from Component) |
| removeChild(component) | T | Removes a child component, if present, and unloads it. (Inherited from Component) |
| setNoIcon() | this | Makes the menu reserve no space for item icons. A no-op in the mock. |
| setParentElement(_el) | this | Sets the element the menu is attached to. A no-op in the mock. |
| setSectionSubmenu(section, config) | this | Records 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) | this | Forces the menu to use the native OS menu or the DOM menu (desktop only). A no-op in the mock. |
| showAtMouseEvent(_event) | this | Shows the menu at a mouse event's position. A no-op in the mock. |
| showAtPosition(_position, _doc?) | this | Shows the menu at a position. A no-op in the mock. |
| unload() | void | Unloads 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__() | Component | Mock-only factory: creates a component, spyable via vi.spyOn(Component, 'create__').(Inherited from Component) |
| static create2__() | Menu | Mock-only factory: creates a menu, spyable via vi.spyOn(Menu, 'create2__'). The subclass variant of Component.create__. |
| static forEvent(_event) | Menu | Creates 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) | Component | Mock-only: views a value typed as Obsidian's Component as this mock.(Inherited from Component) |
| static fromOriginalType2__(value) | Menu | Mock-only: views a value typed as Obsidian's Menu as this mock. |
Links to this page: