2022-11-28 22:16:44 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-11-28 22:16:44 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08: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 22:16:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { act, render, RenderResult, screen } from "jest-matrix-react";
|
2022-11-28 22:16:44 +08:00
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
|
|
|
|
import {
|
2022-11-30 18:16:22 +08:00
|
|
|
VoiceBroadcastPlaybacksStore,
|
2022-11-28 22:16:44 +08:00
|
|
|
VoiceBroadcastPreRecording,
|
|
|
|
VoiceBroadcastPreRecordingPip,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
2024-10-15 21:57:26 +08:00
|
|
|
} from "../../../../../src/voice-broadcast";
|
|
|
|
import { flushPromises, stubClient } from "../../../../test-utils";
|
|
|
|
import { requestMediaPermissions } from "../../../../../src/utils/media/requestMediaPermissions";
|
|
|
|
import MediaDeviceHandler, { MediaDeviceKindEnum } from "../../../../../src/MediaDeviceHandler";
|
|
|
|
import dis from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
import { Action } from "../../../../../src/dispatcher/actions";
|
2022-11-28 22:16:44 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../../src/dispatcher/dispatcher");
|
|
|
|
jest.mock("../../../../../src/utils/media/requestMediaPermissions");
|
2022-11-28 22:16:44 +08:00
|
|
|
|
|
|
|
// mock RoomAvatar, because it is doing too much fancy stuff
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../../src/components/views/avatars/RoomAvatar", () => ({
|
2022-11-28 22:16:44 +08:00
|
|
|
__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 18:16:22 +08:00
|
|
|
let playbacksStore: VoiceBroadcastPlaybacksStore;
|
2022-11-28 22:16:44 +08:00
|
|
|
let recordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
let client: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
let sender: RoomMember;
|
|
|
|
|
2022-12-13 20:32:19 +08: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 22:16:44 +08: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 20:21:33 +08:00
|
|
|
playbacksStore = new VoiceBroadcastPlaybacksStore(recordingsStore);
|
2022-12-13 20:32:19 +08:00
|
|
|
mocked(requestMediaPermissions).mockResolvedValue({
|
|
|
|
getTracks: (): Array<MediaStreamTrack> => [],
|
|
|
|
} as unknown as MediaStream);
|
2022-11-28 22:16:44 +08: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-15 01:08:42 +08:00
|
|
|
jest.spyOn(preRecording, "start").mockResolvedValue();
|
2022-11-28 22:16:44 +08: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-15 01:08:42 +08:00
|
|
|
describe("and double clicking »Go live«", () => {
|
|
|
|
beforeEach(async () => {
|
2024-08-07 01:22:02 +08:00
|
|
|
await userEvent.click(screen.getByText("Go live"));
|
|
|
|
await userEvent.click(screen.getByText("Go live"));
|
2022-12-15 01:08:42 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call start once", () => {
|
|
|
|
expect(preRecording.start).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-13 20:32:19 +08: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 22:16:44 +08: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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|