2022-09-26 13:29:38 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-09-26 13:29:38 +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-09-26 13:29:38 +00:00
|
|
|
*/
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
import React, { ReactElement } from "react";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { act, render, screen } from "jest-matrix-react";
|
2022-09-26 13:29:38 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2022-10-20 10:17:38 +00:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-09-26 13:29:38 +00:00
|
|
|
|
|
|
|
import {
|
2022-12-27 07:39:26 +00:00
|
|
|
VoiceBroadcastBody as UnwrappedVoiceBroadcastBody,
|
2022-09-26 13:29:38 +00:00
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingBody,
|
2022-09-28 08:22:50 +00:00
|
|
|
VoiceBroadcastRecording,
|
2022-10-14 04:13:17 +00:00
|
|
|
VoiceBroadcastPlaybackBody,
|
|
|
|
VoiceBroadcastPlayback,
|
2023-01-02 12:21:33 +00:00
|
|
|
VoiceBroadcastRecordingsStore,
|
2022-09-26 13:29:38 +00:00
|
|
|
} from "../../../src/voice-broadcast";
|
2023-06-14 12:42:07 +00:00
|
|
|
import { withClientContextRenderOptions, stubClient, wrapInSdkContext } from "../../test-utils";
|
2022-10-20 10:17:38 +00:00
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../utils/test-utils";
|
2022-11-07 14:19:49 +00:00
|
|
|
import { MediaEventHelper } from "../../../src/utils/MediaEventHelper";
|
|
|
|
import { RoomPermalinkCreator } from "../../../src/utils/permalinks/Permalinks";
|
2022-12-27 07:39:26 +00:00
|
|
|
import { SdkContextClass } from "../../../src/contexts/SDKContext";
|
2022-09-26 13:29:38 +00:00
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastRecordingBody", () => ({
|
|
|
|
VoiceBroadcastRecordingBody: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-10-14 04:13:17 +00:00
|
|
|
jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastPlaybackBody", () => ({
|
|
|
|
VoiceBroadcastPlaybackBody: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
jest.mock("../../../src/utils/permalinks/Permalinks");
|
|
|
|
jest.mock("../../../src/utils/MediaEventHelper");
|
2024-01-22 10:53:27 +00:00
|
|
|
jest.mock("../../../src/stores/WidgetStore");
|
|
|
|
jest.mock("../../../src/stores/widgets/WidgetLayoutStore");
|
2022-11-07 14:19:49 +00:00
|
|
|
|
2022-09-26 13:29:38 +00:00
|
|
|
describe("VoiceBroadcastBody", () => {
|
|
|
|
const roomId = "!room:example.com";
|
2022-11-07 14:19:49 +00:00
|
|
|
let userId: string;
|
|
|
|
let deviceId: string;
|
2022-09-26 13:29:38 +00:00
|
|
|
let client: MatrixClient;
|
2022-10-20 10:17:38 +00:00
|
|
|
let room: Room;
|
2022-09-28 08:22:50 +00:00
|
|
|
let infoEvent: MatrixEvent;
|
2022-10-20 10:17:38 +00:00
|
|
|
let stoppedEvent: MatrixEvent;
|
2022-10-13 12:02:53 +00:00
|
|
|
let testRecording: VoiceBroadcastRecording;
|
2022-10-14 04:13:17 +00:00
|
|
|
let testPlayback: VoiceBroadcastPlayback;
|
2022-09-26 13:29:38 +00:00
|
|
|
|
2022-09-28 08:22:50 +00:00
|
|
|
const renderVoiceBroadcast = () => {
|
2022-12-27 07:39:26 +00:00
|
|
|
const VoiceBroadcastBody = wrapInSdkContext(UnwrappedVoiceBroadcastBody, SdkContextClass.instance);
|
2022-10-13 12:02:53 +00:00
|
|
|
render(
|
|
|
|
<VoiceBroadcastBody
|
|
|
|
mxEvent={infoEvent}
|
2022-11-07 14:19:49 +00:00
|
|
|
mediaEventHelper={new MediaEventHelper(infoEvent)}
|
2022-10-13 12:02:53 +00:00
|
|
|
onHeightChanged={() => {}}
|
|
|
|
onMessageAllowed={() => {}}
|
2022-11-07 14:19:49 +00:00
|
|
|
permalinkCreator={new RoomPermalinkCreator(room)}
|
2022-10-13 12:02:53 +00:00
|
|
|
/>,
|
2023-06-14 12:42:07 +00:00
|
|
|
withClientContextRenderOptions(client),
|
2022-10-13 12:02:53 +00:00
|
|
|
);
|
2022-12-27 07:39:26 +00:00
|
|
|
testRecording = SdkContextClass.instance.voiceBroadcastRecordingsStore.getByInfoEvent(infoEvent, client);
|
2022-09-26 13:29:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-11-07 14:19:49 +00:00
|
|
|
userId = client.getUserId() || "";
|
|
|
|
deviceId = client.getDeviceId() || "";
|
|
|
|
mocked(client.relations).mockClear();
|
|
|
|
mocked(client.relations).mockResolvedValue({ events: [] });
|
|
|
|
room = new Room(roomId, client, userId);
|
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId?: string) => {
|
2022-10-20 10:17:38 +00:00
|
|
|
if (getRoomId === roomId) return room;
|
2022-11-07 14:19:49 +00:00
|
|
|
return null;
|
2022-10-20 10:17:38 +00:00
|
|
|
});
|
|
|
|
|
2022-10-21 07:30:02 +00:00
|
|
|
infoEvent = mkVoiceBroadcastInfoStateEvent(roomId, VoiceBroadcastInfoState.Started, userId, deviceId);
|
2022-10-20 10:17:38 +00:00
|
|
|
stoppedEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
roomId,
|
|
|
|
VoiceBroadcastInfoState.Stopped,
|
2022-11-07 14:19:49 +00:00
|
|
|
userId,
|
|
|
|
deviceId,
|
2022-10-20 10:17:38 +00:00
|
|
|
infoEvent,
|
|
|
|
);
|
|
|
|
room.addEventsToTimeline([infoEvent], true, room.getLiveTimeline());
|
2022-10-13 12:02:53 +00:00
|
|
|
testRecording = new VoiceBroadcastRecording(infoEvent, client);
|
2023-01-02 12:21:33 +00:00
|
|
|
testPlayback = new VoiceBroadcastPlayback(infoEvent, client, new VoiceBroadcastRecordingsStore());
|
2022-11-07 14:19:49 +00:00
|
|
|
mocked(VoiceBroadcastRecordingBody).mockImplementation(({ recording }): ReactElement | null => {
|
2022-10-13 12:02:53 +00:00
|
|
|
if (testRecording === recording) {
|
|
|
|
return <div data-testid="voice-broadcast-recording-body" />;
|
2022-09-28 08:22:50 +00:00
|
|
|
}
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
return null;
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
|
|
|
|
2022-11-07 14:19:49 +00:00
|
|
|
mocked(VoiceBroadcastPlaybackBody).mockImplementation(({ playback }): ReactElement | null => {
|
2022-10-14 04:13:17 +00:00
|
|
|
if (testPlayback === playback) {
|
|
|
|
return <div data-testid="voice-broadcast-playback-body" />;
|
|
|
|
}
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
return null;
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
2022-12-27 07:39:26 +00:00
|
|
|
jest.spyOn(SdkContextClass.instance.voiceBroadcastRecordingsStore, "getByInfoEvent").mockImplementation(
|
2022-11-07 14:19:49 +00:00
|
|
|
(getEvent: MatrixEvent, getClient: MatrixClient): VoiceBroadcastRecording => {
|
2022-10-13 12:02:53 +00:00
|
|
|
if (getEvent === infoEvent && getClient === client) {
|
|
|
|
return testRecording;
|
|
|
|
}
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
throw new Error("unexpected event");
|
2022-10-13 12:02:53 +00:00
|
|
|
},
|
|
|
|
);
|
2022-10-14 04:13:17 +00:00
|
|
|
|
2022-12-27 07:39:26 +00:00
|
|
|
jest.spyOn(SdkContextClass.instance.voiceBroadcastPlaybacksStore, "getByInfoEvent").mockImplementation(
|
2022-11-07 14:19:49 +00:00
|
|
|
(getEvent: MatrixEvent): VoiceBroadcastPlayback => {
|
2022-10-14 04:13:17 +00:00
|
|
|
if (getEvent === infoEvent) {
|
|
|
|
return testPlayback;
|
|
|
|
}
|
2022-11-07 14:19:49 +00:00
|
|
|
|
|
|
|
throw new Error("unexpected event");
|
2022-10-14 04:13:17 +00:00
|
|
|
},
|
|
|
|
);
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
|
|
|
|
2022-10-20 10:17:38 +00:00
|
|
|
describe("when there is a stopped voice broadcast", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
room.addEventsToTimeline([stoppedEvent], true, room.getLiveTimeline());
|
|
|
|
renderVoiceBroadcast();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a voice broadcast playback body", () => {
|
|
|
|
screen.getByTestId("voice-broadcast-playback-body");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when there is a started voice broadcast from the current user", () => {
|
2022-10-14 04:13:17 +00:00
|
|
|
beforeEach(() => {
|
2022-10-17 19:43:04 +00:00
|
|
|
renderVoiceBroadcast();
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
2022-10-13 12:02:53 +00:00
|
|
|
it("should render a voice broadcast recording body", () => {
|
|
|
|
screen.getByTestId("voice-broadcast-recording-body");
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
2022-10-17 19:43:04 +00:00
|
|
|
|
|
|
|
describe("and the recordings ends", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
act(() => {
|
2022-10-20 10:17:38 +00:00
|
|
|
room.addEventsToTimeline([stoppedEvent], true, room.getLiveTimeline());
|
2022-10-17 19:43:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a voice broadcast playback body", () => {
|
|
|
|
screen.getByTestId("voice-broadcast-playback-body");
|
|
|
|
});
|
|
|
|
});
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
2022-10-14 04:13:17 +00:00
|
|
|
|
|
|
|
describe("when displaying a voice broadcast playback", () => {
|
|
|
|
beforeEach(() => {
|
2022-10-17 19:43:04 +00:00
|
|
|
mocked(client).getUserId.mockReturnValue("@other:example.com");
|
|
|
|
renderVoiceBroadcast();
|
2022-10-14 04:13:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render a voice broadcast playback body", () => {
|
|
|
|
screen.getByTestId("voice-broadcast-playback-body");
|
|
|
|
});
|
|
|
|
});
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|