Introduce a Module API to get application settings. (#34278)
* Add a module API to get application settings. * fix test
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { test, expect } from "../../element-web-test";
|
||||
import { getSampleFilePath } from "../../sample-files";
|
||||
|
||||
test.describe("Settings API", () => {
|
||||
test.use({
|
||||
displayName: "Manny",
|
||||
config: {
|
||||
modules: ["/modules/settings-module.js"],
|
||||
setting_defaults: {
|
||||
language: "de",
|
||||
},
|
||||
},
|
||||
page: async ({ page }, use) => {
|
||||
await page.route("/modules/settings-module.js", async (route) => {
|
||||
await route.fulfill({ path: getSampleFilePath("settings-module.js") });
|
||||
});
|
||||
await use(page);
|
||||
},
|
||||
});
|
||||
|
||||
test("should read a config-resolved setting value via api.settings.getValue", async ({ page }) => {
|
||||
const dialogPromise = page.waitForEvent("dialog");
|
||||
await page.goto("/");
|
||||
const dialog = await dialogPromise;
|
||||
expect(dialog.message()).toBe("de");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
export default class SettingsModule {
|
||||
static moduleApiVersion = "^*";
|
||||
constructor(api) {
|
||||
this.api = api;
|
||||
}
|
||||
async load() {
|
||||
alert(this.api.settings.getValue("language"));
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,7 @@ import { WidgetLifecycleApi } from "./WidgetLifecycleApi.ts";
|
||||
import { WidgetApi } from "./WidgetApi.ts";
|
||||
import { CustomisationsApi } from "./customisationsApi.ts";
|
||||
import { ComposerApi } from "./ComposerApi.ts";
|
||||
import { SettingsApi } from "./SettingsApi.ts";
|
||||
import defaultDispatcher from "../dispatcher/dispatcher.ts";
|
||||
|
||||
const legacyCustomisationsFactory = <T extends object>(baseCustomisations: T) => {
|
||||
@@ -98,6 +99,7 @@ export class ModuleApi implements Api {
|
||||
public readonly client = new ClientApi();
|
||||
public readonly stores = new StoresApi();
|
||||
public readonly composer = new ComposerApi(defaultDispatcher);
|
||||
public readonly settings = new SettingsApi();
|
||||
|
||||
public createRoot(element: Element): Root {
|
||||
return createRoot(element);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
|
||||
import SettingsStore from "../settings/SettingsStore";
|
||||
import { SettingsApi } from "./SettingsApi";
|
||||
|
||||
describe("SettingsApi", () => {
|
||||
it("should return the value from SettingsStore.getValue", () => {
|
||||
const spy = vi.spyOn(SettingsStore, "getValue").mockReturnValue("en" as any);
|
||||
const api = new SettingsApi();
|
||||
expect(api.getValue("language")).toBe("en");
|
||||
expect(spy).toHaveBeenCalledWith("language", undefined, undefined);
|
||||
});
|
||||
|
||||
it("should pass roomId and excludeDefault through to SettingsStore.getValue", () => {
|
||||
const spy = vi.spyOn(SettingsStore, "getValue").mockReturnValue(null);
|
||||
const api = new SettingsApi();
|
||||
api.getValue("m.setting", "!room:example.org", true);
|
||||
expect(spy).toHaveBeenCalledWith("m.setting", "!room:example.org", true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import type { SettingsApi as ISettingsApi } from "@element-hq/element-web-module-api";
|
||||
import SettingsStore from "../settings/SettingsStore";
|
||||
|
||||
export class SettingsApi implements ISettingsApi {
|
||||
public getValue<T = any>(settingName: string, roomId?: string | null, excludeDefault?: boolean): T | undefined {
|
||||
//@ts-expect-error: SettingsStore.getValue will throw on an invalid setting name anyway.
|
||||
return SettingsStore.getValue(settingName, roomId, excludeDefault);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user