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

Commit 9a386cf

Browse files
committed
Test VideoLobby
1 parent fb36fe0 commit 9a386cf

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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 { mount } from "enzyme";
19+
import { act } from "react-dom/test-utils";
20+
import { mocked } from "jest-mock";
21+
22+
import { stubClient, stubVideoChannelStore, mkRoom } from "../../../test-utils";
23+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
24+
import VideoLobby from "../../../../src/components/views/voip/VideoLobby";
25+
26+
describe("VideoLobby", () => {
27+
stubClient();
28+
Object.defineProperty(navigator, "mediaDevices", {
29+
value: {
30+
enumerateDevices: jest.fn(),
31+
getUserMedia: () => null,
32+
},
33+
});
34+
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
35+
36+
const cli = MatrixClientPeg.get();
37+
const room = mkRoom(cli, "!1:example.org");
38+
39+
let store;
40+
beforeEach(() => {
41+
store = stubVideoChannelStore();
42+
mocked(navigator.mediaDevices.enumerateDevices).mockResolvedValue([]);
43+
});
44+
45+
afterEach(() => {
46+
jest.clearAllMocks();
47+
});
48+
49+
describe("device buttons", () => {
50+
it("hides when no devices are available", async () => {
51+
const lobby = mount(<VideoLobby room={room} />);
52+
// Wait for state to settle
53+
await act(() => Promise.resolve());
54+
lobby.update();
55+
56+
expect(lobby.find("DeviceButton").children().exists()).toEqual(false);
57+
});
58+
59+
it("hides device list when only one device is available", async () => {
60+
mocked(navigator.mediaDevices.enumerateDevices).mockResolvedValue([{
61+
deviceId: "1",
62+
groupId: "1",
63+
label: "Webcam",
64+
kind: "videoinput",
65+
toJSON: () => {},
66+
}]);
67+
68+
const lobby = mount(<VideoLobby room={room} />);
69+
// Wait for state to settle
70+
await act(() => Promise.resolve());
71+
lobby.update();
72+
73+
expect(lobby.find(".mx_VideoLobby_deviceListButton").exists()).toEqual(false);
74+
});
75+
76+
it("shows device list when multiple devices are available", async () => {
77+
mocked(navigator.mediaDevices.enumerateDevices).mockResolvedValue([
78+
{
79+
deviceId: "1",
80+
groupId: "1",
81+
label: "Front camera",
82+
kind: "videoinput",
83+
toJSON: () => {},
84+
},
85+
{
86+
deviceId: "2",
87+
groupId: "1",
88+
label: "Back camera",
89+
kind: "videoinput",
90+
toJSON: () => {},
91+
},
92+
]);
93+
94+
const lobby = mount(<VideoLobby room={room} />);
95+
// Wait for state to settle
96+
await act(() => Promise.resolve());
97+
lobby.update();
98+
99+
expect(lobby.find(".mx_VideoLobby_deviceListButton").exists()).toEqual(true);
100+
});
101+
});
102+
103+
describe("join button", () => {
104+
it("works", async () => {
105+
const lobby = mount(<VideoLobby room={room} />);
106+
// Wait for state to settle
107+
await act(() => Promise.resolve());
108+
lobby.update();
109+
110+
act(() => {
111+
lobby.find("AccessibleButton.mx_VideoLobby_joinButton").simulate("click");
112+
});
113+
expect(store.connect).toHaveBeenCalled();
114+
});
115+
});
116+
});

test/test-utils/video.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ class StubVideoChannelStore extends EventEmitter {
2929
this._roomId = roomId;
3030
this.emit(VideoChannelEvent.StartConnect, roomId);
3131
};
32-
public connect = (roomId: string) => {
32+
public connect = jest.fn((roomId: string) => {
3333
this._roomId = roomId;
3434
this._connected = true;
3535
this.emit(VideoChannelEvent.Connect, roomId);
36-
};
37-
public disconnect = () => {
36+
});
37+
public disconnect = jest.fn(() => {
3838
const roomId = this._roomId;
3939
this._roomId = null;
4040
this._connected = false;
4141
this.emit(VideoChannelEvent.Disconnect, roomId);
42-
};
42+
});
4343
}
4444

4545
export const stubVideoChannelStore = (): StubVideoChannelStore => {

0 commit comments

Comments
 (0)