2022-09-26 13:29:38 +00: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";
|
2022-10-17 19:43:04 +00:00
|
|
|
import { act, render, screen } from "@testing-library/react";
|
2022-09-26 13:29:38 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2022-10-13 12:02:53 +00:00
|
|
|
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-09-26 13:29:38 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
VoiceBroadcastBody,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingBody,
|
2022-09-28 08:22:50 +00:00
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecording,
|
2022-10-14 04:13:17 +00:00
|
|
|
VoiceBroadcastPlaybackBody,
|
|
|
|
VoiceBroadcastPlayback,
|
|
|
|
VoiceBroadcastPlaybacksStore,
|
2022-09-26 13:29:38 +00:00
|
|
|
} from "../../../src/voice-broadcast";
|
2022-10-13 12:02:53 +00:00
|
|
|
import { mkEvent, stubClient } from "../../test-utils";
|
2022-10-17 19:43:04 +00:00
|
|
|
import { RelationsHelper } from "../../../src/events/RelationsHelper";
|
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-10-17 19:43:04 +00:00
|
|
|
jest.mock("../../../src/events/RelationsHelper");
|
2022-10-14 04:13:17 +00:00
|
|
|
|
2022-09-26 13:29:38 +00:00
|
|
|
describe("VoiceBroadcastBody", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
let client: MatrixClient;
|
2022-09-28 08:22:50 +00:00
|
|
|
let infoEvent: 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
|
|
|
|
|
|
|
const mkVoiceBroadcastInfoEvent = (state: VoiceBroadcastInfoState) => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
|
|
|
user: client.getUserId(),
|
|
|
|
room: roomId,
|
|
|
|
content: {
|
|
|
|
state,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-28 08:22:50 +00:00
|
|
|
const renderVoiceBroadcast = () => {
|
2022-10-13 12:02:53 +00:00
|
|
|
render(<VoiceBroadcastBody
|
|
|
|
mxEvent={infoEvent}
|
|
|
|
mediaEventHelper={null}
|
|
|
|
onHeightChanged={() => {}}
|
|
|
|
onMessageAllowed={() => {}}
|
|
|
|
permalinkCreator={null}
|
|
|
|
/>);
|
|
|
|
testRecording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(infoEvent, client);
|
2022-09-26 13:29:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
2022-10-13 12:02:53 +00:00
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started);
|
|
|
|
testRecording = new VoiceBroadcastRecording(infoEvent, client);
|
2022-10-14 14:48:54 +00:00
|
|
|
testPlayback = new VoiceBroadcastPlayback(infoEvent, client);
|
2022-10-13 12:02:53 +00:00
|
|
|
mocked(VoiceBroadcastRecordingBody).mockImplementation(({ recording }) => {
|
|
|
|
if (testRecording === recording) {
|
|
|
|
return <div data-testid="voice-broadcast-recording-body" />;
|
2022-09-28 08:22:50 +00:00
|
|
|
}
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
|
|
|
|
2022-10-14 04:13:17 +00:00
|
|
|
mocked(VoiceBroadcastPlaybackBody).mockImplementation(({ playback }) => {
|
|
|
|
if (testPlayback === playback) {
|
|
|
|
return <div data-testid="voice-broadcast-playback-body" />;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-13 12:02:53 +00:00
|
|
|
jest.spyOn(VoiceBroadcastRecordingsStore.instance(), "getByInfoEvent").mockImplementation(
|
|
|
|
(getEvent: MatrixEvent, getClient: MatrixClient) => {
|
|
|
|
if (getEvent === infoEvent && getClient === client) {
|
|
|
|
return testRecording;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-10-14 04:13:17 +00:00
|
|
|
|
|
|
|
jest.spyOn(VoiceBroadcastPlaybacksStore.instance(), "getByInfoEvent").mockImplementation(
|
|
|
|
(getEvent: MatrixEvent) => {
|
|
|
|
if (getEvent === infoEvent) {
|
|
|
|
return testPlayback;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-09-26 13:29:38 +00:00
|
|
|
});
|
|
|
|
|
2022-10-14 04:13:17 +00:00
|
|
|
describe("when displaying a voice broadcast recording", () => {
|
|
|
|
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(() => {
|
|
|
|
const stoppedEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Stopped);
|
|
|
|
// get the RelationsHelper instanced used in VoiceBroadcastBody
|
|
|
|
const relationsHelper = mocked(RelationsHelper).mock.instances[5];
|
|
|
|
act(() => {
|
|
|
|
// invoke the callback of the VoiceBroadcastBody hook to simulate an ended broadcast
|
|
|
|
// @ts-ignore
|
|
|
|
mocked(relationsHelper.on).mock.calls[0][1](stoppedEvent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
});
|