Files
element-call/component/ElementCall.test.tsx
T
Timo K. 2a8836e44a never use rtcSession.room (Relation classes refactor)
make sure everything is passed through dedicated room / client props.
2026-11-20 01:01:28 +01:00

122 lines
3.7 KiB
TypeScript

/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { type MatrixClient, type Room } from "matrix-js-sdk";
import { MatrixRTCSession } from "matrix-js-sdk/lib/matrixrtc";
import { mockEmitter, mockMatrixRoom, MockRTCSession } from "../src/utils/test";
import { ElementCall } from "./index";
// The call itself is out of scope: what matters is the session it is handed
const callView = vi.hoisted(() => vi.fn());
vi.mock("../src/room/CallView", () => ({
CallView: (props: { rtcSession: MatrixRTCSession }): string => {
callView(props);
return "call";
},
}));
// Media devices need a browser jsdom does not have
vi.mock("../src/state/AppViewModel", () => ({
AppViewModel: class {
public mediaDevices = {};
public backgroundEffects = {};
},
}));
const roomId = "!room:example.org";
let room: Room;
let sessions: MockRTCSession[];
// No `matrixRTC`: the host's session manager is not ours to use
function mockClient(knownRoom: Room | null = room): MatrixClient {
return {
...mockEmitter(),
getRoom: (id: string) => (id === roomId ? knownRoom : null),
// What the call view's client is made from
baseUrl: "https://matrix.example.org",
getUserId: () => "@alice:example.org",
getDeviceId: () => "DEVICE",
getAccessToken: () => "token",
getSyncState: () => null,
getUser: () => null,
getCrypto: () => undefined,
sendEvent: vi.fn(),
redactEvent: vi.fn(),
decryptEventIfNeeded: vi.fn(),
setDisplayName: vi.fn(),
setAvatarUrl: vi.fn(),
uploadContent: vi.fn(),
doesServerSupportUnstableFeature: vi.fn(),
} as unknown as MatrixClient;
}
beforeEach(() => {
room = mockMatrixRoom({ roomId });
sessions = [];
vi.spyOn(MatrixRTCSession, "sessionForSlot").mockImplementation(() => {
const session = new MockRTCSession(room);
sessions.push(session);
return session as unknown as MatrixRTCSession;
});
});
afterEach(() => {
vi.restoreAllMocks();
callView.mockClear();
});
test("calls in the room's default slot, with a session of its own", async () => {
const client = mockClient();
render(<ElementCall client={client} roomId={roomId} />);
await screen.findByText("call");
expect(MatrixRTCSession.sessionForSlot).toHaveBeenCalledExactlyOnceWith(
client,
room,
{ application: "m.call", id: "ROOM" },
);
expect(callView).toHaveBeenLastCalledWith(
expect.objectContaining({ rtcSession: sessions[0] }),
);
});
test("stops its session when unmounted", async () => {
const { unmount } = render(
<ElementCall client={mockClient()} roomId={roomId} />,
);
await screen.findByText("call");
expect(sessions[0].stop).not.toHaveBeenCalled();
unmount();
expect(sessions[0].stop).toHaveBeenCalledOnce();
});
test("swaps the session for a new one with a new client", async () => {
const { rerender } = render(
<ElementCall client={mockClient()} roomId={roomId} />,
);
await screen.findByText("call");
rerender(<ElementCall client={mockClient()} roomId={roomId} />);
await vi.waitFor(() =>
expect(callView).toHaveBeenLastCalledWith(
expect.objectContaining({ rtcSession: sessions[1] }),
),
);
expect(sessions[0].stop).toHaveBeenCalledOnce();
expect(sessions[1].stop).not.toHaveBeenCalled();
});
test("shows no call in a room the host's client does not know", () => {
render(<ElementCall client={mockClient(null)} roomId={roomId} />);
expect(MatrixRTCSession.sessionForSlot).not.toHaveBeenCalled();
expect(screen.queryByText("call")).toBeNull();
});