Skip to content

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 ItemView

Extends: ItemView

Constructor

new FileView(leaf: WorkspaceLeaf)

Creates a file view in a leaf.

Properties

PropertyTypeDescription
allowNoFilebooleanWhether the view may stay open with no file loaded.
contentElHTMLElementThe element views render their content into, created inside the view's container.
(Inherited from ItemView)
filenull | TFileThe file the view currently displays, or null when none is loaded.
navigationbooleanWhether the view takes part in navigation history. As in Obsidian, a file view opts back into the history the base view opts out of.

Methods

MethodReturnsDescription
addAction(_icon, _title, _callback)HTMLElementAdds 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)
asOriginalType3__()ItemViewOriginalMock-only: views this mock as Obsidian's ItemView type.
(Inherited from ItemView)
asOriginalType4__()FileViewOriginalMock-only: views this mock as Obsidian's FileView type.
canAcceptExtension(_extension)booleanTells whether the view can open files with the given extension.
constructor3__(_leaf)voidMock-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)voidMock-only construction hook, called at the end of the constructor; a no-op meant for vi.spyOn(FileView.prototype, 'constructor4__').
getDisplayText()stringGets the text shown in the view's tab header.
getState()Record<string, unknown>Gets the view's serializable state, which carries the loaded file's path as Obsidian's does.
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.
onload()voidCalled 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.
onRename(_file)Promise<void>Called when the displayed file is renamed. A no-op in the mock.
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.
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.
static fromOriginalType3__(value)ItemViewMock-only: views a value typed as Obsidian's ItemView as this mock.
(Inherited from ItemView)
static fromOriginalType4__(value)FileViewMock-only: views a value typed as Obsidian's FileView as this mock.

Links to this page: