obsidian#InstalledPlugin TypeScript Examples
The following examples show how to use
obsidian#InstalledPlugin.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: commandHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeCommandPalettePluginInstall(): MockProxy<InstalledPlugin> {
const mockInstance = mock<CommandPalettePluginInstance>({
id: COMMAND_PALETTE_PLUGIN_ID,
options: {
pinned: null,
},
});
return mock<InstalledPlugin>({
enabled: true,
instance: mockInstance,
});
}
Example #2
Source File: commandHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeInternalPluginList(
commandPalettePlugin: MockProxy<InstalledPlugin>,
): MockProxy<InternalPlugins> {
const mockPlugins = mock<Record<string, InstalledPlugin>>({
[COMMAND_PALETTE_PLUGIN_ID]: commandPalettePlugin,
});
const mockInternalPlugins = mock<InternalPlugins>({ plugins: mockPlugins });
mockInternalPlugins.getPluginById.mockImplementation((id) => mockPlugins[id]);
return mockInternalPlugins;
}
Example #3
Source File: starredHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeStarredPluginInstall(): MockProxy<InstalledPlugin> {
const mockInstance = mock<StarredPluginInstance>({
id: STARRED_PLUGIN_ID,
items: [
makeFileStarredItem(),
makeFileStarredItem(expectedStarredFileTitle),
makeSearchStarredItem(),
],
});
return mock<InstalledPlugin>({
enabled: true,
instance: mockInstance,
});
}
Example #4
Source File: starredHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeInternalPluginList(
starredPlugin: MockProxy<InstalledPlugin>,
): MockProxy<InternalPlugins> {
const mockPlugins = mock<Record<string, InstalledPlugin>>({
starred: starredPlugin,
});
const mockInternalPlugins = mock<InternalPlugins>({ plugins: mockPlugins });
mockInternalPlugins.getPluginById
.calledWith(STARRED_PLUGIN_ID)
.mockReturnValue(mockPlugins[STARRED_PLUGIN_ID]);
return mockInternalPlugins;
}
Example #5
Source File: workspaceHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeWorkspacesPluginInstall(): MockProxy<InstalledPlugin> {
const mockInstance = mock<WorkspacesPluginInstance>({
id: WORKSPACE_PLUGIN_ID,
workspaces: {
'first workspace': {},
'second workspace': {},
},
});
return mock<InstalledPlugin>({
enabled: true,
instance: mockInstance,
});
}
Example #6
Source File: workspaceHandler.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
function makeInternalPluginList(
workspacePlugin: MockProxy<InstalledPlugin>,
): MockProxy<InternalPlugins> {
const mockPlugins = mock<Record<string, InstalledPlugin>>({
workspaces: workspacePlugin,
});
const mockInternalPlugins = mock<InternalPlugins>({ plugins: mockPlugins });
mockInternalPlugins.getPluginById.mockImplementation((id) => mockPlugins[id]);
return mockInternalPlugins;
}
Example #7
Source File: utils.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 6 votes |
export function getInternalPluginById(app: App, id: string): InstalledPlugin {
return app?.internalPlugins?.getPluginById(id);
}
Example #8
Source File: commandHandler.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 5 votes |
private getCommandPalettePlugin(): InstalledPlugin {
return getInternalPluginById(this.app, COMMAND_PALETTE_PLUGIN_ID);
}
Example #9
Source File: starredHandler.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 5 votes |
private getSystemStarredPlugin(): InstalledPlugin {
return getInternalPluginById(this.app, STARRED_PLUGIN_ID);
}
Example #10
Source File: workspaceHandler.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 5 votes |
private getSystemWorkspacesPlugin(): InstalledPlugin {
return getInternalPluginById(this.app, WORKSPACE_PLUGIN_ID);
}
Example #11
Source File: switcherPlusSettings.test.ts From obsidian-switcher-plus with GNU General Public License v3.0 | 4 votes |
describe('SwitcherPlusSettings', () => {
let mockApp: MockProxy<App>;
let mockPlugin: MockProxy<SwitcherPlusPlugin>;
let sut: SwitcherPlusSettings;
beforeAll(() => {
mockApp = mock<App>({ internalPlugins: mock<InternalPlugins>() });
mockPlugin = mock<SwitcherPlusPlugin>({ app: mockApp });
});
beforeEach(() => {
sut = new SwitcherPlusSettings(mockPlugin);
});
it('should return default settings', () => {
// extract enabledSymbolTypes to handle separately, because it's not exposed
// on SwitcherPlusSettings directly
const { enabledSymbolTypes, ...defaults } = transientSettingsData(true);
expect(sut).toEqual(expect.objectContaining(defaults));
expect(sut.editorListPlaceholderText).toBe(defaults.editorListCommand);
expect(sut.symbolListPlaceholderText).toBe(defaults.symbolListCommand);
expect(sut.workspaceListPlaceholderText).toBe(defaults.workspaceListCommand);
expect(sut.headingsListPlaceholderText).toBe(defaults.headingsListCommand);
expect(sut.starredListPlaceholderText).toBe(defaults.starredListCommand);
expect(sut.commandListPlaceholderText).toBe(defaults.commandListCommand);
expect(sut.relatedItemsListPlaceholderText).toBe(defaults.relatedItemsListCommand);
expect(sut.includeSidePanelViewTypesPlaceholder).toBe(
defaults.includeSidePanelViewTypes.join('\n'),
);
expect(sut.isSymbolTypeEnabled(SymbolType.Embed)).toBe(
enabledSymbolTypes[SymbolType.Embed],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Heading)).toBe(
enabledSymbolTypes[SymbolType.Heading],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Link)).toBe(
enabledSymbolTypes[SymbolType.Link],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Tag)).toBe(
enabledSymbolTypes[SymbolType.Tag],
);
});
it('should save modified settings', async () => {
const settings = transientSettingsData(false);
sut.onOpenPreferNewPane = settings.onOpenPreferNewPane;
sut.alwaysNewPaneForSymbols = settings.alwaysNewPaneForSymbols;
sut.useActivePaneForSymbolsOnMobile = settings.useActivePaneForSymbolsOnMobile;
sut.symbolsInLineOrder = settings.symbolsInLineOrder;
sut.editorListCommand = settings.editorListCommand;
sut.symbolListCommand = settings.symbolListCommand;
sut.workspaceListCommand = settings.workspaceListCommand;
sut.headingsListCommand = settings.headingsListCommand;
sut.starredListCommand = settings.starredListCommand;
sut.commandListCommand = settings.commandListCommand;
sut.relatedItemsListCommand = settings.relatedItemsListCommand;
sut.strictHeadingsOnly = settings.strictHeadingsOnly;
sut.searchAllHeadings = settings.searchAllHeadings;
sut.includeSidePanelViewTypes = settings.includeSidePanelViewTypes;
sut.limit = settings.limit;
sut.selectNearestHeading = settings.selectNearestHeading;
sut.excludeFolders = settings.excludeFolders;
sut.excludeLinkSubTypes = settings.excludeLinkSubTypes;
sut.excludeRelatedFolders = settings.excludeRelatedFolders;
sut.excludeOpenRelatedFiles = settings.excludeOpenRelatedFiles;
sut.setSymbolTypeEnabled(
SymbolType.Heading,
settings.enabledSymbolTypes[SymbolType.Heading],
);
sut.setSymbolTypeEnabled(
SymbolType.Link,
settings.enabledSymbolTypes[SymbolType.Link],
);
sut.setSymbolTypeEnabled(SymbolType.Tag, settings.enabledSymbolTypes[SymbolType.Tag]);
sut.setSymbolTypeEnabled(
SymbolType.Embed,
settings.enabledSymbolTypes[SymbolType.Embed],
);
let savedSettings: SettingsData;
mockPlugin.saveData.mockImplementationOnce((data: SettingsData) => {
savedSettings = data;
return Promise.resolve();
});
await sut.saveSettings();
expect(savedSettings).toEqual(expect.objectContaining(settings));
expect(mockPlugin.saveData).toHaveBeenCalled();
mockPlugin.saveData.mockReset();
});
it('should load saved settings', async () => {
const settings = transientSettingsData(false);
const { enabledSymbolTypes, ...prunedSettings } = settings;
mockPlugin.loadData.mockResolvedValueOnce(settings);
await sut.loadSettings();
expect(sut).toEqual(expect.objectContaining(prunedSettings));
expect(sut.isSymbolTypeEnabled(SymbolType.Heading)).toBe(
enabledSymbolTypes[SymbolType.Heading],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Link)).toBe(
enabledSymbolTypes[SymbolType.Link],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Tag)).toBe(
enabledSymbolTypes[SymbolType.Tag],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Embed)).toBe(
enabledSymbolTypes[SymbolType.Embed],
);
expect(mockPlugin.loadData).toHaveBeenCalled();
mockPlugin.loadData.mockReset();
});
it('should load saved settings, even with missing data keys', async () => {
const defaults = transientSettingsData(true);
const settings = transientSettingsData(false);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { enabledSymbolTypes, ...prunedSettings } = settings;
mockPlugin.loadData.mockResolvedValueOnce(prunedSettings);
await sut.loadSettings();
expect(sut).toEqual(expect.objectContaining(prunedSettings));
expect(sut.isSymbolTypeEnabled(SymbolType.Heading)).toBe(
defaults.enabledSymbolTypes[SymbolType.Heading],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Link)).toBe(
defaults.enabledSymbolTypes[SymbolType.Link],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Tag)).toBe(
defaults.enabledSymbolTypes[SymbolType.Tag],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Embed)).toBe(
defaults.enabledSymbolTypes[SymbolType.Embed],
);
expect(mockPlugin.loadData).toHaveBeenCalled();
mockPlugin.loadData.mockReset();
});
it('should use default data if settings cannot be loaded', async () => {
const { enabledSymbolTypes, ...defaults } = transientSettingsData(true);
mockPlugin.loadData.mockResolvedValueOnce(null);
await sut.loadSettings();
expect(sut).toEqual(expect.objectContaining(defaults));
expect(sut.editorListPlaceholderText).toBe(defaults.editorListCommand);
expect(sut.symbolListPlaceholderText).toBe(defaults.symbolListCommand);
expect(sut.workspaceListPlaceholderText).toBe(defaults.workspaceListCommand);
expect(sut.headingsListPlaceholderText).toBe(defaults.headingsListCommand);
expect(sut.includeSidePanelViewTypesPlaceholder).toBe(
defaults.includeSidePanelViewTypes.join('\n'),
);
expect(sut.isSymbolTypeEnabled(SymbolType.Embed)).toBe(
enabledSymbolTypes[SymbolType.Embed],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Heading)).toBe(
enabledSymbolTypes[SymbolType.Heading],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Link)).toBe(
enabledSymbolTypes[SymbolType.Link],
);
expect(sut.isSymbolTypeEnabled(SymbolType.Tag)).toBe(
enabledSymbolTypes[SymbolType.Tag],
);
expect(mockPlugin.loadData).toHaveBeenCalled();
});
it('should load built-in system switcher settings', () => {
const builtInOptions = mock<QuickSwitcherOptions>({
showAllFileTypes: chance.bool(),
showAttachments: chance.bool(),
showExistingOnly: chance.bool(),
});
const pluginInstance = mock<QuickSwitcherPluginInstance>({
id: 'switcher',
options: builtInOptions,
QuickSwitcherModal: null,
});
const builtInSwitcherPlugin = mock<InstalledPlugin>({
enabled: true,
instance: pluginInstance,
});
const mockInternalPlugins = mockApp.internalPlugins as MockProxy<InternalPlugins>;
mockInternalPlugins.getPluginById.mockReturnValue(builtInSwitcherPlugin);
expect(sut.builtInSystemOptions).toMatchObject(builtInOptions);
expect(sut.showAllFileTypes).toBe(builtInOptions.showAllFileTypes);
expect(sut.showAttachments).toBe(builtInOptions.showAttachments);
expect(sut.showExistingOnly).toBe(builtInOptions.showExistingOnly);
expect(mockInternalPlugins.getPluginById).toHaveBeenCalled();
mockInternalPlugins.getPluginById.mockReset();
});
it('should log errors to console on fire and forget save operation', () => {
// Promise used to trigger the error condition
const saveDataPromise = Promise.resolve();
mockPlugin.saveData.mockImplementationOnce((_data: SettingsData) => {
// throw to simulate saveData() failing. This happens first
return saveDataPromise.then(() => {
throw new Error('saveData() unit test mock error');
});
});
// Promise used to track the call to console.log
let consoleLogPromiseResolveFn: (value: void | PromiseLike<void>) => void;
const consoleLogPromise = new Promise<void>((resolve, _reject) => {
consoleLogPromiseResolveFn = resolve;
});
const consoleLogSpy = jest
.spyOn(console, 'log')
.mockImplementation((message: string) => {
if (message.startsWith('Switcher++: error saving changes to settings')) {
// resolve the consoleLogPromise. This happens second and will allow
// allPromises to resolve itself
consoleLogPromiseResolveFn();
}
});
// wait for the other promises to resolve before this promise can resolve
const allPromises = Promise.all([saveDataPromise, consoleLogPromise]);
sut.save();
// when all the promises are resolved check expectations and clean up
return allPromises.finally(() => {
expect(mockPlugin.saveData).toHaveBeenCalled();
expect(consoleLogSpy).toHaveBeenCalled();
consoleLogSpy.mockRestore();
});
});
});