2022-08-30 19:13:39 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-08-30 19:13:39 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-08-30 19:13:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { zip } from "lodash";
|
2022-09-27 11:54:51 +00:00
|
|
|
import { render, screen, act, fireEvent, waitFor, cleanup } from "@testing-library/react";
|
2022-08-30 19:13:39 +00:00
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2023-08-09 07:18:41 +00:00
|
|
|
import { MatrixClient, PendingEventOrdering, Room, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-08-30 19:13:39 +00:00
|
|
|
import { Widget } from "matrix-widget-api";
|
|
|
|
|
2023-08-04 11:22:08 +00:00
|
|
|
import type { RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-08-30 19:13:39 +00:00
|
|
|
import type { ClientWidgetApi } from "matrix-widget-api";
|
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
mkRoomMember,
|
2022-09-27 11:54:51 +00:00
|
|
|
wrapInMatrixClientContext,
|
2022-08-30 19:13:39 +00:00
|
|
|
useMockedCalls,
|
2022-09-27 11:54:51 +00:00
|
|
|
MockedCall,
|
2022-08-30 19:13:39 +00:00
|
|
|
setupAsyncStoreWithClient,
|
2023-08-16 08:29:34 +00:00
|
|
|
useMockMediaDevices,
|
2022-08-30 19:13:39 +00:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-09-27 11:54:51 +00:00
|
|
|
import { CallView as _CallView } from "../../../../src/components/views/voip/CallView";
|
2022-08-30 19:13:39 +00:00
|
|
|
import { WidgetMessagingStore } from "../../../../src/stores/widgets/WidgetMessagingStore";
|
|
|
|
import { CallStore } from "../../../../src/stores/CallStore";
|
2022-09-27 11:54:51 +00:00
|
|
|
import { Call, ConnectionState } from "../../../../src/models/Call";
|
|
|
|
|
|
|
|
const CallView = wrapInMatrixClientContext(_CallView);
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2023-08-16 08:29:34 +00:00
|
|
|
describe("CallView", () => {
|
2022-08-30 19:13:39 +00:00
|
|
|
useMockedCalls();
|
|
|
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
|
|
|
|
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
let room: Room;
|
|
|
|
let alice: RoomMember;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-08-16 08:29:34 +00:00
|
|
|
useMockMediaDevices();
|
2022-08-30 19:13:39 +00:00
|
|
|
|
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2022-08-30 19:13:39 +00:00
|
|
|
|
|
|
|
room = new Room("!1:example.org", client, "@alice:example.org", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
alice = mkRoomMember(room.roomId, "@alice:example.org");
|
|
|
|
jest.spyOn(room, "getMember").mockImplementation((userId) => (userId === alice.userId ? alice : null));
|
|
|
|
|
|
|
|
client.getRoom.mockImplementation((roomId) => (roomId === room.roomId ? room : null));
|
|
|
|
client.getRooms.mockReturnValue([room]);
|
|
|
|
client.reEmitter.reEmit(room, [RoomStateEvent.Events]);
|
|
|
|
|
|
|
|
setupAsyncStoreWithClient(CallStore.instance, client);
|
|
|
|
setupAsyncStoreWithClient(WidgetMessagingStore.instance, client);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
client.reEmitter.stopReEmitting(room, [RoomStateEvent.Events]);
|
|
|
|
});
|
|
|
|
|
2024-01-29 16:06:12 +00:00
|
|
|
const renderView = async (skipLobby = false): Promise<void> => {
|
2024-04-15 13:54:37 +00:00
|
|
|
render(<CallView room={room} resizing={false} waitForCall={false} skipLobby={skipLobby} />);
|
2022-08-30 19:13:39 +00:00
|
|
|
await act(() => Promise.resolve()); // Let effects settle
|
|
|
|
};
|
|
|
|
|
2022-09-27 11:54:51 +00:00
|
|
|
describe("with an existing call", () => {
|
|
|
|
let call: MockedCall;
|
|
|
|
let widget: Widget;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
MockedCall.create(room, "1");
|
2022-10-07 02:27:28 +00:00
|
|
|
const maybeCall = CallStore.instance.getCall(room.roomId);
|
2022-09-27 11:54:51 +00:00
|
|
|
if (!(maybeCall instanceof MockedCall)) throw new Error("Failed to create call");
|
|
|
|
call = maybeCall;
|
|
|
|
|
|
|
|
widget = new Widget(call.widget);
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, room.roomId, {
|
|
|
|
stop: () => {},
|
|
|
|
} as unknown as ClientWidgetApi);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
cleanup(); // Unmount before we do any cleanup that might update the component
|
|
|
|
call.destroy();
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, room.roomId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("calls clean on mount", async () => {
|
|
|
|
const cleanSpy = jest.spyOn(call, "clean");
|
|
|
|
await renderView();
|
|
|
|
expect(cleanSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-08-24 03:48:35 +00:00
|
|
|
/**
|
|
|
|
* TODO: Fix I do not understand this test
|
|
|
|
*/
|
|
|
|
it.skip("tracks participants", async () => {
|
2022-09-27 11:54:51 +00:00
|
|
|
const bob = mkRoomMember(room.roomId, "@bob:example.org");
|
|
|
|
const carol = mkRoomMember(room.roomId, "@carol:example.org");
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-09-27 11:54:51 +00:00
|
|
|
const expectAvatars = (userIds: string[]) => {
|
2023-07-07 10:54:43 +00:00
|
|
|
const avatars = screen.queryAllByRole("button", { name: "Profile picture" });
|
2022-09-27 11:54:51 +00:00
|
|
|
expect(userIds.length).toBe(avatars.length);
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-09-27 11:54:51 +00:00
|
|
|
for (const [userId, avatar] of zip(userIds, avatars)) {
|
|
|
|
fireEvent.focus(avatar!);
|
2022-11-28 21:37:32 +00:00
|
|
|
screen.getAllByRole("tooltip", { name: userId });
|
2022-09-27 11:54:51 +00:00
|
|
|
}
|
|
|
|
};
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-09-27 11:54:51 +00:00
|
|
|
await renderView();
|
|
|
|
expect(screen.queryByLabelText(/joined/)).toBe(null);
|
|
|
|
expectAvatars([]);
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-11-28 21:37:32 +00:00
|
|
|
act(() => {
|
|
|
|
call.participants = new Map([[alice, new Set(["a"])]]);
|
|
|
|
});
|
2022-09-27 11:54:51 +00:00
|
|
|
screen.getByText("1 person joined");
|
|
|
|
expectAvatars([alice.userId]);
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-11-28 21:37:32 +00:00
|
|
|
act(() => {
|
|
|
|
call.participants = new Map([
|
|
|
|
[alice, new Set(["a"])],
|
|
|
|
[bob, new Set(["b1", "b2"])],
|
|
|
|
[carol, new Set(["c"])],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
screen.getByText("4 people joined");
|
|
|
|
expectAvatars([alice.userId, bob.userId, bob.userId, carol.userId]);
|
2022-08-30 19:13:39 +00:00
|
|
|
|
2022-11-28 21:37:32 +00:00
|
|
|
act(() => {
|
|
|
|
call.participants = new Map();
|
|
|
|
});
|
2022-09-27 11:54:51 +00:00
|
|
|
expect(screen.queryByLabelText(/joined/)).toBe(null);
|
|
|
|
expectAvatars([]);
|
|
|
|
});
|
|
|
|
|
2024-01-29 16:06:12 +00:00
|
|
|
it("automatically connects to the call when skipLobby is true", async () => {
|
|
|
|
const connectSpy = jest.spyOn(call, "start");
|
|
|
|
await renderView(true);
|
2022-09-27 11:54:51 +00:00
|
|
|
await waitFor(() => expect(connectSpy).toHaveBeenCalled(), { interval: 1 });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("without an existing call", () => {
|
|
|
|
it("creates and connects to a new call when the join button is pressed", async () => {
|
|
|
|
expect(Call.get(room)).toBeNull();
|
2024-01-29 16:06:12 +00:00
|
|
|
await renderView(true);
|
2022-10-07 02:27:28 +00:00
|
|
|
await waitFor(() => expect(CallStore.instance.getCall(room.roomId)).not.toBeNull());
|
|
|
|
const call = CallStore.instance.getCall(room.roomId)!;
|
2022-09-27 11:54:51 +00:00
|
|
|
|
|
|
|
const widget = new Widget(call.widget);
|
|
|
|
WidgetMessagingStore.instance.storeMessaging(widget, room.roomId, {
|
|
|
|
stop: () => {},
|
|
|
|
} as unknown as ClientWidgetApi);
|
|
|
|
await waitFor(() => expect(call.connectionState).toBe(ConnectionState.Connected));
|
|
|
|
|
|
|
|
cleanup(); // Unmount before we do any cleanup that might update the component
|
|
|
|
call.destroy();
|
|
|
|
WidgetMessagingStore.instance.stopMessaging(widget, room.roomId);
|
|
|
|
});
|
2022-08-30 19:13:39 +00:00
|
|
|
});
|
|
|
|
});
|