mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-08-29 14:57:05 +00:00
Adapt settings pages to Obsidian's declarative API
This commit is contained in:
@@ -7,10 +7,25 @@ const negotiationMocks = vi.hoisted(() => ({
|
||||
|
||||
vi.mock("@/deps.ts", () => ({
|
||||
App: class {},
|
||||
Component: class {},
|
||||
Component: class {
|
||||
load = vi.fn();
|
||||
unload = vi.fn();
|
||||
register = vi.fn();
|
||||
},
|
||||
PluginSettingTab: class {},
|
||||
SettingPage: undefined,
|
||||
requireApiVersion: vi.fn(() => false),
|
||||
}));
|
||||
vi.mock("@/main.ts", () => ({ default: class {} }));
|
||||
vi.mock("@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions", () => ({
|
||||
getLanguage: vi.fn(() => "en"),
|
||||
compatGlobal: {
|
||||
localStorage: {
|
||||
getItem: vi.fn(() => null),
|
||||
setItem: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
vi.mock("@/common/events.ts", () => ({
|
||||
EVENT_REQUEST_RELOAD_SETTING_TAB: "request-reload-setting-tab",
|
||||
eventHub: { onEvent: vi.fn() },
|
||||
@@ -73,3 +88,101 @@ describe("ObsidianLiveSyncSettingTab passphrase verification", () => {
|
||||
expect(remoteDatabase.close).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
describe("ObsidianLiveSyncSettingTab declarative settings boundary", () => {
|
||||
function createSettingsTab() {
|
||||
const saveSettingData = vi.fn(async () => undefined);
|
||||
const plugin = {
|
||||
app: {},
|
||||
core: {
|
||||
settings: {
|
||||
...DEFAULT_SETTINGS,
|
||||
hashCacheMaxCount: 300,
|
||||
displayLanguage: "",
|
||||
},
|
||||
services: {
|
||||
setting: {
|
||||
saveSettingData,
|
||||
getDeviceAndVaultName: vi.fn(() => ""),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
Object.defineProperty(plugin, "settings", {
|
||||
get: () => {
|
||||
throw new Error("The declarative adapter must not use plugin.settings");
|
||||
},
|
||||
});
|
||||
const tab = new ObsidianLiveSyncSettingTab({} as never, plugin as never);
|
||||
Object.assign(tab, {
|
||||
_editingSettings: {
|
||||
...DEFAULT_SETTINGS,
|
||||
hashCacheMaxCount: 300,
|
||||
displayLanguage: "",
|
||||
},
|
||||
initialSettings: {
|
||||
...DEFAULT_SETTINGS,
|
||||
hashCacheMaxCount: 300,
|
||||
displayLanguage: "",
|
||||
},
|
||||
});
|
||||
return { tab, saveSettingData };
|
||||
}
|
||||
|
||||
it("loads the imperative fallback without a SettingPage runtime export", () => {
|
||||
const { tab } = createSettingsTab();
|
||||
|
||||
expect(tab.display).toBeTypeOf("function");
|
||||
expect(tab.getSettingDefinitions()).toEqual([]);
|
||||
});
|
||||
|
||||
it("reads and writes registered controls through the editing buffer and existing save owner", async () => {
|
||||
const { tab } = createSettingsTab();
|
||||
const saveSettings = vi.spyOn(tab, "saveSettings").mockResolvedValue(undefined);
|
||||
|
||||
expect(tab.getControlValue("hashCacheMaxCount")).toBe(300);
|
||||
|
||||
await tab.setControlValue("hashCacheMaxCount", 321);
|
||||
|
||||
expect(tab.editingSettings.hashCacheMaxCount).toBe(321);
|
||||
expect(saveSettings).toHaveBeenCalledOnce();
|
||||
expect(saveSettings).toHaveBeenCalledWith(["hashCacheMaxCount"]);
|
||||
});
|
||||
|
||||
it("rejects unregistered declarative control keys", async () => {
|
||||
const { tab } = createSettingsTab();
|
||||
|
||||
expect(() => tab.getControlValue("couchDB_PASSWORD")).toThrow(/Unknown declarative setting key/u);
|
||||
await expect(tab.setControlValue("couchDB_PASSWORD", "secret")).rejects.toThrow(
|
||||
/Unknown declarative setting key/u
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects declarative values outside the registered control contract", async () => {
|
||||
const { tab } = createSettingsTab();
|
||||
const saveSettings = vi.spyOn(tab, "saveSettings").mockResolvedValue(undefined);
|
||||
|
||||
await expect(tab.setControlValue("hashCacheMaxCount", 9)).rejects.toThrow(
|
||||
/Invalid value for declarative setting/u
|
||||
);
|
||||
await expect(tab.setControlValue("chunkSplitterVersion", "unknown-splitter")).rejects.toThrow(
|
||||
/Invalid value for declarative setting/u
|
||||
);
|
||||
|
||||
expect(saveSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("replaces a saved-setting handler when a page is rendered again", async () => {
|
||||
const { tab } = createSettingsTab();
|
||||
const first = vi.fn();
|
||||
const replacement = vi.fn();
|
||||
tab.addOnSaved("displayLanguage", first);
|
||||
tab.addOnSaved("displayLanguage", replacement);
|
||||
tab.editingSettings.displayLanguage = "ja";
|
||||
|
||||
await tab.saveSettings(["displayLanguage"]);
|
||||
|
||||
expect(first).not.toHaveBeenCalled();
|
||||
expect(replacement).toHaveBeenCalledOnce();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user