2022-11-28 14:16:44 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-28 14:16:44 +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-11-28 14:16:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { act, render, RenderResult, screen } from "jest-matrix-react";
|
2022-11-28 14:16:44 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
|
|
|
|
import {
|
2022-11-30 10:16:22 +00:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
2022-11-28 14:16:44 +00:00
|
|
|
VoiceBroadcastPreRecording,
|
|
|
|
VoiceBroadcastPreRecordingPip,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
} from "../../../../src/voice-broadcast";
|
|
|
|
import { flushPromises, stubClient } from "../../../test-utils";
|
|
|
|
import { requestMediaPermissions } from "../../../../src/utils/media/requestMediaPermissions";
|
|
|
|
import MediaDeviceHandler, { MediaDeviceKindEnum } from "../../../../src/MediaDeviceHandler";
|
2022-12-13 12:32:19 +00:00
|
|
|
import dis from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../src/dispatcher/actions";
|
2022-11-28 14:16:44 +00:00
|
|
|
|
2022-12-13 12:32:19 +00:00
|
|
|
jest.mock("../../../../src/dispatcher/dispatcher");
|
2022-11-28 14:16:44 +00:00
|
|
|
jest.mock("../../../../src/utils/media/requestMediaPermissions");
|
|
|
|
|
|
|
|
// mock RoomAvatar, because it is doing too much fancy stuff
|
|
|
|
jest.mock("../../../../src/components/views/avatars/RoomAvatar", () => ({
|
|
|
|
__esModule: true,
|
|
|
|
default: jest.fn().mockImplementation(({ room }) => {
|
|
|
|
return <div data-testid="room-avatar">room avatar: {room.name}</div>;
|
|
|
|
}),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("VoiceBroadcastPreRecordingPip", () => {
|
|
|
|
let renderResult: RenderResult;
|
|
|
|
let preRecording: VoiceBroadcastPreRecording;
|
2022-11-30 10:16:22 +00:00
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-11-28 14:16:44 +00:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let sender: RoomMember;
|
|
|
|
|
2022-12-13 12:32:19 +00:00
|
|
|
const itShouldShowTheBroadcastRoom = () => {
|
|
|
|
it("should show the broadcast room", () => {
|
|
|
|
expect(dis.dispatch).toHaveBeenCalledWith({
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
room_id: room.roomId,
|
|
|
|
metricsTrigger: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-28 14:16:44 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
room = new Room("!room@example.com", client, client.getUserId() || "");
|
|
|
|
sender = new RoomMember(room.roomId, client.getUserId() || "");
|
|
|
|
recordingsStore = new VoiceBroadcastRecordingsStore();
|
2023-01-02 12:21:33 +00:00
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore(recordingsStore);
|
2022-12-13 12:32:19 +00:00
|
|
|
mocked(requestMediaPermissions).mockResolvedValue({
|
|
|
|
getTracks: (): Array<MediaStreamTrack> => [],
|
|
|
|
} as unknown as MediaStream);
|
2022-11-28 14:16:44 +00:00
|
|
|
jest.spyOn(MediaDeviceHandler, "getDevices").mockResolvedValue({
|
|
|
|
[MediaDeviceKindEnum.AudioInput]: [
|
|
|
|
{
|
|
|
|
deviceId: "d1",
|
|
|
|
label: "Device 1",
|
|
|
|
} as MediaDeviceInfo,
|
|
|
|
{
|
|
|
|
deviceId: "d2",
|
|
|
|
label: "Device 2",
|
|
|
|
} as MediaDeviceInfo,
|
|
|
|
],
|
|
|
|
[MediaDeviceKindEnum.AudioOutput]: [],
|
|
|
|
[MediaDeviceKindEnum.VideoInput]: [],
|
|
|
|
});
|
|
|
|
jest.spyOn(MediaDeviceHandler.instance, "setDevice").mockImplementation();
|
|
|
|
preRecording = new VoiceBroadcastPreRecording(room, sender, client, playbacksStore, recordingsStore);
|
2022-12-14 17:08:42 +00:00
|
|
|
jest.spyOn(preRecording, "start").mockResolvedValue();
|
2022-11-28 14:16:44 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when rendered", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
renderResult = render(<VoiceBroadcastPreRecordingPip voiceBroadcastPreRecording={preRecording} />);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
flushPromises();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should match the snapshot", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-14 17:08:42 +00:00
|
|
|
describe("and double clicking »Go live«", () => {
|
|
|
|
beforeEach(async () => {
|
2024-08-06 17:22:02 +00:00
|
|
|
await userEvent.click(screen.getByText("Go live"));
|
|
|
|
await userEvent.click(screen.getByText("Go live"));
|
2022-12-14 17:08:42 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call start once", () => {
|
|
|
|
expect(preRecording.start).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-13 12:32:19 +00:00
|
|
|
describe("and clicking the room name", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await userEvent.click(screen.getByText(room.name));
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldShowTheBroadcastRoom();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and clicking the room avatar", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await userEvent.click(screen.getByText(`room avatar: ${room.name}`));
|
|
|
|
});
|
|
|
|
|
|
|
|
itShouldShowTheBroadcastRoom();
|
|
|
|
});
|
|
|
|
|
2022-11-28 14:16:44 +00:00
|
|
|
describe("and clicking the device label", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await act(async () => {
|
|
|
|
await userEvent.click(screen.getByText("Default Device"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should display the device selection", () => {
|
|
|
|
expect(screen.queryAllByText("Default Device").length).toBe(2);
|
|
|
|
expect(screen.queryByText("Device 1")).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText("Device 2")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and selecting a device", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
await act(async () => {
|
|
|
|
await userEvent.click(screen.getByText("Device 1"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set it as current device", () => {
|
|
|
|
expect(MediaDeviceHandler.instance.setDevice).toHaveBeenCalledWith(
|
|
|
|
"d1",
|
|
|
|
MediaDeviceKindEnum.AudioInput,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not show the device selection", () => {
|
|
|
|
expect(screen.queryByText("Default Device")).not.toBeInTheDocument();
|
|
|
|
// expected to be one in the document, displayed in the pip directly
|
|
|
|
expect(screen.queryByText("Device 1")).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText("Device 2")).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|