FileView
Mock of Obsidian’s FileView.
FileView.setState resolves the file path in the state and loads it through FileView.loadFile, exactly as Obsidian does, so WorkspaceLeaf.openFile really leaves the view holding the file. The rename hook does nothing.
Import:
import { FileView } from 'obsidian-test-mocks/obsidian';Signature:
export class FileView extends ItemViewExtends: ItemView
Constructor
new FileView(leaf: WorkspaceLeaf)Creates a file view in a leaf.
Properties
| Property | Type | Description |
|---|---|---|
| _children | Component[] | The child components, unloaded when this component unloads. (Inherited from ItemView) |
| _events | EventRefOriginal[] | The event refs registered with Component.registerEvent, detached when this component unloads. (Inherited from ItemView) |
| _loaded | boolean | Whether this component is currently loaded. (Inherited from ItemView) |
| allowNoFile | boolean | Whether the view may stay open with no file loaded. |
| app | App | The app instance, taken from the leaf. (Inherited from ItemView) |
| cleanups__ | (() => unknown)[] | Mock-only: the callbacks registered with Component.register, run in reverse order on unload. (Inherited from ItemView) |
| containerEl | HTMLElement | The view's root element; detached in the mock. (Inherited from ItemView) |
| contentEl | HTMLElement | The element views render their content into, created inside the view's container. (Inherited from ItemView) |
| file | null | TFile | The file the view currently displays, or null when none is loaded. |
| icon | IconNameOriginal | The view's icon, Obsidian's generic document glyph until a subclass sets its own. (Inherited from ItemView) |
| intervals__ | number[] | Mock-only: the interval ids registered with Component.registerInterval, cleared on unload. (Inherited from ItemView) |
| leaf | WorkspaceLeaf | The workspace leaf hosting the view. (Inherited from ItemView) |
| navigation | boolean | Whether the view takes part in navigation history. As in Obsidian, a file view opts back into the history the base view opts out of. |
| scope | null | ScopeOriginal | An optional scope for hotkeys that apply while the view is focused. (Inherited from ItemView) |
Methods
| Method | Returns | Description |
|---|---|---|
| addAction(_icon, _title, _callback) | HTMLElement | Adds an icon button to the view's header. The mock does not attach the button or wire the callback: it returns a new detached element. (Inherited from ItemView) |
| addChild(component) | T | Adds a child component, loading it if this component is already loaded. (Inherited from ItemView) |
| asOriginalType__() | ComponentOriginal | Mock-only: views this mock as Obsidian's Component type.(Inherited from ItemView) |
| asOriginalType2__() | ViewOriginal | Mock-only: views this mock as Obsidian's View type.(Inherited from ItemView) |
| asOriginalType3__() | ItemViewOriginal | Mock-only: views this mock as Obsidian's ItemView type.(Inherited from ItemView) |
| asOriginalType4__() | FileViewOriginal | Mock-only: views this mock as Obsidian's FileView type. |
| canAcceptExtension(_extension) | boolean | Tells whether the view can open files with the given extension. |
| close() | Promise<void> | Closes the view, as WorkspaceLeaf.open does when another view replaces this one. Detaches the view's element, unloads the component and awaits View.onClose, exactly as Obsidian does.(Inherited from ItemView) |
| 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 ItemView) |
| constructor2__(_leaf) | void | Mock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(View.prototype, 'constructor2__').(Inherited from ItemView) |
| constructor3__(_leaf) | void | Mock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(ItemView.prototype, 'constructor3__').(Inherited from ItemView) |
| constructor4__(_leaf) | void | Mock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(FileView.prototype, 'constructor4__'). |
| getDisplayText() | string | Gets the text shown in the view's tab header. |
| getEphemeralState() | Record<string, unknown> | Gets the view's ephemeral state, such as scroll position, which is not persisted in the workspace layout. (Inherited from ItemView) |
| getIcon() | IconNameOriginal | Gets the view's icon. (Inherited from ItemView) |
| getState() | Record<string, unknown> | Gets the view's serializable state, which carries the loaded file's path as Obsidian's does. |
| getViewType() | string | Gets the identifier of the view's type, as registered with registerView.(Inherited from ItemView) |
| load() | void | Loads this component and then its children; does nothing if it is already loaded. (Inherited from ItemView) |
| loadFile(file) | Promise<boolean> | Loads a file into the view, as Obsidian does: the outgoing file is unloaded through FileView.onUnloadFile, the new one is stored and passed to FileView.onLoadFile, and a hook that throws leaves the view with no file rather than a half-loaded one. Loading the file the view already holds does nothing. The user-facing notice Obsidian shows for a file that failed to load is not modeled; the error is logged, as it is there. |
| onClose() | Promise<void> | Called when the view is closed, to release its resources. A no-op in the mock. (Inherited from ItemView) |
| onload() | void | Called when the view is loaded. A no-op in the mock. |
| onLoadFile(_file) | Promise<void> | Called when a file is loaded into the view, after FileView.loadFile has stored it in FileView.file. An empty hook for subclasses, as in Obsidian. |
| onOpen() | Promise<void> | Called when the view is opened, to build its content. A no-op in the mock. (Inherited from ItemView) |
| onPaneMenu(_menu, _source) | void | Populates the pane menu shown from the tab header or the "more options" button. A no-op in the mock. (Inherited from ItemView) |
| onRename(_file) | Promise<void> | Called when the displayed file is renamed. A no-op in the mock. |
| onResize() | void | Called when the view's size changes. A no-op in the mock. (Inherited from ItemView) |
| onunload() | void | Called when the component unloads; override it to tear the component down. A no-op by default. (Inherited from ItemView) |
| onUnloadFile(_file) | Promise<void> | Called when a file is unloaded from the view, before FileView.loadFile clears FileView.file. An empty hook for subclasses, as in Obsidian. |
| open(containerEl) | Promise<void> | Opens the view into an element, as WorkspaceLeaf.open does. Appends the view's element to it, loads the component and awaits View.onOpen, exactly as Obsidian does.(Inherited from ItemView) |
| register(callback) | void | Registers a callback to run when this component unloads. (Inherited from ItemView) |
| registerDomEvent(el, type, callback, options?) | void | Adds a DOM event listener, and registers a cleanup that removes it when this component unloads. (Inherited from ItemView) |
| registerEvent(ref) | void | Registers an event ref to be detached, through its owning Events object's offref, when this component unloads.(Inherited from ItemView) |
| registerInterval(id) | number | Registers an interval to be cleared with clearInterval when this component unloads.(Inherited from ItemView) |
| removeChild(component) | T | Removes a child component, if present, and unloads it. (Inherited from ItemView) |
| setEphemeralState(state) | void | Sets the view's ephemeral state. The mock stores it as given. (Inherited from ItemView) |
| setState(state, result) | Promise<void> | Restores the view from a serialized state, loading the file its file path names, as Obsidian does. A state naming a path that is not a file unloads the current one; a view left with no file asks the leaf to close it, unless FileView.allowNoFile is set.Obsidian also schedules the linked-pane sync on the result's done callback; syncState is not modeled, so the mock does not. |
| setState(state, _result) | Promise<void> | Sets the view's state. The mock stores it as given and leaves the result untouched. (Inherited from ItemView) |
| 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 ItemView) |
| static create__() | Component | Mock-only factory: creates a component, spyable via vi.spyOn(Component, 'create__').(Inherited from ItemView) |
| static fromOriginalType__(value) | Component | Mock-only: views a value typed as Obsidian's Component as this mock.(Inherited from ItemView) |
| static fromOriginalType2__(value) | View | Mock-only: views a value typed as Obsidian's View as this mock.(Inherited from ItemView) |
| static fromOriginalType3__(value) | ItemView | Mock-only: views a value typed as Obsidian's ItemView as this mock.(Inherited from ItemView) |
| static fromOriginalType4__(value) | FileView | Mock-only: views a value typed as Obsidian's FileView as this mock. |
Links to this page: