=> {
export const useVoiceBroadcastRecording = (recording: VoiceBroadcastRecording) => {
const client = MatrixClientPeg.get();
- const room = client.getRoom(recording.infoEvent.getRoomId());
+ const roomId = recording.infoEvent.getRoomId();
+ const room = client.getRoom(roomId);
+
+ if (!room) {
+ throw new Error("Unable to find voice broadcast room with Id: " + roomId);
+ }
+
const stopRecording = async () => {
const confirmed = await showStopBroadcastingDialog();
diff --git a/test/voice-broadcast/components/molecules/VoiceBroadcastPreRecordingPip-test.tsx b/test/voice-broadcast/components/molecules/VoiceBroadcastPreRecordingPip-test.tsx
new file mode 100644
index 0000000000..91658f26ed
--- /dev/null
+++ b/test/voice-broadcast/components/molecules/VoiceBroadcastPreRecordingPip-test.tsx
@@ -0,0 +1,138 @@
+/*
+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 { mocked } from "jest-mock";
+import { MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
+import { act, render, RenderResult, screen } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+
+import {
+ 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";
+
+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 room avatar: { room.name }
;
+ }),
+}));
+
+describe("VoiceBroadcastPreRecordingPip", () => {
+ let renderResult: RenderResult;
+ let preRecording: VoiceBroadcastPreRecording;
+ let recordingsStore: VoiceBroadcastRecordingsStore;
+ let client: MatrixClient;
+ let room: Room;
+ let sender: RoomMember;
+
+ beforeEach(() => {
+ client = stubClient();
+ room = new Room("!room@example.com", client, client.getUserId() || "");
+ sender = new RoomMember(room.roomId, client.getUserId() || "");
+ recordingsStore = new VoiceBroadcastRecordingsStore();
+ mocked(requestMediaPermissions).mockReturnValue(new Promise((r) => {
+ r({
+ getTracks: () => [],
+ } as unknown as MediaStream);
+ }));
+ 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,
+ recordingsStore,
+ );
+ });
+
+ afterAll(() => {
+ jest.resetAllMocks();
+ });
+
+ describe("when rendered", () => {
+ beforeEach(async () => {
+ renderResult = render();
+
+ await act(async () => {
+ flushPromises();
+ });
+ });
+
+ it("should match the snapshot", () => {
+ expect(renderResult.container).toMatchSnapshot();
+ });
+
+ 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();
+ });
+ });
+ });
+ });
+});
diff --git a/test/voice-broadcast/components/molecules/VoiceBroadcastRecordingPip-test.tsx b/test/voice-broadcast/components/molecules/VoiceBroadcastRecordingPip-test.tsx
index 6a0c1016ff..5aac28fbeb 100644
--- a/test/voice-broadcast/components/molecules/VoiceBroadcastRecordingPip-test.tsx
+++ b/test/voice-broadcast/components/molecules/VoiceBroadcastRecordingPip-test.tsx
@@ -16,18 +16,23 @@ limitations under the License.
//
import React from "react";
-import { render, RenderResult, screen } from "@testing-library/react";
+import { act, render, RenderResult, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { sleep } from "matrix-js-sdk/src/utils";
+import { mocked } from "jest-mock";
import {
VoiceBroadcastInfoState,
VoiceBroadcastRecording,
VoiceBroadcastRecordingPip,
} from "../../../../src/voice-broadcast";
-import { stubClient } from "../../../test-utils";
+import { filterConsole, flushPromises, stubClient } from "../../../test-utils";
import { mkVoiceBroadcastInfoStateEvent } from "../../utils/test-utils";
+import { requestMediaPermissions } from "../../../../src/utils/media/requestMediaPermissions";
+import MediaDeviceHandler, { MediaDeviceKindEnum } from "../../../../src/MediaDeviceHandler";
+
+jest.mock("../../../../src/utils/media/requestMediaPermissions");
// mock RoomAvatar, because it is doing too much fancy stuff
jest.mock("../../../../src/components/views/avatars/RoomAvatar", () => ({
@@ -54,31 +59,80 @@ describe("VoiceBroadcastRecordingPip", () => {
let infoEvent: MatrixEvent;
let recording: VoiceBroadcastRecording;
let renderResult: RenderResult;
+ let restoreConsole: () => void;
- const renderPip = (state: VoiceBroadcastInfoState) => {
+ const renderPip = async (state: VoiceBroadcastInfoState) => {
infoEvent = mkVoiceBroadcastInfoStateEvent(
roomId,
state,
- client.getUserId(),
- client.getDeviceId(),
+ client.getUserId() || "",
+ client.getDeviceId() || "",
);
recording = new VoiceBroadcastRecording(infoEvent, client, state);
+ jest.spyOn(recording, "pause");
+ jest.spyOn(recording, "resume");
renderResult = render();
+ await act(async () => {
+ flushPromises();
+ });
};
beforeAll(() => {
client = stubClient();
+ mocked(requestMediaPermissions).mockReturnValue(new Promise((r) => {
+ r({
+ getTracks: () => [],
+ } as unknown as MediaStream);
+ }));
+ 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();
+ restoreConsole = filterConsole("Starting load of AsyncWrapper for modal");
+ });
+
+ afterAll(() => {
+ restoreConsole();
});
describe("when rendering a started recording", () => {
- beforeEach(() => {
- renderPip(VoiceBroadcastInfoState.Started);
+ beforeEach(async () => {
+ await renderPip(VoiceBroadcastInfoState.Started);
});
it("should render as expected", () => {
expect(renderResult.container).toMatchSnapshot();
});
+ describe("and selecting another input device", () => {
+ beforeEach(async () => {
+ await act(async () => {
+ await userEvent.click(screen.getByLabelText("Change input device"));
+ await userEvent.click(screen.getByText("Device 1"));
+ });
+ });
+
+ it("should select the device and pause and resume the broadcast", () => {
+ expect(MediaDeviceHandler.instance.setDevice).toHaveBeenCalledWith(
+ "d1",
+ MediaDeviceKindEnum.AudioInput,
+ );
+ expect(recording.pause).toHaveBeenCalled();
+ expect(recording.resume).toHaveBeenCalled();
+ });
+ });
+
describe("and clicking the pause button", () => {
beforeEach(async () => {
await userEvent.click(screen.getByLabelText("pause voice broadcast"));
@@ -113,8 +167,8 @@ describe("VoiceBroadcastRecordingPip", () => {
});
describe("when rendering a paused recording", () => {
- beforeEach(() => {
- renderPip(VoiceBroadcastInfoState.Paused);
+ beforeEach(async () => {
+ await renderPip(VoiceBroadcastInfoState.Paused);
});
it("should render as expected", () => {
diff --git a/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastPreRecordingPip-test.tsx.snap b/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastPreRecordingPip-test.tsx.snap
new file mode 100644
index 0000000000..758d2c6371
--- /dev/null
+++ b/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastPreRecordingPip-test.tsx.snap
@@ -0,0 +1,58 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`VoiceBroadcastPreRecordingPip when rendered should match the snapshot 1`] = `
+
+`;
diff --git a/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastRecordingPip-test.tsx.snap b/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastRecordingPip-test.tsx.snap
index 00166f5bcc..72c345fb54 100644
--- a/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastRecordingPip-test.tsx.snap
+++ b/test/voice-broadcast/components/molecules/__snapshots__/VoiceBroadcastRecordingPip-test.tsx.snap
@@ -60,6 +60,16 @@ exports[`VoiceBroadcastRecordingPip when rendering a paused recording should ren
class="mx_Icon mx_Icon_16"
/>
+
+