This commit is contained in:
Michael Telatynski
2026-06-08 13:34:08 +01:00
parent 99dcfe7fe1
commit 6f46493b4d
2 changed files with 25 additions and 36 deletions
@@ -65,7 +65,7 @@ test.describe("Widget Lifecycle", () => {
});
test("auto-approves preload and identity", async ({ page, user, homeserver }, testInfo) => {
rejectToastIfExists(page, "Verify this device");
await rejectToastIfExists(page, "Verify this device");
// A bot creates a room with the widget pinned to the top panel, then invites the test user.
// Because the widget was added by a different user (the bot), Element would normally show a
@@ -123,7 +123,7 @@ test.describe("Widget Lifecycle", () => {
});
test("prompts for capabilities not in the allowlist", async ({ page, user, homeserver }, testInfo) => {
rejectToastIfExists(page, "Verify this device");
await rejectToastIfExists(page, "Verify this device");
const bot = await homeserver.registerUser(`bot_${testInfo.testId}`, "password", "Bot");
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
@@ -186,7 +186,7 @@ test.describe("Widget Lifecycle", () => {
user,
homeserver,
}, testInfo) => {
rejectToastIfExists(page, "Verify this device");
await rejectToastIfExists(page, "Verify this device");
const bot = await homeserver.registerUser(`bot_${testInfo.testId}`, "password", "Bot");
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
+22 -33
View File
@@ -6,7 +6,7 @@ Please see LICENSE files in the repository root for full details.
*/
import { beforeEach, describe, expect, test, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { render, type RenderResult, screen } from "@testing-library/react";
import { type Api } from "@element-hq/element-web-module-api";
import { type IWidget } from "matrix-widget-api";
@@ -98,6 +98,25 @@ describe("WidgetToggleModule", () => {
return (api.extras.addRoomHeaderButtonCallback as ReturnType<typeof vi.fn>).mock.calls[0][0];
};
const mockAndRender = async (api: Api): Promise<RenderResult> => {
(api.i18n.translate as ReturnType<typeof vi.fn>).mockImplementation(
(key: string, vars?: Record<string, string>) => {
let result = key;
if (vars) {
for (const [k, v] of Object.entries(vars)) {
result = result.replace(`%(${k})s`, v);
}
}
return result;
},
);
const callback = await getCallback(api);
const result = callback(roomId);
expect(result).toBeDefined();
return render(result!);
};
test("returns undefined when there are no widgets in the room", async () => {
const api = makeApi([]);
const callback = await getCallback(api);
@@ -119,22 +138,7 @@ describe("WidgetToggleModule", () => {
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
mockWidget({ id: "w2", type: "m.custom", name: "Widget Two" }),
]);
(api.i18n.translate as ReturnType<typeof vi.fn>).mockImplementation(
(key: string, vars?: Record<string, string>) => {
let result = key;
if (vars) {
for (const [k, v] of Object.entries(vars)) {
result = result.replace(`%(${k})s`, v);
}
}
return result;
},
);
const callback = await getCallback(api);
const result = callback(roomId);
expect(result).toBeDefined();
render(result!);
await mockAndRender(api);
expect(screen.getAllByRole("button").length).toBe(2);
});
@@ -144,22 +148,7 @@ describe("WidgetToggleModule", () => {
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
mockWidget({ id: "w2", type: "m.other", name: "Widget Other" }),
]);
(api.i18n.translate as ReturnType<typeof vi.fn>).mockImplementation(
(key: string, vars?: Record<string, string>) => {
let result = key;
if (vars) {
for (const [k, v] of Object.entries(vars)) {
result = result.replace(`%(${k})s`, v);
}
}
return result;
},
);
const callback = await getCallback(api);
const result = callback(roomId);
expect(result).toBeDefined();
render(result!);
await mockAndRender(api);
expect(screen.getAllByRole("button").length).toBe(1);
});