|
| 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 { EventEmitter } from "events"; |
| 18 | +import React from "react"; |
| 19 | +import { mount } from "enzyme"; |
| 20 | +import { act } from "react-dom/test-utils"; |
| 21 | + |
| 22 | +import "../../../skinned-sdk"; |
| 23 | +import { stubClient, mkStubRoom, wrapInMatrixClientContext } from "../../../test-utils"; |
| 24 | +import _VoiceChannelRadio from "../../../../src/components/views/voip/VoiceChannelRadio"; |
| 25 | +import VoiceChannelStore, { VoiceChannelEvent } from "../../../../src/stores/VoiceChannelStore"; |
| 26 | +import DMRoomMap from "../../../../src/utils/DMRoomMap"; |
| 27 | + |
| 28 | +const VoiceChannelRadio = wrapInMatrixClientContext(_VoiceChannelRadio); |
| 29 | + |
| 30 | +export default class StubVoiceChannelStore extends EventEmitter { |
| 31 | + private _roomId: string; |
| 32 | + public get roomId(): string { return this._roomId; } |
| 33 | + private _audioMuted: boolean; |
| 34 | + public get audioMuted(): boolean { return this._audioMuted; } |
| 35 | + private _videoMuted: boolean; |
| 36 | + public get videoMuted(): boolean { return this._videoMuted; } |
| 37 | + |
| 38 | + public connect = jest.fn().mockImplementation(async (roomId: string) => { |
| 39 | + this._roomId = roomId; |
| 40 | + this._audioMuted = true; |
| 41 | + this._videoMuted = true; |
| 42 | + this.emit(VoiceChannelEvent.Connect); |
| 43 | + }); |
| 44 | + public disconnect = jest.fn().mockImplementation(async () => { |
| 45 | + this._roomId = null; |
| 46 | + this.emit(VoiceChannelEvent.Disconnect); |
| 47 | + }); |
| 48 | + public muteAudio = jest.fn().mockImplementation(async () => { |
| 49 | + this._audioMuted = true; |
| 50 | + this.emit(VoiceChannelEvent.MuteAudio); |
| 51 | + }); |
| 52 | + public unmuteAudio = jest.fn().mockImplementation(async () => { |
| 53 | + this._audioMuted = false; |
| 54 | + this.emit(VoiceChannelEvent.UnmuteAudio); |
| 55 | + }); |
| 56 | + public muteVideo = jest.fn().mockImplementation(async () => { |
| 57 | + this._videoMuted = true; |
| 58 | + this.emit(VoiceChannelEvent.MuteVideo); |
| 59 | + }); |
| 60 | + public unmuteVideo = jest.fn().mockImplementation(async () => { |
| 61 | + this._videoMuted = false; |
| 62 | + this.emit(VoiceChannelEvent.UnmuteVideo); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +describe("VoiceChannelRadio", () => { |
| 67 | + const room = mkStubRoom("!1:example.org"); |
| 68 | + room.isCallRoom.mockReturnValue(true); |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + stubClient(); |
| 72 | + DMRoomMap.makeShared(); |
| 73 | + // Stub out the VoiceChannelStore |
| 74 | + jest.spyOn(VoiceChannelStore, "instance", "get").mockReturnValue(new StubVoiceChannelStore()); |
| 75 | + }); |
| 76 | + |
| 77 | + it("shows when connecting voice", async () => { |
| 78 | + const radio = mount(<VoiceChannelRadio />); |
| 79 | + expect(radio.children().children().exists()).toEqual(false); |
| 80 | + |
| 81 | + act(() => { VoiceChannelStore.instance.connect("!1:example.org"); }); |
| 82 | + radio.update(); |
| 83 | + expect(radio.children().children().exists()).toEqual(true); |
| 84 | + }); |
| 85 | + |
| 86 | + it("hides when disconnecting voice", () => { |
| 87 | + VoiceChannelStore.instance.connect("!1:example.org"); |
| 88 | + const radio = mount(<VoiceChannelRadio />); |
| 89 | + expect(radio.children().children().exists()).toEqual(true); |
| 90 | + |
| 91 | + act(() => { VoiceChannelStore.instance.disconnect(); }); |
| 92 | + radio.update(); |
| 93 | + expect(radio.children().children().exists()).toEqual(false); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("disconnect button", () => { |
| 97 | + it("works", () => { |
| 98 | + VoiceChannelStore.instance.connect("!1:example.org"); |
| 99 | + const radio = mount(<VoiceChannelRadio />); |
| 100 | + |
| 101 | + act(() => { |
| 102 | + radio.find("AccessibleButton.mx_VoiceChannelRadio_disconnectButton").simulate("click"); |
| 103 | + }); |
| 104 | + expect(VoiceChannelStore.instance.disconnect).toHaveBeenCalled(); |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + describe("video button", () => { |
| 109 | + it("works", () => { |
| 110 | + VoiceChannelStore.instance.connect("!1:example.org"); |
| 111 | + const radio = mount(<VoiceChannelRadio />); |
| 112 | + |
| 113 | + act(() => { |
| 114 | + radio.find("AccessibleButton.mx_VoiceChannelRadio_videoButton").simulate("click"); |
| 115 | + }); |
| 116 | + expect(VoiceChannelStore.instance.unmuteVideo).toHaveBeenCalled(); |
| 117 | + |
| 118 | + act(() => { |
| 119 | + radio.find("AccessibleButton.mx_VoiceChannelRadio_videoButton").simulate("click"); |
| 120 | + }); |
| 121 | + expect(VoiceChannelStore.instance.muteVideo).toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("audio button", () => { |
| 126 | + it("works", () => { |
| 127 | + VoiceChannelStore.instance.connect("!1:example.org"); |
| 128 | + const radio = mount(<VoiceChannelRadio />); |
| 129 | + |
| 130 | + act(() => { |
| 131 | + radio.find("AccessibleButton.mx_VoiceChannelRadio_audioButton").simulate("click"); |
| 132 | + }); |
| 133 | + expect(VoiceChannelStore.instance.unmuteAudio).toHaveBeenCalled(); |
| 134 | + |
| 135 | + act(() => { |
| 136 | + radio.find("AccessibleButton.mx_VoiceChannelRadio_audioButton").simulate("click"); |
| 137 | + }); |
| 138 | + expect(VoiceChannelStore.instance.muteAudio).toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments