Fix some tests

This commit is contained in:
Robin
2026-10-08 10:22:57 +02:00
parent 62fe492fd3
commit f5f215f0ef
2 changed files with 50 additions and 28 deletions
+48 -26
View File
@@ -7,7 +7,7 @@ Please see LICENSE in the repository root for full details.
import { afterEach, describe, expect, it, type Mock, vi } from "vitest"; import { afterEach, describe, expect, it, type Mock, vi } from "vitest";
import { render, waitFor, screen } from "@testing-library/react"; import { render, waitFor, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event"; import userEvent, { type UserEvent } from "@testing-library/user-event";
import { TooltipProvider } from "@vector-im/compound-web"; import { TooltipProvider } from "@vector-im/compound-web";
import type { MatrixClient } from "matrix-js-sdk"; import type { MatrixClient } from "matrix-js-sdk";
@@ -24,7 +24,7 @@ import {
matrixRTCMode as matrixRTCModeSetting, matrixRTCMode as matrixRTCModeSetting,
} from "./settings"; } from "./settings";
import { MatrixRTCMode } from "../config/ConfigOptions"; import { MatrixRTCMode } from "../config/ConfigOptions";
import { mockConfig } from "../utils/test"; import { exampleTransport, mockConfig } from "../utils/test";
import { getSFUConfig } from "../livekit/auth"; import { getSFUConfig } from "../livekit/auth";
// Mock url params hook to avoid environment-dependent snapshot churn. // Mock url params hook to avoid environment-dependent snapshot churn.
@@ -77,6 +77,21 @@ function createMockMatrixClient(): MatrixClient {
} as unknown as MatrixClient; } as unknown as MatrixClient;
} }
/**
* Types the JSON representation of an object into the given element.
*/
async function typeJson(
user: UserEvent,
element: Element,
object: object,
): Promise<void> {
// `user.type` requires us to escape brackets by doubling them
await user.type(
element,
JSON.stringify(object).replace(/([{[\]])/g, (c) => c + c),
);
}
describe("DeveloperSettingsTab", () => { describe("DeveloperSettingsTab", () => {
it("renders and matches snapshot", async () => { it("renders and matches snapshot", async () => {
const client = createMockMatrixClient(); const client = createMockMatrixClient();
@@ -124,7 +139,7 @@ describe("DeveloperSettingsTab", () => {
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
}); });
describe("custom livekit url", () => { describe("custom transport", () => {
afterEach(() => { afterEach(() => {
customTransportSetting.setValue(null); customTransportSetting.setValue(null);
}); });
@@ -134,7 +149,7 @@ describe("DeveloperSettingsTab", () => {
getUserId: () => "@u:hs", getUserId: () => "@u:hs",
getDeviceId: () => "DEVICE", getDeviceId: () => "DEVICE",
} as unknown as MatrixClient; } as unknown as MatrixClient;
it("will not update custom livekit url without roomId", async () => { it("will not update custom transport without roomId", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render( render(
@@ -147,9 +162,9 @@ describe("DeveloperSettingsTab", () => {
</TooltipProvider>, </TooltipProvider>,
); );
const input = screen.getByLabelText("Custom Livekit-url"); const input = screen.getByLabelText("Custom transport JSON");
await user.clear(input); await user.clear(input);
await user.type(input, "wss://example.livekit.invalid"); await typeJson(user, input, exampleTransport);
const saveButton = screen.getByRole("button", { name: "Save" }); const saveButton = screen.getByRole("button", { name: "Save" });
await user.click(saveButton); await user.click(saveButton);
@@ -157,7 +172,7 @@ describe("DeveloperSettingsTab", () => {
expect(customTransportSetting.getValue()).toBe(null); expect(customTransportSetting.getValue()).toBe(null);
}); });
it("will not update custom livekit url without text in input", async () => { it("will not update custom transport without text in input", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render( render(
@@ -171,7 +186,7 @@ describe("DeveloperSettingsTab", () => {
</TooltipProvider>, </TooltipProvider>,
); );
const input = screen.getByLabelText("Custom Livekit-url"); const input = screen.getByLabelText("Custom transport JSON");
await user.clear(input); await user.clear(input);
const saveButton = screen.getByRole("button", { name: "Save" }); const saveButton = screen.getByRole("button", { name: "Save" });
@@ -180,7 +195,7 @@ describe("DeveloperSettingsTab", () => {
expect(customTransportSetting.getValue()).toBe(null); expect(customTransportSetting.getValue()).toBe(null);
}); });
it("will not update custom livekit url when pressing cancel", async () => { it("will not update custom transport when pressing cancel", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render( render(
@@ -194,7 +209,7 @@ describe("DeveloperSettingsTab", () => {
</TooltipProvider>, </TooltipProvider>,
); );
const input = screen.getByLabelText("Custom Livekit-url"); const input = screen.getByLabelText("Custom transport JSON");
await user.clear(input); await user.clear(input);
await user.type(input, "wss://example.livekit.invalid"); await user.type(input, "wss://example.livekit.invalid");
@@ -206,7 +221,7 @@ describe("DeveloperSettingsTab", () => {
expect(customTransportSetting.getValue()).toBe(null); expect(customTransportSetting.getValue()).toBe(null);
}); });
it("will update custom livekit url", async () => { it("will update custom transport", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render( render(
@@ -220,24 +235,28 @@ describe("DeveloperSettingsTab", () => {
</TooltipProvider>, </TooltipProvider>,
); );
const input = screen.getByLabelText("Custom Livekit-url"); const input = screen.getByLabelText("Custom transport JSON");
await user.clear(input); await user.clear(input);
await user.type(input, "wss://example.livekit.valid"); await typeJson(user, input, {
type: "livekit",
url: "wss://example.livekit.valid",
});
const saveButton = screen.getByRole("button", { name: "Save" }); const saveButton = screen.getByRole("button", { name: "Save" });
await user.click(saveButton); await user.click(saveButton);
expect(getSFUConfig).toHaveBeenCalledWith( expect(getSFUConfig).toHaveBeenCalledWith(
expect.anything(), expect.objectContaining({
expect.anything(), transport: { type: "livekit", url: "wss://example.livekit.valid" },
"wss://example.livekit.valid", roomId: "#testRoom",
"#testRoom", }),
); );
expect(customTransportSetting.getValue()).toBe( expect(customTransportSetting.getValue()).toEqual({
"wss://example.livekit.valid", type: "livekit",
); url: "wss://example.livekit.valid",
});
}); });
it("will show error on invalid url", async () => { it("will show error on unreachable transport", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
render( render(
@@ -251,24 +270,27 @@ describe("DeveloperSettingsTab", () => {
</TooltipProvider>, </TooltipProvider>,
); );
const input = screen.getByLabelText("Custom Livekit-url"); const input = screen.getByLabelText("Custom transport JSON");
await user.clear(input); await user.clear(input);
await user.type(input, "wss://example.livekit.valid"); await typeJson(user, input, {
type: "livekit",
url: "wss://example.livekit.valid",
});
const saveButton = screen.getByRole("button", { name: "Save" }); const saveButton = screen.getByRole("button", { name: "Save" });
(getSFUConfig as Mock).mockImplementation(() => { (getSFUConfig as Mock).mockImplementation(() => {
throw new Error("Invalid URL"); throw new Error("Failed to get SFU config");
}); });
await user.click(saveButton); await user.click(saveButton);
expect( expect(
screen.getByText("invalid URL (did not update)"), screen.getByText("invalid transport (did not update)"),
).toBeInTheDocument(); ).toBeInTheDocument();
expect(customTransportSetting.getValue()).toBe(null); expect(customTransportSetting.getValue()).toBe(null);
}); });
}); });
// Add this test inside the describe("DeveloperSettingsTab", () => { block, // Add this test inside the describe("DeveloperSettingsTab", () => { block,
// after the custom livekit url tests: // after the custom transport tests:
describe("enable extended livekit logs", () => { describe("enable extended livekit logs", () => {
afterEach(() => { afterEach(() => {
@@ -239,7 +239,7 @@ exports[`DeveloperSettingsTab > renders and matches snapshot 1`] = `
class="_label_1o4d9_60" class="_label_1o4d9_60"
for="radix-_r_7_" for="radix-_r_7_"
> >
Custom Livekit-url Custom transport JSON
</label> </label>
<div <div
class="_controls_17lij_8" class="_controls_17lij_8"
@@ -250,7 +250,7 @@ exports[`DeveloperSettingsTab > renders and matches snapshot 1`] = `
id="radix-_r_7_" id="radix-_r_7_"
name="input" name="input"
title="" title=""
value="" value="null"
/> />
</div> </div>
<span <span