DRY
This commit is contained in:
@@ -65,7 +65,7 @@ test.describe("Widget Lifecycle", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("auto-approves preload and identity", async ({ page, user, homeserver }, testInfo) => {
|
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.
|
// 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
|
// 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) => {
|
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 bot = await homeserver.registerUser(`bot_${testInfo.testId}`, "password", "Bot");
|
||||||
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
|
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
|
||||||
@@ -186,7 +186,7 @@ test.describe("Widget Lifecycle", () => {
|
|||||||
user,
|
user,
|
||||||
homeserver,
|
homeserver,
|
||||||
}, testInfo) => {
|
}, testInfo) => {
|
||||||
rejectToastIfExists(page, "Verify this device");
|
await rejectToastIfExists(page, "Verify this device");
|
||||||
|
|
||||||
const bot = await homeserver.registerUser(`bot_${testInfo.testId}`, "password", "Bot");
|
const bot = await homeserver.registerUser(`bot_${testInfo.testId}`, "password", "Bot");
|
||||||
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
|
const { room_id: roomId } = await homeserver.csApi.request<{ room_id: string }>(
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ Please see LICENSE files in the repository root for full details.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
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 Api } from "@element-hq/element-web-module-api";
|
||||||
import { type IWidget } from "matrix-widget-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];
|
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 () => {
|
test("returns undefined when there are no widgets in the room", async () => {
|
||||||
const api = makeApi([]);
|
const api = makeApi([]);
|
||||||
const callback = await getCallback(api);
|
const callback = await getCallback(api);
|
||||||
@@ -119,22 +138,7 @@ describe("WidgetToggleModule", () => {
|
|||||||
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
|
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
|
||||||
mockWidget({ id: "w2", type: "m.custom", name: "Widget Two" }),
|
mockWidget({ id: "w2", type: "m.custom", name: "Widget Two" }),
|
||||||
]);
|
]);
|
||||||
(api.i18n.translate as ReturnType<typeof vi.fn>).mockImplementation(
|
await mockAndRender(api);
|
||||||
(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!);
|
|
||||||
|
|
||||||
expect(screen.getAllByRole("button").length).toBe(2);
|
expect(screen.getAllByRole("button").length).toBe(2);
|
||||||
});
|
});
|
||||||
@@ -144,22 +148,7 @@ describe("WidgetToggleModule", () => {
|
|||||||
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
|
mockWidget({ id: "w1", type: "m.custom", name: "Widget One" }),
|
||||||
mockWidget({ id: "w2", type: "m.other", name: "Widget Other" }),
|
mockWidget({ id: "w2", type: "m.other", name: "Widget Other" }),
|
||||||
]);
|
]);
|
||||||
(api.i18n.translate as ReturnType<typeof vi.fn>).mockImplementation(
|
await mockAndRender(api);
|
||||||
(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!);
|
|
||||||
|
|
||||||
expect(screen.getAllByRole("button").length).toBe(1);
|
expect(screen.getAllByRole("button").length).toBe(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user