Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Extract useVoiceBroadcastRecording hook
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Oct 13, 2022
1 parent 1800cb8 commit c542421
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 180 deletions.
26 changes: 2 additions & 24 deletions src/voice-broadcast/components/VoiceBroadcastBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import React, { useState } from "react";
import React from "react";

import {
VoiceBroadcastInfoState,
VoiceBroadcastRecordingBody,
VoiceBroadcastRecordingsStore,
VoiceBroadcastRecording,
VoiceBroadcastRecordingEvent,
} from "..";
import { IBodyProps } from "../../components/views/messages/IBodyProps";
import { MatrixClientPeg } from "../../MatrixClientPeg";
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";

export const VoiceBroadcastBody: React.FC<IBodyProps> = ({ mxEvent }) => {
const client = MatrixClientPeg.get();
const room = client.getRoom(mxEvent.getRoomId());
const recording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(mxEvent, client);
const [recordingState, setRecordingState] = useState(recording.getState());

useTypedEventEmitter(
recording,
VoiceBroadcastRecordingEvent.StateChanged,
(state: VoiceBroadcastInfoState, _recording: VoiceBroadcastRecording) => {
setRecordingState(state);
},
);

const stopVoiceBroadcast = () => {
if (recordingState !== VoiceBroadcastInfoState.Started) return;
recording.stop();
};

return <VoiceBroadcastRecordingBody
onClick={stopVoiceBroadcast}
live={recordingState === VoiceBroadcastInfoState.Started}
sender={mxEvent.sender}
roomName={room.name}
recording={recording}
/>;
};
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
/*
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, { MouseEventHandler } from "react";
import { RoomMember } from "matrix-js-sdk/src/matrix";
import React from "react";

import { VoiceBroadcastHeader } from "../..";
import { VoiceBroadcastRecording } from "../..";
import { useVoiceBroadcastRecording } from "../../hooks/useVoiceBroadcastRecording";
import { VoiceBroadcastHeader } from "../atoms/VoiceBroadcastHeader";

interface VoiceBroadcastRecordingBodyProps {
live: boolean;
onClick: MouseEventHandler<HTMLDivElement>;
roomName: string;
sender: RoomMember;
recording: VoiceBroadcastRecording;
}

export const VoiceBroadcastRecordingBody: React.FC<VoiceBroadcastRecordingBodyProps> = ({
live,
onClick,
roomName,
sender,
}) => {
export const VoiceBroadcastRecordingBody: React.FC<VoiceBroadcastRecordingBodyProps> = ({ recording }) => {
const {
live,
roomName,
sender,
stopRecording,
} = useVoiceBroadcastRecording(recording);

return (
<div
className="mx_VoiceBroadcastRecordingBody"
onClick={onClick}
onClick={stopRecording}
>
<VoiceBroadcastHeader
live={live}
Expand Down
51 changes: 51 additions & 0 deletions src/voice-broadcast/hooks/useVoiceBroadcastRecording.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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 { useState } from "react";

import {
VoiceBroadcastInfoState,
VoiceBroadcastRecording,
VoiceBroadcastRecordingEvent,
VoiceBroadcastRecordingsStore,
} from "..";
import { useTypedEventEmitter } from "../../hooks/useEventEmitter";
import { MatrixClientPeg } from "../../MatrixClientPeg";

export const useVoiceBroadcastRecording = (recording: VoiceBroadcastRecording) => {
const client = MatrixClientPeg.get();
const room = client.getRoom(recording.infoEvent.getRoomId());
const stopRecording = () => {
recording.stop();
VoiceBroadcastRecordingsStore.instance().clearCurrent();
};

const [live, setLive] = useState(recording.getState() === VoiceBroadcastInfoState.Started);
useTypedEventEmitter(
recording,
VoiceBroadcastRecordingEvent.StateChanged,
(state: VoiceBroadcastInfoState, _recording: VoiceBroadcastRecording) => {
setLive(state === VoiceBroadcastInfoState.Started);
},
);

return {
live,
roomName: room.name,
sender: recording.infoEvent.sender,
stopRecording,
};
};
7 changes: 7 additions & 0 deletions src/voice-broadcast/stores/VoiceBroadcastRecordingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export class VoiceBroadcastRecordingsStore extends TypedEventEmitter<VoiceBroadc
return this.current;
}

public clearCurrent(): void {
if (this.current === null) return;

this.current = null;
this.emit(VoiceBroadcastRecordingsStoreEvent.CurrentChanged, null);
}

public getByInfoEvent(infoEvent: MatrixEvent, client: MatrixClient): VoiceBroadcastRecording {
const infoEventId = infoEvent.getId();

Expand Down
123 changes: 26 additions & 97 deletions test/voice-broadcast/components/VoiceBroadcastBody-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ limitations under the License.

import React from "react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";

import {
VoiceBroadcastBody,
Expand All @@ -27,23 +26,18 @@ import {
VoiceBroadcastRecordingBody,
VoiceBroadcastRecordingsStore,
VoiceBroadcastRecording,
VoiceBroadcastRecordingEvent,
} from "../../../src/voice-broadcast";
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";
import { IBodyProps } from "../../../src/components/views/messages/IBodyProps";
import { mkEvent, stubClient } from "../../test-utils";

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;
let room: Room;
let infoEvent: MatrixEvent;
let recording: VoiceBroadcastRecording;
let onRecordingStateChanged: (state: VoiceBroadcastInfoState) => void;
let testRecording: VoiceBroadcastRecording;

const mkVoiceBroadcastInfoEvent = (state: VoiceBroadcastInfoState) => {
return mkEvent({
Expand All @@ -58,104 +52,39 @@ describe("VoiceBroadcastBody", () => {
};

const renderVoiceBroadcast = () => {
const props: IBodyProps = {
mxEvent: infoEvent,
} as unknown as IBodyProps;
render(<VoiceBroadcastBody {...props} />);
recording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(infoEvent, client);
recording.on(VoiceBroadcastRecordingEvent.StateChanged, onRecordingStateChanged);
};

const itShouldRenderALiveVoiceBroadcast = () => {
it("should render a live voice broadcast", () => {
expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
{
onClick: expect.any(Function),
live: true,
sender: infoEvent.sender,
roomName: room.name,
},
{},
);
screen.getByTestId(recordingTestid);
screen.getByText("Live");
});
};

const itShouldRenderANonLiveVoiceBroadcast = () => {
it("should render a non-live voice broadcast", () => {
expect(VoiceBroadcastRecordingBody).toHaveBeenCalledWith(
{
onClick: expect.any(Function),
live: false,
sender: infoEvent.sender,
roomName: room.name,
},
{},
);
expect(screen.getByTestId(recordingTestid)).not.toBeNull();
screen.getByTestId(recordingTestid);
expect(screen.queryByText("live")).toBeNull();
});
render(<VoiceBroadcastBody
mxEvent={infoEvent}
mediaEventHelper={null}
onHeightChanged={() => {}}
onMessageAllowed={() => {}}
permalinkCreator={null}
/>);
testRecording = VoiceBroadcastRecordingsStore.instance().getByInfoEvent(infoEvent, client);
};

beforeEach(() => {
mocked(VoiceBroadcastRecordingBody).mockImplementation(
({
live,
sender,
onClick,
roomName,
}) => {
return (
<div
data-testid={recordingTestid}
onClick={onClick}
>
<div>{ sender.name }</div>
<div>{ roomName }</div>
<div>{ live && "Live" }</div>
</div>
);
},
);
client = stubClient();
room = mkStubRoom(roomId, "test room", client);
mocked(client.getRoom).mockImplementation((getRoomId: string) => {
if (getRoomId === roomId) {
return room;
infoEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started);
testRecording = new VoiceBroadcastRecording(infoEvent, client);
mocked(VoiceBroadcastRecordingBody).mockImplementation(({ recording }) => {
if (testRecording === recording) {
return <div data-testid="voice-broadcast-recording-body" />;
}
});
infoEvent = mkVoiceBroadcastInfoEvent(VoiceBroadcastInfoState.Started);
onRecordingStateChanged = jest.fn();
});

afterEach(() => {
if (recording && onRecordingStateChanged) {
recording.off(VoiceBroadcastRecordingEvent.StateChanged, onRecordingStateChanged);
}
jest.spyOn(VoiceBroadcastRecordingsStore.instance(), "getByInfoEvent").mockImplementation(
(getEvent: MatrixEvent, getClient: MatrixClient) => {
if (getEvent === infoEvent && getClient === client) {
return testRecording;
}
},
);
});

describe("when there is a Started Voice Broadcast info event", () => {
beforeEach(() => {
describe("when rendering a voice broadcast", () => {
it("should render a voice broadcast recording body", () => {
renderVoiceBroadcast();
});

itShouldRenderALiveVoiceBroadcast();

describe("and it is clicked", () => {
beforeEach(async () => {
mocked(VoiceBroadcastRecordingBody).mockClear();
mocked(onRecordingStateChanged).mockClear();
await userEvent.click(screen.getByTestId(recordingTestid));
});

itShouldRenderANonLiveVoiceBroadcast();

it("should call stop on the recording", () => {
expect(recording.getState()).toBe(VoiceBroadcastInfoState.Stopped);
expect(onRecordingStateChanged).toHaveBeenCalledWith(VoiceBroadcastInfoState.Stopped);
});
screen.getByTestId("voice-broadcast-recording-body");
});
});
});
Loading

0 comments on commit c542421

Please sign in to comment.