2022-09-26 15:29:38 +02: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-09-28 10:22:50 +02:00
|
|
|
import { render, screen } from "@testing-library/react";
|
2022-09-26 15:29:38 +02:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-09-28 10:22:50 +02:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-09-26 15:29:38 +02:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
|
|
|
import {
|
|
|
|
VoiceBroadcastBody,
|
|
|
|
VoiceBroadcastInfoEventType,
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecordingBody,
|
2022-09-28 10:22:50 +02:00
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
VoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastRecordingEvent,
|
2022-09-26 15:29:38 +02:00
|
|
|
} from "../../../src/voice-broadcast";
|
2022-09-28 10:22:50 +02:00
|
|
|
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";
|
2022-09-26 15:29:38 +02:00
|
|
|
import { IBodyProps } from "../../../src/components/views/messages/IBodyProps";
|
|
|
|
|
|
|
|
jest.mock("../../../src/voice-broadcast/components/molecules/VoiceBroadcastRecordingBody", () => ({
|
|
|
|
VoiceBroadcastRecordingBody: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("VoiceBroadcastBody", () => {
|
|
|
|
const roomId = "!room:example.com";
|
|
|
|
const recordingTestid = "voice-recording";
|
|
|
|
let client: MatrixClient;
|
2022-09-28 10:22:50 +02:00
|
|
|
let room: Room;
|
|
|
|
let infoEvent: MatrixEvent;
|
|
|
|
let recording: VoiceBroadcastRecording;
|
|
|
|
let onRecordingStateChanged: (state: VoiceBroadcastInfoState) => void;
|
2022-09-26 15:29:38 +02:00
|
|
|
|
|
|
|
const mkVoiceBroadcastInfoEvent = (state: VoiceBroadcastInfoState) => {
|
|
|
|
return mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: VoiceBroadcastInfoEventType,
|
|
|
|
user: client.getUserId(),
|
|
|
|
room: roomId,
|
|
|
|
content: {
|
|
|
|
state,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
const renderVoiceBroadcast = () => {
|
2022-09-26 15:29:38 +02:00
|
|
|
const props: IBodyProps = {
|
2022-09-28 10:22:50 +02:00
|
|
|
mxEvent: infoEvent,
|
2022-09-26 15:29:38 +02:00
|
|
|
} as unknown as IBodyProps;
|
2022-09-28 10:22:50 +02:00
|
|
|
render(<VoiceBroadcastBody {...props} />);
|
|
|
|
recording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(infoEvent, client);
|
|
|
|
recording.on(VoiceBroadcastRecordingEvent.StateChanged, onRecordingStateChanged);
|
2022-09-26 15:29:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldRenderALiveVoiceBroadcast = () => {
|
|
|
|
it("should render a live voice broadcast", () => {
|
|
|
|
expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
onClick: expect.any(Function),
|
|
|
|
live: true,
|
2022-09-28 10:22:50 +02:00
|
|
|
member: infoEvent.sender,
|
2022-09-26 15:29:38 +02:00
|
|
|
userId: client.getUserId(),
|
2022-09-28 10:22:50 +02:00
|
|
|
title: "@userId:matrix.org • test room",
|
2022-09-26 15:29:38 +02:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
2022-09-28 10:22:50 +02:00
|
|
|
screen.getByTestId(recordingTestid);
|
|
|
|
screen.getByText("Live");
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const itShouldRenderANonLiveVoiceBroadcast = () => {
|
|
|
|
it("should render a non-live voice broadcast", () => {
|
|
|
|
expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
onClick: expect.any(Function),
|
|
|
|
live: false,
|
2022-09-28 10:22:50 +02:00
|
|
|
member: infoEvent.sender,
|
2022-09-26 15:29:38 +02:00
|
|
|
userId: client.getUserId(),
|
2022-09-28 10:22:50 +02:00
|
|
|
title: "@userId:matrix.org • test room",
|
2022-09-26 15:29:38 +02:00
|
|
|
},
|
|
|
|
{},
|
|
|
|
);
|
2022-09-28 10:22:50 +02:00
|
|
|
expect(screen.getByTestId(recordingTestid)).not.toBeNull();
|
|
|
|
screen.getByTestId(recordingTestid);
|
|
|
|
expect(screen.queryByText("live")).toBeNull();
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mocked(VoiceBroadcastRecordingBody).mockImplementation(
|
|
|
|
({
|
|
|
|
live,
|
|
|
|
member: _member,
|
|
|
|
onClick,
|
|
|
|
title,
|
|
|
|
userId: _userId,
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
data-testid={recordingTestid}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
2022-09-28 10:22:50 +02:00
|
|
|
<div>{ title }</div>
|
|
|
|
<div>{ live && "Live" }</div>
|
2022-09-26 15:29:38 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
client = stubClient();
|
2022-09-28 10:22:50 +02:00
|
|
|
room = mkStubRoom(roomId, "test room", client);
|
|
|
|
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
|
|
|
|
if (getRoomId === roomId) {
|
|
|
|
return room;
|
|
|
|
}
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
2022-09-28 10:22:50 +02:00
|
|
|
infoEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started);
|
|
|
|
onRecordingStateChanged = jest.fn();
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
afterEach(() => {
|
|
|
|
if (recording && onRecordingStateChanged) {
|
|
|
|
recording.off(VoiceBroadcastRecordingEvent.StateChanged, onRecordingStateChanged);
|
|
|
|
}
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
describe("when there is a Started Voice Broadcast info event", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
renderVoiceBroadcast();
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
itShouldRenderALiveVoiceBroadcast();
|
2022-09-26 15:29:38 +02:00
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
describe("and it is clicked", () => {
|
2022-09-26 15:29:38 +02:00
|
|
|
beforeEach(async () => {
|
2022-09-28 10:22:50 +02:00
|
|
|
mocked(VoiceBroadcastRecordingBody).mockClear();
|
|
|
|
mocked(onRecordingStateChanged).mockClear();
|
|
|
|
await userEvent.click(screen.getByTestId(recordingTestid));
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
|
2022-09-28 10:22:50 +02:00
|
|
|
itShouldRenderANonLiveVoiceBroadcast();
|
|
|
|
|
|
|
|
it("should call stop on the recording", () => {
|
|
|
|
expect(recording.state).toBe(VoiceBroadcastInfoState.Stopped);
|
|
|
|
expect(onRecordingStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Stopped);
|
2022-09-26 15:29:38 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|