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

Commit aae9dfb

Browse files
authored
Strictify voice-broadcast (#10393)
1 parent 6d15b05 commit aae9dfb

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

src/voice-broadcast/components/atoms/VoiceBroadcastPlaybackControl.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -27,8 +27,8 @@ interface Props {
2727
}
2828

2929
export const VoiceBroadcastPlaybackControl: React.FC<Props> = ({ onClick, state }) => {
30-
let controlIcon: ReactElement;
31-
let controlLabel: string;
30+
let controlIcon: ReactElement | null = null;
31+
let controlLabel: string | null = null;
3232
let className = "";
3333

3434
switch (state) {
@@ -49,5 +49,11 @@ export const VoiceBroadcastPlaybackControl: React.FC<Props> = ({ onClick, state
4949
break;
5050
}
5151

52-
return <VoiceBroadcastControl className={className} label={controlLabel} icon={controlIcon} onClick={onClick} />;
52+
if (controlIcon && controlLabel) {
53+
return (
54+
<VoiceBroadcastControl className={className} label={controlLabel} icon={controlIcon} onClick={onClick} />
55+
);
56+
}
57+
58+
return null;
5359
};

src/voice-broadcast/hooks/useVoiceBroadcastPlayback.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -48,6 +48,12 @@ export const useVoiceBroadcastPlayback = (
4848
throw new Error(`Voice Broadcast room not found (event ${playback.infoEvent.getId()})`);
4949
}
5050

51+
const sender = playback.infoEvent.sender;
52+
53+
if (!sender) {
54+
throw new Error(`Voice Broadcast sender not found (event ${playback.infoEvent.getId()})`);
55+
}
56+
5157
const playbackToggle = (): void => {
5258
playback.toggle();
5359
};
@@ -87,7 +93,7 @@ export const useVoiceBroadcastPlayback = (
8793
liveness: liveness,
8894
playbackState,
8995
room: room,
90-
sender: playback.infoEvent.sender,
96+
sender,
9197
toggle: playbackToggle,
9298
};
9399
};

src/voice-broadcast/hooks/useVoiceBroadcastRecording.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -66,6 +66,12 @@ export const useVoiceBroadcastRecording = (
6666
throw new Error("Unable to find voice broadcast room with Id: " + roomId);
6767
}
6868

69+
const sender = recording.infoEvent.sender;
70+
71+
if (!sender) {
72+
throw new Error(`Voice Broadcast sender not found (event ${recording.infoEvent.getId()})`);
73+
}
74+
6975
const stopRecording = async (): Promise<void> => {
7076
const confirmed = await showStopBroadcastingDialog();
7177

@@ -99,7 +105,7 @@ export const useVoiceBroadcastRecording = (
99105
timeLeft,
100106
recordingState,
101107
room,
102-
sender: recording.infoEvent.sender,
108+
sender,
103109
stopRecording,
104110
toggleRecording: recording.toggle,
105111
};

src/voice-broadcast/stores/VoiceBroadcastRecordingsStore.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,7 +17,12 @@ limitations under the License.
1717
import { MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
1818
import { TypedEventEmitter } from "matrix-js-sdk/src/models/typed-event-emitter";
1919

20-
import { VoiceBroadcastInfoState, VoiceBroadcastRecording, VoiceBroadcastRecordingEvent } from "..";
20+
import {
21+
VoiceBroadcastInfoState,
22+
VoiceBroadcastRecording,
23+
VoiceBroadcastRecordingEvent,
24+
VoiceBroadcastRecordingState,
25+
} from "..";
2126

2227
export enum VoiceBroadcastRecordingsStoreEvent {
2328
CurrentChanged = "current_changed",
@@ -85,7 +90,7 @@ export class VoiceBroadcastRecordingsStore extends TypedEventEmitter<VoiceBroadc
8590
return recording;
8691
}
8792

88-
private onCurrentStateChanged = (state: VoiceBroadcastInfoState): void => {
93+
private onCurrentStateChanged = (state: VoiceBroadcastRecordingState): void => {
8994
if (state === VoiceBroadcastInfoState.Stopped) {
9095
this.clearCurrent();
9196
}

test/voice-broadcast/components/atoms/VoiceBroadcastPlaybackControl-test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -38,6 +38,12 @@ describe("<VoiceBroadcastPlaybackControl />", () => {
3838
expect(renderControl(state).result.container).toMatchSnapshot();
3939
});
4040

41+
it("should not render for error state", () => {
42+
expect(renderControl(VoiceBroadcastPlaybackState.Error).result.asFragment()).toMatchInlineSnapshot(
43+
`<DocumentFragment />`,
44+
);
45+
});
46+
4147
describe("when clicking the control", () => {
4248
let onClick: () => void;
4349

test/voice-broadcast/components/molecules/VoiceBroadcastPlaybackBody-test.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -233,15 +233,25 @@ describe("VoiceBroadcastPlaybackBody", () => {
233233
describe.each([
234234
[VoiceBroadcastPlaybackState.Paused, "not-live"],
235235
[VoiceBroadcastPlaybackState.Playing, "live"],
236-
])("when rendering a %s/%s broadcast", (state: VoiceBroadcastPlaybackState, liveness: VoiceBroadcastLiveness) => {
237-
beforeEach(() => {
238-
mocked(playback.getState).mockReturnValue(state);
239-
mocked(playback.getLiveness).mockReturnValue(liveness);
240-
renderResult = render(<VoiceBroadcastPlaybackBody playback={playback} />);
241-
});
236+
] satisfies [VoiceBroadcastPlaybackState, VoiceBroadcastLiveness][])(
237+
"when rendering a %s/%s broadcast",
238+
(state: VoiceBroadcastPlaybackState, liveness: VoiceBroadcastLiveness) => {
239+
beforeEach(() => {
240+
mocked(playback.getState).mockReturnValue(state);
241+
mocked(playback.getLiveness).mockReturnValue(liveness);
242+
renderResult = render(<VoiceBroadcastPlaybackBody playback={playback} />);
243+
});
242244

243-
it("should render as expected", () => {
244-
expect(renderResult.container).toMatchSnapshot();
245-
});
245+
it("should render as expected", () => {
246+
expect(renderResult.container).toMatchSnapshot();
247+
});
248+
},
249+
);
250+
251+
it("when there is a broadcast without sender, it should raise an error", () => {
252+
infoEvent.sender = null;
253+
expect(() => {
254+
render(<VoiceBroadcastPlaybackBody playback={playback} />);
255+
}).toThrow(`Voice Broadcast sender not found (event ${playback.infoEvent.getId()})`);
246256
});
247257
});

test/voice-broadcast/components/molecules/VoiceBroadcastRecordingBody-test.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 The Matrix.org Foundation C.I.C.
2+
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -77,4 +77,11 @@ describe("VoiceBroadcastRecordingBody", () => {
7777
expect(renderResult.container).toMatchSnapshot();
7878
});
7979
});
80+
81+
it("when there is a broadcast without sender, it should raise an error", () => {
82+
infoEvent.sender = null;
83+
expect(() => {
84+
render(<VoiceBroadcastRecordingBody recording={recording} />);
85+
}).toThrow(`Voice Broadcast sender not found (event ${recording.infoEvent.getId()})`);
86+
});
8087
});

0 commit comments

Comments
 (0)