2022-08-30 21:13:39 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { zip } from "lodash";
|
2022-09-27 13:54:51 +02:00
|
|
|
import { render, screen, act, fireEvent, waitFor, cleanup } from "@testing-library/react";
|
2022-08-30 21:13:39 +02:00
|
|
|
import { mocked, Mocked } from "jest-mock";
|
2023-08-09 09:18:41 +02:00
|
|
|
import { MatrixClient, PendingEventOrdering, Room, RoomStateEvent } from "matrix-js-sdk/src/matrix";
|
2022-08-30 21:13:39 +02:00
|
|
|
import { Widget } from "matrix-widget-api";
|
2024-01-11 20:56:36 +01:00
|
|
|
import { TooltipProvider } from "@vector-im/compound-web";
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2023-08-04 13:22:08 +02:00
|
|
|
import type { RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-08-30 21:13:39 +02:00
|
|
|
import type { ClientWidgetApi } from "matrix-widget-api";
|
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
mkRoomMember,
|
2022-09-27 13:54:51 +02:00
|
|
|
wrapInMatrixClientContext,
|
2022-08-30 21:13:39 +02:00
|
|
|
useMockedCalls,
|
2022-09-27 13:54:51 +02:00
|
|
|
MockedCall,
|
2022-08-30 21:13:39 +02:00
|
|
|
setupAsyncStoreWithClient,
|
2023-08-16 10:29:34 +02:00
|
|
|
useMockMediaDevices,
|
2022-08-30 21:13:39 +02:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-09-27 13:54:51 +02:00
|
|
|
import { CallView as _CallView } from "../../../../src/components/views/voip/CallView";
|
2022-08-30 21:13:39 +02:00
|
|
|
import { WidgetMessagingStore } from "../../../../src/stores/widgets/WidgetMessagingStore";
|
|
|
|
import { CallStore } from "../../../../src/stores/CallStore";
|
2022-09-27 13:54:51 +02:00
|
|
|
import { Call, ConnectionState } from "../../../../src/models/Call";
|
|
|
|
|
|
|
|
const CallView = wrapInMatrixClientContext(_CallView);
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2023-08-16 10:29:34 +02:00
|
|
|
describe("CallView", () => {
|
2022-08-30 21:13:39 +02:00
|
|
|
useMockedCalls();
|
|
|
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
|
|
|
|
|
|
|
let client: Mocked<MatrixClient>;
|
|
|
|
let room: Room;
|
|
|
|
let alice: RoomMember;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-08-16 10:29:34 +02:00
|
|
|
useMockMediaDevices();
|
2022-08-30 21:13:39 +02:00
|
|
|
|
|
|
|
stubClient();
|
2023-06-05 19:12:23 +02:00
|
|
|
client = mocked(MatrixClientPeg.safeGet());
|
2022-08-30 21:13:39 +02:00
|
|
|
|
|
|
|
room = new Room("!1:example.org", client, "@alice:example.org", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
alice = mkRoomMember(room.roomId, "@alice:example.org");
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(room, "getMember").mockImplementation((userId) => (userId === alice.userId ? alice : null));
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
client.getRoom.mockImplementation((roomId) => (roomId === room.roomId ? room : null));
|
2022-08-30 21:13:39 +02:00
|
|
|
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 17:06:12 +01:00
|
|
|
const renderView = async (skipLobby = false): Promise<void> => {
|
|
|
|
render(<CallView room={room} resizing={false} waitForCall={false} skipLobby={skipLobby} />, {
|
|
|
|
wrapper: TooltipProvider,
|
|
|
|
});
|
2022-08-30 21:13:39 +02:00
|
|
|
await act(() => Promise.resolve()); // Let effects settle
|
|
|
|
};
|
|
|
|
|
2022-09-27 13:54:51 +02:00
|
|
|
describe("with an existing call", () => {
|
|
|
|
let call: MockedCall;
|
|
|
|
let widget: Widget;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
MockedCall.create(room, "1");
|
2022-10-07 04:27:28 +02:00
|
|
|
const maybeCall = CallStore.instance.getCall(room.roomId);
|
2022-09-27 13:54:51 +02: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 05:48:35 +02:00
|
|
|
/**
|
|
|
|
* TODO: Fix I do not understand this test
|
|
|
|
*/
|
|
|
|
it.skip("tracks participants", async () => {
|
2022-09-27 13:54:51 +02:00
|
|
|
const bob = mkRoomMember(room.roomId, "@bob:example.org");
|
|
|
|
const carol = mkRoomMember(room.roomId, "@carol:example.org");
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-09-27 13:54:51 +02:00
|
|
|
const expectAvatars = (userIds: string[]) => {
|
2023-07-07 12:54:43 +02:00
|
|
|
const avatars = screen.queryAllByRole("button", { name: "Profile picture" });
|
2022-09-27 13:54:51 +02:00
|
|
|
expect(userIds.length).toBe(avatars.length);
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-09-27 13:54:51 +02:00
|
|
|
for (const [userId, avatar] of zip(userIds, avatars)) {
|
|
|
|
fireEvent.focus(avatar!);
|
2022-11-28 22:37:32 +01:00
|
|
|
screen.getAllByRole("tooltip", { name: userId });
|
2022-09-27 13:54:51 +02:00
|
|
|
}
|
|
|
|
};
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-09-27 13:54:51 +02:00
|
|
|
await renderView();
|
|
|
|
expect(screen.queryByLabelText(/joined/)).toBe(null);
|
|
|
|
expectAvatars([]);
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
act(() => {
|
|
|
|
call.participants = new Map([[alice, new Set(["a"])]]);
|
|
|
|
});
|
2022-09-27 13:54:51 +02:00
|
|
|
screen.getByText("1 person joined");
|
|
|
|
expectAvatars([alice.userId]);
|
2022-08-30 21:13:39 +02:00
|
|
|
|
2022-11-28 22:37:32 +01: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 21:13:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
act(() => {
|
|
|
|
call.participants = new Map();
|
|
|
|
});
|
2022-09-27 13:54:51 +02:00
|
|
|
expect(screen.queryByLabelText(/joined/)).toBe(null);
|
|
|
|
expectAvatars([]);
|
|
|
|
});
|
|
|
|
|
2024-01-29 17:06:12 +01:00
|
|
|
it("automatically connects to the call when skipLobby is true", async () => {
|
|
|
|
const connectSpy = jest.spyOn(call, "start");
|
|
|
|
await renderView(true);
|
2022-09-27 13:54:51 +02: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 17:06:12 +01:00
|
|
|
await renderView(true);
|
2022-10-07 04:27:28 +02:00
|
|
|
await waitFor(() => expect(CallStore.instance.getCall(room.roomId)).not.toBeNull());
|
|
|
|
const call = CallStore.instance.getCall(room.roomId)!;
|
2022-09-27 13:54:51 +02: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 21:13:39 +02:00
|
|
|
});
|
|
|
|
});
|