element-web/test/voice-broadcast/stores/VoiceBroadcastRecordingsStore-test.ts
Michael Weimann d775e403c4
Add voice broadcast recording stores (#9319)
* Add voice broadcast recording stores

* Refactor startNewVoiceBroadcastRecording
2022-09-28 10:22:50 +02:00

129 lines
4.2 KiB
TypeScript

/*
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 { mocked } from "jest-mock";
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
import {
VoiceBroadcastInfoEventType,
VoiceBroadcastRecordingsStore,
VoiceBroadcastRecordingsStoreEvent,
VoiceBroadcastRecording,
} from "../../../src/voice-broadcast";
import { mkEvent, mkStubRoom, stubClient } from "../../test-utils";
jest.mock("../../../src/voice-broadcast/models/VoiceBroadcastRecording.ts", () => ({
VoiceBroadcastRecording: jest.fn().mockImplementation(
(
infoEvent: MatrixEvent,
client: MatrixClient,
) => ({ infoEvent, client }),
),
}));
describe("VoiceBroadcastRecordingsStore", () => {
const roomId = "!room:example.com";
let client: MatrixClient;
let room: Room;
let infoEvent: MatrixEvent;
let recording: VoiceBroadcastRecording;
let recordings: VoiceBroadcastRecordingsStore;
let onCurrentChanged: (recording: VoiceBroadcastRecording) => void;
beforeEach(() => {
client = stubClient();
room = mkStubRoom(roomId, "test room", client);
mocked(client.getRoom).mockImplementation((roomId: string) => {
if (roomId === room.roomId) {
return room;
}
});
infoEvent = mkEvent({
event: true,
type: VoiceBroadcastInfoEventType,
user: client.getUserId(),
room: roomId,
content: {},
});
recording = {
infoEvent,
} as unknown as VoiceBroadcastRecording;
recordings = new VoiceBroadcastRecordingsStore();
onCurrentChanged = jest.fn();
recordings.on(VoiceBroadcastRecordingsStoreEvent.CurrentChanged, onCurrentChanged);
});
afterEach(() => {
recordings.off(VoiceBroadcastRecordingsStoreEvent.CurrentChanged, onCurrentChanged);
});
describe("when setting a current Voice Broadcast recording", () => {
beforeEach(() => {
recordings.setCurrent(recording);
});
it("should return it as current", () => {
expect(recordings.current).toBe(recording);
});
it("should return it by id", () => {
expect(recordings.getByInfoEvent(infoEvent, client)).toBe(recording);
});
it("should emit a CurrentChanged event", () => {
expect(onCurrentChanged).toHaveBeenCalledWith(recording);
});
describe("and setting the same again", () => {
beforeEach(() => {
mocked(onCurrentChanged).mockClear();
recordings.setCurrent(recording);
});
it("should not emit a CurrentChanged event", () => {
expect(onCurrentChanged).not.toHaveBeenCalled();
});
});
});
describe("getByInfoEventId", () => {
let returnedRecording: VoiceBroadcastRecording;
describe("when retrieving a known recording", () => {
beforeEach(() => {
recordings.setCurrent(recording);
returnedRecording = recordings.getByInfoEvent(infoEvent, client);
});
it("should return the recording", () => {
expect(returnedRecording).toBe(recording);
});
});
describe("when retrieving an unknown recording", () => {
beforeEach(() => {
returnedRecording = recordings.getByInfoEvent(infoEvent, client);
});
it("should return the recording", () => {
expect(returnedRecording).toEqual({
infoEvent,
client,
});
});
});
});
});