|
| 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 { act } from "react-dom/test-utils"; |
| 19 | +import { |
| 20 | + Room, |
| 21 | + PendingEventOrdering, |
| 22 | + MatrixClient, |
| 23 | + RoomMember, |
| 24 | + RoomStateEvent, |
| 25 | +} from "matrix-js-sdk/src/matrix"; |
| 26 | +import { ClientWidgetApi, Widget } from "matrix-widget-api"; |
| 27 | +import { |
| 28 | + cleanup, |
| 29 | + render, |
| 30 | + screen, |
| 31 | +} from "@testing-library/react"; |
| 32 | +import { mocked, Mocked } from "jest-mock"; |
| 33 | + |
| 34 | +import { |
| 35 | + mkRoomMember, |
| 36 | + MockedCall, |
| 37 | + setupAsyncStoreWithClient, |
| 38 | + stubClient, |
| 39 | + useMockedCalls, |
| 40 | +} from "../../../test-utils"; |
| 41 | +import RoomCallBanner from "../../../../src/components/views/beacon/RoomCallBanner"; |
| 42 | +import { CallStore } from "../../../../src/stores/CallStore"; |
| 43 | +import { WidgetMessagingStore } from "../../../../src/stores/widgets/WidgetMessagingStore"; |
| 44 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 45 | +import { RoomViewStore } from "../../../../src/stores/RoomViewStore"; |
| 46 | +import { ConnectionState } from "../../../../src/models/Call"; |
| 47 | + |
| 48 | +describe("<RoomCallBanner />", () => { |
| 49 | + let client: Mocked<MatrixClient>; |
| 50 | + let room: Room; |
| 51 | + let alice: RoomMember; |
| 52 | + useMockedCalls(); |
| 53 | + |
| 54 | + const defaultProps = { |
| 55 | + roomId: "!1:example.org", |
| 56 | + }; |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + stubClient(); |
| 60 | + |
| 61 | + client = mocked(MatrixClientPeg.get()); |
| 62 | + |
| 63 | + room = new Room("!1:example.org", client, "@alice:example.org", { |
| 64 | + pendingEventOrdering: PendingEventOrdering.Detached, |
| 65 | + }); |
| 66 | + alice = mkRoomMember(room.roomId, "@alice:example.org"); |
| 67 | + jest.spyOn(room, "getMember").mockImplementation((userId) => |
| 68 | + userId === alice.userId ? alice : null, |
| 69 | + ); |
| 70 | + |
| 71 | + client.getRoom.mockImplementation((roomId) => |
| 72 | + roomId === room.roomId ? room : null, |
| 73 | + ); |
| 74 | + client.getRooms.mockReturnValue([room]); |
| 75 | + client.reEmitter.reEmit(room, [RoomStateEvent.Events]); |
| 76 | + |
| 77 | + setupAsyncStoreWithClient(CallStore.instance, client); |
| 78 | + setupAsyncStoreWithClient(WidgetMessagingStore.instance, client); |
| 79 | + }); |
| 80 | + |
| 81 | + afterEach(async () => { |
| 82 | + client.reEmitter.stopReEmitting(room, [RoomStateEvent.Events]); |
| 83 | + }); |
| 84 | + |
| 85 | + const renderBanner = async (props = {}): Promise<void> => { |
| 86 | + render(<RoomCallBanner {...defaultProps} {...props} />); |
| 87 | + await act(() => Promise.resolve()); // Let effects settle |
| 88 | + }; |
| 89 | + |
| 90 | + it("renders nothing when there is no call", async () => { |
| 91 | + await renderBanner(); |
| 92 | + const banner = await screen.queryByText("Video call"); |
| 93 | + expect(banner).toBeFalsy(); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("call started", () => { |
| 97 | + let call: MockedCall; |
| 98 | + let widget: Widget; |
| 99 | + |
| 100 | + beforeEach(() => { |
| 101 | + MockedCall.create(room, "1"); |
| 102 | + const maybeCall = CallStore.instance.getCall(room.roomId); |
| 103 | + if (!(maybeCall instanceof MockedCall)) {throw new Error("Failed to create call");} |
| 104 | + call = maybeCall; |
| 105 | + |
| 106 | + widget = new Widget(call.widget); |
| 107 | + WidgetMessagingStore.instance.storeMessaging(widget, room.roomId, { |
| 108 | + stop: () => {}, |
| 109 | + } as unknown as ClientWidgetApi); |
| 110 | + }); |
| 111 | + afterEach(() => { |
| 112 | + cleanup(); // Unmount before we do any cleanup that might update the component |
| 113 | + call.destroy(); |
| 114 | + WidgetMessagingStore.instance.stopMessaging(widget, room.roomId); |
| 115 | + }); |
| 116 | + |
| 117 | + it("renders if there is a call", async () => { |
| 118 | + await renderBanner(); |
| 119 | + await screen.findByText("Video call"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("shows Join button if the user has not joined", async () => { |
| 123 | + await renderBanner(); |
| 124 | + await screen.findByText("Join"); |
| 125 | + }); |
| 126 | + |
| 127 | + it("doesn't show banner if the call is connected", async () => { |
| 128 | + call.setConnectionState(ConnectionState.Connected); |
| 129 | + await renderBanner(); |
| 130 | + const banner = await screen.queryByText("Video call"); |
| 131 | + expect(banner).toBeFalsy(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("doesn't show banner if the call is shown", async () => { |
| 135 | + jest.spyOn(RoomViewStore.instance, 'isViewingCall').mockReturnValue(true); |
| 136 | + await renderBanner(); |
| 137 | + const banner = await screen.queryByText("Video call"); |
| 138 | + expect(banner).toBeFalsy(); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + // TODO: test clicking buttons |
| 143 | + // TODO: add live location share warning test (should not render if there is an active live location share) |
| 144 | +}); |
0 commit comments