Strictify voice-broadcast (#10393)

This commit is contained in:
Michael Weimann 2023-03-16 10:55:06 +01:00 committed by GitHub
parent 6d15b05b86
commit aae9dfbb7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 23 deletions

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -27,8 +27,8 @@ interface Props {
} }
export const VoiceBroadcastPlaybackControl: React.FC<Props> = ({ onClick, state }) => { export const VoiceBroadcastPlaybackControl: React.FC<Props> = ({ onClick, state }) => {
let controlIcon: ReactElement; let controlIcon: ReactElement | null = null;
let controlLabel: string; let controlLabel: string | null = null;
let className = ""; let className = "";
switch (state) { switch (state) {
@ -49,5 +49,11 @@ export const VoiceBroadcastPlaybackControl: React.FC<Props> = ({ onClick, state
break; break;
} }
return <VoiceBroadcastControl className={className} label={controlLabel} icon={controlIcon} onClick={onClick} />; if (controlIcon && controlLabel) {
return (
<VoiceBroadcastControl className={className} label={controlLabel} icon={controlIcon} onClick={onClick} />
);
}
return null;
}; };

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -48,6 +48,12 @@ export const useVoiceBroadcastPlayback = (
throw new Error(`Voice Broadcast room not found (event ${playback.infoEvent.getId()})`); throw new Error(`Voice Broadcast room not found (event ${playback.infoEvent.getId()})`);
} }
const sender = playback.infoEvent.sender;
if (!sender) {
throw new Error(`Voice Broadcast sender not found (event ${playback.infoEvent.getId()})`);
}
const playbackToggle = (): void => { const playbackToggle = (): void => {
playback.toggle(); playback.toggle();
}; };
@ -87,7 +93,7 @@ export const useVoiceBroadcastPlayback = (
liveness: liveness, liveness: liveness,
playbackState, playbackState,
room: room, room: room,
sender: playback.infoEvent.sender, sender,
toggle: playbackToggle, toggle: playbackToggle,
}; };
}; };

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -66,6 +66,12 @@ export const useVoiceBroadcastRecording = (
throw new Error("Unable to find voice broadcast room with Id: " + roomId); throw new Error("Unable to find voice broadcast room with Id: " + roomId);
} }
const sender = recording.infoEvent.sender;
if (!sender) {
throw new Error(`Voice Broadcast sender not found (event ${recording.infoEvent.getId()})`);
}
const stopRecording = async (): Promise<void> => { const stopRecording = async (): Promise<void> => {
const confirmed = await showStopBroadcastingDialog(); const confirmed = await showStopBroadcastingDialog();
@ -99,7 +105,7 @@ export const useVoiceBroadcastRecording = (
timeLeft, timeLeft,
recordingState, recordingState,
room, room,
sender: recording.infoEvent.sender, sender,
stopRecording, stopRecording,
toggleRecording: recording.toggle, toggleRecording: recording.toggle,
}; };

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -17,7 +17,12 @@ limitations under the License.
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter"; import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter";
import { VoiceBroadcastInfoState, VoiceBroadcastRecording, VoiceBroadcastRecordingEvent } from ".."; import {
VoiceBroadcastInfoState,
VoiceBroadcastRecording,
VoiceBroadcastRecordingEvent,
VoiceBroadcastRecordingState,
} from "..";
export enum VoiceBroadcastRecordingsStoreEvent { export enum VoiceBroadcastRecordingsStoreEvent {
CurrentChanged = "current_changed", CurrentChanged = "current_changed",
@ -85,7 +90,7 @@ export class VoiceBroadcastRecordingsStore extends TypedEventEmitter<VoiceBroadc
return recording; return recording;
} }
private onCurrentStateChanged = (state: VoiceBroadcastInfoState): void => { private onCurrentStateChanged = (state: VoiceBroadcastRecordingState): void => {
if (state === VoiceBroadcastInfoState.Stopped) { if (state === VoiceBroadcastInfoState.Stopped) {
this.clearCurrent(); this.clearCurrent();
} }

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -38,6 +38,12 @@ describe("<VoiceBroadcastPlaybackControl />", () => {
expect(renderControl(state).result.container).toMatchSnapshot(); expect(renderControl(state).result.container).toMatchSnapshot();
}); });
it("should not render for error state", () => {
expect(renderControl(VoiceBroadcastPlaybackState.Error).result.asFragment()).toMatchInlineSnapshot(
`<DocumentFragment />`,
);
});
describe("when clicking the control", () => { describe("when clicking the control", () => {
let onClick: () => void; let onClick: () => void;

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -233,7 +233,9 @@ describe("VoiceBroadcastPlaybackBody", () => {
describe.each([ describe.each([
[VoiceBroadcastPlaybackState.Paused, "not-live"], [VoiceBroadcastPlaybackState.Paused, "not-live"],
[VoiceBroadcastPlaybackState.Playing, "live"], [VoiceBroadcastPlaybackState.Playing, "live"],
])("when rendering a %s/%s broadcast", (state: VoiceBroadcastPlaybackState, liveness: VoiceBroadcastLiveness) => { ] satisfies [VoiceBroadcastPlaybackState, VoiceBroadcastLiveness][])(
"when rendering a %s/%s broadcast",
(state: VoiceBroadcastPlaybackState, liveness: VoiceBroadcastLiveness) => {
beforeEach(() => { beforeEach(() => {
mocked(playback.getState).mockReturnValue(state); mocked(playback.getState).mockReturnValue(state);
mocked(playback.getLiveness).mockReturnValue(liveness); mocked(playback.getLiveness).mockReturnValue(liveness);
@ -243,5 +245,13 @@ describe("VoiceBroadcastPlaybackBody", () => {
it("should render as expected", () => { it("should render as expected", () => {
expect(renderResult.container).toMatchSnapshot(); expect(renderResult.container).toMatchSnapshot();
}); });
},
);
it("when there is a broadcast without sender, it should raise an error", () => {
infoEvent.sender = null;
expect(() => {
render(<VoiceBroadcastPlaybackBody playback={playback} />);
}).toThrow(`Voice Broadcast sender not found (event ${playback.infoEvent.getId()})`);
}); });
}); });

View file

@ -1,5 +1,5 @@
/* /*
Copyright 2022 The Matrix.org Foundation C.I.C. Copyright 2022-2023 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -77,4 +77,11 @@ describe("VoiceBroadcastRecordingBody", () => {
expect(renderResult.container).toMatchSnapshot(); expect(renderResult.container).toMatchSnapshot();
}); });
}); });
it("when there is a broadcast without sender, it should raise an error", () => {
infoEvent.sender = null;
expect(() => {
render(<VoiceBroadcastRecordingBody recording={recording} />);
}).toThrow(`Voice Broadcast sender not found (event ${recording.infoEvent.getId()})`);
});
}); });