|
| 1 | +/* |
| 2 | +Copyright 2024 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 { mocked, Mocked } from "jest-mock"; |
| 19 | +import { render, screen } from "@testing-library/react"; |
| 20 | +import { MatrixClient, Room } from "matrix-js-sdk/src/matrix"; |
| 21 | +import { MatrixWidgetType } from "matrix-widget-api"; |
| 22 | +import userEvent from "@testing-library/user-event"; |
| 23 | + |
| 24 | +import ExtensionsCard from "../../../../src/components/views/right_panel/ExtensionsCard"; |
| 25 | +import { stubClient } from "../../../test-utils"; |
| 26 | +import { IApp } from "../../../../src/stores/WidgetStore"; |
| 27 | +import WidgetUtils, { useWidgets } from "../../../../src/utils/WidgetUtils"; |
| 28 | +import { WidgetLayoutStore } from "../../../../src/stores/widgets/WidgetLayoutStore"; |
| 29 | +import { IntegrationManagers } from "../../../../src/integrations/IntegrationManagers"; |
| 30 | + |
| 31 | +jest.mock("../../../../src/utils/WidgetUtils"); |
| 32 | + |
| 33 | +describe("<ExtensionsCard />", () => { |
| 34 | + let client: Mocked<MatrixClient>; |
| 35 | + let room: Room; |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + client = mocked(stubClient()); |
| 39 | + room = new Room("!room:server", client, client.getSafeUserId()); |
| 40 | + mocked(WidgetUtils.getWidgetName).mockImplementation((app) => app?.name ?? "No Name"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should render empty state", () => { |
| 44 | + mocked(useWidgets).mockReturnValue([]); |
| 45 | + const { asFragment } = render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 46 | + expect(screen.getByText("Boost productivity with more tools, widgets and bots")).toBeInTheDocument(); |
| 47 | + expect(asFragment()).toMatchSnapshot(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should render widgets", async () => { |
| 51 | + mocked(useWidgets).mockReturnValue([ |
| 52 | + { |
| 53 | + id: "id", |
| 54 | + roomId: room.roomId, |
| 55 | + eventId: "$event1", |
| 56 | + creatorUserId: client.getSafeUserId(), |
| 57 | + type: MatrixWidgetType.Custom, |
| 58 | + name: "Custom Widget", |
| 59 | + url: "http://url1", |
| 60 | + }, |
| 61 | + { |
| 62 | + id: "jitsi", |
| 63 | + roomId: room.roomId, |
| 64 | + eventId: "$event2", |
| 65 | + creatorUserId: client.getSafeUserId(), |
| 66 | + type: MatrixWidgetType.JitsiMeet, |
| 67 | + name: "Jitsi", |
| 68 | + url: "http://jitsi", |
| 69 | + }, |
| 70 | + ] satisfies IApp[]); |
| 71 | + |
| 72 | + const { asFragment } = render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 73 | + expect(screen.getByText("Custom Widget")).toBeInTheDocument(); |
| 74 | + expect(screen.getByText("Jitsi")).toBeInTheDocument(); |
| 75 | + expect(asFragment()).toMatchSnapshot(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should show context menu on widget row", async () => { |
| 79 | + jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true); |
| 80 | + mocked(useWidgets).mockReturnValue([ |
| 81 | + { |
| 82 | + id: "id", |
| 83 | + roomId: room.roomId, |
| 84 | + eventId: "$event1", |
| 85 | + creatorUserId: client.getSafeUserId(), |
| 86 | + type: MatrixWidgetType.Custom, |
| 87 | + name: "Custom Widget", |
| 88 | + url: "http://url1", |
| 89 | + }, |
| 90 | + ] satisfies IApp[]); |
| 91 | + |
| 92 | + const { container } = render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 93 | + await userEvent.click(container.querySelector(".mx_ExtensionsCard_app_options")!); |
| 94 | + expect(document.querySelector(".mx_IconizedContextMenu")).toMatchSnapshot(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should show set room layout button", async () => { |
| 98 | + jest.spyOn(WidgetLayoutStore.instance, "canCopyLayoutToRoom").mockReturnValue(true); |
| 99 | + mocked(useWidgets).mockReturnValue([ |
| 100 | + { |
| 101 | + id: "id", |
| 102 | + roomId: room.roomId, |
| 103 | + eventId: "$event1", |
| 104 | + creatorUserId: client.getSafeUserId(), |
| 105 | + type: MatrixWidgetType.Custom, |
| 106 | + name: "Custom Widget", |
| 107 | + url: "http://url1", |
| 108 | + }, |
| 109 | + ] satisfies IApp[]); |
| 110 | + |
| 111 | + render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 112 | + expect(screen.getByText("Set layout for everyone")).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should show widget as pinned", async () => { |
| 116 | + jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockReturnValue(true); |
| 117 | + mocked(useWidgets).mockReturnValue([ |
| 118 | + { |
| 119 | + id: "id", |
| 120 | + roomId: room.roomId, |
| 121 | + eventId: "$event1", |
| 122 | + creatorUserId: client.getSafeUserId(), |
| 123 | + type: MatrixWidgetType.Custom, |
| 124 | + name: "Custom Widget", |
| 125 | + url: "http://url1", |
| 126 | + }, |
| 127 | + ] satisfies IApp[]); |
| 128 | + |
| 129 | + render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 130 | + expect(screen.getByText("Custom Widget").closest(".mx_ExtensionsCard_Button_pinned")).toBeInTheDocument(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should show cannot pin warning", async () => { |
| 134 | + jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockReturnValue(false); |
| 135 | + jest.spyOn(WidgetLayoutStore.instance, "canAddToContainer").mockReturnValue(false); |
| 136 | + mocked(useWidgets).mockReturnValue([ |
| 137 | + { |
| 138 | + id: "id", |
| 139 | + roomId: room.roomId, |
| 140 | + eventId: "$event1", |
| 141 | + creatorUserId: client.getSafeUserId(), |
| 142 | + type: MatrixWidgetType.Custom, |
| 143 | + name: "Custom Widget", |
| 144 | + url: "http://url1", |
| 145 | + }, |
| 146 | + ] satisfies IApp[]); |
| 147 | + |
| 148 | + render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 149 | + expect(screen.getByLabelText("You can only pin up to 3 widgets")).toBeInTheDocument(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should should open integration manager on click", async () => { |
| 153 | + jest.spyOn(IntegrationManagers.sharedInstance(), "hasManager").mockReturnValue(false); |
| 154 | + const spy = jest.spyOn(IntegrationManagers.sharedInstance(), "openNoManagerDialog"); |
| 155 | + render(<ExtensionsCard room={room} onClose={jest.fn()} />); |
| 156 | + await userEvent.click(screen.getByText("Add extensions")); |
| 157 | + expect(spy).toHaveBeenCalled(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments