2022-03-22 23:14:11 +01:00
|
|
|
/*
|
|
|
|
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 React from "react";
|
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { act } from "react-dom/test-utils";
|
2022-03-23 00:07:37 +01:00
|
|
|
import { mocked } from "jest-mock";
|
2022-03-28 15:12:09 +02:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2022-03-22 23:14:11 +01:00
|
|
|
|
|
|
|
import "../../../skinned-sdk";
|
2022-03-28 15:12:09 +02:00
|
|
|
import {
|
|
|
|
stubClient,
|
|
|
|
mockStateEventImplementation,
|
|
|
|
mkRoom,
|
|
|
|
mkEvent,
|
|
|
|
} from "../../../test-utils";
|
|
|
|
import { stubVoiceChannelStore } from "../../../test-utils/voice";
|
2022-03-22 23:14:11 +01:00
|
|
|
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
|
2022-03-28 15:12:09 +02:00
|
|
|
import MemberAvatar from "../../../../src/components/views/avatars/MemberAvatar";
|
2022-03-22 23:14:11 +01:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import VoiceChannelStore, { VoiceChannelEvent } from "../../../../src/stores/VoiceChannelStore";
|
|
|
|
import { DefaultTagID } from "../../../../src/stores/room-list/models";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2022-03-28 15:12:09 +02:00
|
|
|
import { VOICE_CHANNEL_MEMBER } from "../../../../src/utils/VoiceChannelUtils";
|
2022-03-22 23:40:09 +01:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-23 00:07:37 +01:00
|
|
|
import PlatformPeg from "../../../../src/PlatformPeg";
|
2022-03-22 23:40:09 +01:00
|
|
|
import BasePlatform from "../../../../src/BasePlatform";
|
2022-03-22 23:14:11 +01:00
|
|
|
|
2022-03-28 15:12:09 +02:00
|
|
|
const mkVoiceChannelMember = (userId: string, devices: string[]): MatrixEvent => mkEvent({
|
|
|
|
event: true,
|
|
|
|
type: VOICE_CHANNEL_MEMBER,
|
|
|
|
room: "!1:example.org",
|
|
|
|
user: userId,
|
|
|
|
skey: userId,
|
|
|
|
content: { devices },
|
|
|
|
});
|
|
|
|
|
2022-03-22 23:14:11 +01:00
|
|
|
describe("RoomTile", () => {
|
2022-03-22 23:40:09 +01:00
|
|
|
jest.spyOn(PlatformPeg, 'get')
|
|
|
|
.mockReturnValue({ overrideBrowserShortcuts: () => false } as unknown as BasePlatform);
|
|
|
|
|
2022-03-28 15:12:09 +02:00
|
|
|
let cli;
|
|
|
|
let store;
|
2022-03-22 23:40:09 +01:00
|
|
|
|
2022-03-22 23:14:11 +01:00
|
|
|
beforeEach(() => {
|
2022-03-22 23:40:09 +01:00
|
|
|
const realGetValue = SettingsStore.getValue;
|
2022-03-28 15:12:09 +02:00
|
|
|
SettingsStore.getValue = <T, >(name: string, roomId?: string): T => {
|
2022-03-22 23:40:09 +01:00
|
|
|
if (name === "feature_voice_rooms") {
|
2022-03-28 15:12:09 +02:00
|
|
|
return true as unknown as T;
|
2022-03-22 23:40:09 +01:00
|
|
|
}
|
|
|
|
return realGetValue(name, roomId);
|
2022-03-28 15:12:09 +02:00
|
|
|
};
|
2022-03-22 23:40:09 +01:00
|
|
|
|
2022-03-22 23:14:11 +01:00
|
|
|
stubClient();
|
2022-03-28 15:12:09 +02:00
|
|
|
stubVoiceChannelStore();
|
2022-03-22 23:14:11 +01:00
|
|
|
DMRoomMap.makeShared();
|
2022-03-28 15:12:09 +02:00
|
|
|
|
|
|
|
cli = mocked(MatrixClientPeg.get());
|
|
|
|
store = VoiceChannelStore.instance;
|
2022-03-22 23:14:11 +01:00
|
|
|
});
|
|
|
|
|
2022-03-28 15:12:09 +02:00
|
|
|
afterEach(() => jest.clearAllMocks());
|
|
|
|
|
2022-03-22 23:14:11 +01:00
|
|
|
describe("voice rooms", () => {
|
2022-03-28 15:12:09 +02:00
|
|
|
const room = mkRoom(cli, "!1:example.org");
|
|
|
|
room.isCallRoom.mockReturnValue(true);
|
2022-03-22 23:14:11 +01:00
|
|
|
|
|
|
|
it("tracks connection state", async () => {
|
2022-03-28 15:12:09 +02:00
|
|
|
// Insert a breakpoint in the connect method, so we can see the intermediate connecting state
|
|
|
|
let continueJoin;
|
|
|
|
const breakpoint = new Promise(resolve => continueJoin = resolve);
|
|
|
|
const realConnect = store.connect;
|
|
|
|
store.connect = async () => {
|
|
|
|
await breakpoint;
|
|
|
|
await realConnect();
|
|
|
|
};
|
|
|
|
|
2022-03-22 23:14:11 +01:00
|
|
|
const tile = mount(
|
|
|
|
<RoomTile
|
|
|
|
room={room}
|
|
|
|
showMessagePreview={false}
|
|
|
|
isMinimized={false}
|
|
|
|
tag={DefaultTagID.Untagged}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
expect(tile.find(".mx_RoomTile_voiceIndicator").text()).toEqual("Voice room");
|
|
|
|
|
|
|
|
act(() => { tile.simulate("click"); });
|
|
|
|
tile.update();
|
|
|
|
expect(tile.find(".mx_RoomTile_voiceIndicator").text()).toEqual("Connecting...");
|
|
|
|
|
2022-03-28 15:12:09 +02:00
|
|
|
// Now we confirm the join and wait for the store to update
|
2022-03-22 23:14:11 +01:00
|
|
|
const waitForConnect = new Promise<void>(resolve =>
|
2022-03-28 15:12:09 +02:00
|
|
|
store.once(VoiceChannelEvent.Connect, resolve),
|
2022-03-22 23:14:11 +01:00
|
|
|
);
|
2022-03-28 15:12:09 +02:00
|
|
|
continueJoin();
|
2022-03-22 23:14:11 +01:00
|
|
|
await waitForConnect;
|
2022-03-28 15:12:09 +02:00
|
|
|
// Wait exactly 2 ticks for the room tile to update
|
|
|
|
await Promise.resolve();
|
2022-03-22 23:14:11 +01:00
|
|
|
await Promise.resolve();
|
|
|
|
|
|
|
|
tile.update();
|
|
|
|
expect(tile.find(".mx_RoomTile_voiceIndicator").text()).toEqual("Connected");
|
|
|
|
|
2022-03-28 15:12:09 +02:00
|
|
|
await store.disconnect();
|
2022-03-22 23:14:11 +01:00
|
|
|
|
|
|
|
tile.update();
|
|
|
|
expect(tile.find(".mx_RoomTile_voiceIndicator").text()).toEqual("Voice room");
|
|
|
|
});
|
2022-03-28 15:12:09 +02:00
|
|
|
|
|
|
|
it("displays connected members", async () => {
|
|
|
|
mocked(room.currentState).getStateEvents.mockImplementation(mockStateEventImplementation([
|
|
|
|
// A user connected from 2 devices
|
|
|
|
mkVoiceChannelMember("@alice:example.org", ["device 1", "device 2"]),
|
|
|
|
// A disconnected user
|
|
|
|
mkVoiceChannelMember("@bob:example.org", []),
|
|
|
|
// A user that claims to have a connected device, but has left the room
|
|
|
|
mkVoiceChannelMember("@chris:example.org", ["device 1"]),
|
|
|
|
]));
|
|
|
|
|
|
|
|
mocked(room.currentState).getMember.mockImplementation(userId => ({
|
|
|
|
userId,
|
|
|
|
membership: userId === "@chris:example.org" ? "leave" : "join",
|
|
|
|
name: userId,
|
|
|
|
rawDisplayName: userId,
|
|
|
|
roomId: "!1:example.org",
|
|
|
|
getAvatarUrl: () => {},
|
|
|
|
getMxcAvatarUrl: () => {},
|
|
|
|
}) as unknown as RoomMember);
|
|
|
|
|
|
|
|
const tile = mount(
|
|
|
|
<RoomTile
|
|
|
|
room={room}
|
|
|
|
showMessagePreview={false}
|
|
|
|
isMinimized={false}
|
|
|
|
tag={DefaultTagID.Untagged}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Only Alice should display as connected
|
|
|
|
const avatar = tile.find(MemberAvatar);
|
|
|
|
expect(avatar.length).toEqual(1);
|
|
|
|
expect(avatar.props().member.userId).toEqual("@alice:example.org");
|
|
|
|
});
|
2022-03-22 23:14:11 +01:00
|
|
|
});
|
|
|
|
});
|