|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from "react"; |
| 18 | +import { render, RenderResult } from "@testing-library/react"; |
| 19 | +import { mocked } from "jest-mock"; |
| 20 | +import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; |
| 21 | + |
| 22 | +import { Features } from "../../../../src/settings/Settings"; |
| 23 | +import SettingsStore, { CallbackFn } from "../../../../src/settings/SettingsStore"; |
| 24 | +import { VoiceBroadcastInfoEventType, VoiceBroadcastInfoState } from "../../../../src/voice-broadcast"; |
| 25 | +import { mkEvent, mkRoom, stubClient } from "../../../test-utils"; |
| 26 | +import MessageEvent from "../../../../src/components/views/messages/MessageEvent"; |
| 27 | +import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks"; |
| 28 | + |
| 29 | +jest.mock("../../../../src/components/views/messages/UnknownBody", () => ({ |
| 30 | + __esModule: true, |
| 31 | + default: () => (<div data-testid="unknown-body" />), |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock("../../../../src/voice-broadcast/components/VoiceBroadcastBody", () => ({ |
| 35 | + VoiceBroadcastBody: () => (<div data-testid="voice-broadcast-body" />), |
| 36 | +})); |
| 37 | + |
| 38 | +describe("MessageEvent", () => { |
| 39 | + let room: Room; |
| 40 | + let client: MatrixClient; |
| 41 | + let event: MatrixEvent; |
| 42 | + |
| 43 | + const renderMessageEvent = (): RenderResult => { |
| 44 | + return render(<MessageEvent |
| 45 | + mxEvent={event} |
| 46 | + onHeightChanged={jest.fn()} |
| 47 | + permalinkCreator={new RoomPermalinkCreator(room)} |
| 48 | + />); |
| 49 | + }; |
| 50 | + |
| 51 | + beforeEach(() => { |
| 52 | + client = stubClient(); |
| 53 | + room = mkRoom(client, "!room:example.com"); |
| 54 | + jest.spyOn(SettingsStore, "getValue"); |
| 55 | + jest.spyOn(SettingsStore, "watchSetting"); |
| 56 | + jest.spyOn(SettingsStore, "unwatchSetting").mockImplementation(jest.fn()); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("when a voice broadcast start event occurs", () => { |
| 60 | + const voiceBroadcastSettingWatcherRef = "vb ref"; |
| 61 | + let onVoiceBroadcastSettingChanged: CallbackFn; |
| 62 | + |
| 63 | + beforeEach(() => { |
| 64 | + event = mkEvent({ |
| 65 | + event: true, |
| 66 | + type: VoiceBroadcastInfoEventType, |
| 67 | + user: client.getUserId(), |
| 68 | + room: room.roomId, |
| 69 | + content: { |
| 70 | + state: VoiceBroadcastInfoState.Started, |
| 71 | + }, |
| 72 | + }); |
| 73 | + |
| 74 | + mocked(SettingsStore.watchSetting).mockImplementation( |
| 75 | + (settingName: string, roomId: string | null, callbackFn: CallbackFn) => { |
| 76 | + if (settingName === Features.VoiceBroadcast) { |
| 77 | + onVoiceBroadcastSettingChanged = callbackFn; |
| 78 | + return voiceBroadcastSettingWatcherRef; |
| 79 | + } |
| 80 | + }, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("and the voice broadcast feature is enabled", () => { |
| 85 | + let result: RenderResult; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + mocked(SettingsStore.getValue).mockImplementation((settingName: string) => { |
| 89 | + return settingName === Features.VoiceBroadcast; |
| 90 | + }); |
| 91 | + result = renderMessageEvent(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should render a VoiceBroadcast component", () => { |
| 95 | + result.getByTestId("voice-broadcast-body"); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("and switching the voice broadcast feature off", () => { |
| 99 | + beforeEach(() => { |
| 100 | + onVoiceBroadcastSettingChanged(Features.VoiceBroadcast, null, null, null, false); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should render an UnknownBody component", () => { |
| 104 | + const result = renderMessageEvent(); |
| 105 | + result.getByTestId("unknown-body"); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe("and unmounted", () => { |
| 110 | + beforeEach(() => { |
| 111 | + result.unmount(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should unregister the settings watcher", () => { |
| 115 | + expect(SettingsStore.unwatchSetting).toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe("and the voice broadcast feature is disabled", () => { |
| 121 | + beforeEach(() => { |
| 122 | + mocked(SettingsStore.getValue).mockImplementation((settingName: string) => { |
| 123 | + return false; |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should render an UnknownBody component", () => { |
| 128 | + const result = renderMessageEvent(); |
| 129 | + result.getByTestId("unknown-body"); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | +}); |
0 commit comments