Add support for Widget & Room Header Buttons module APIs (#32734)
* Add support for Widget & Room Header Buttons module APIs To support https://github.com/element-hq/element-modules/pull/217 * Update for new api * Test addRoomHeaderButtonCallback * Extra mock api * Test for widgetapi * Convert enum * Convert other enum usage * Add tests for widget context menu move buttons Which have just changed because of the enum * Add tests for moving the widgets * Fix copyright Co-authored-by: Florian Duros <florianduros@element.io> * Update module API * A little import/export --------- Co-authored-by: Florian Duros <florianduros@element.io>
This commit is contained in:
co-authored by
Florian Duros
parent
86692ce0a7
commit
09bbf796dc
@@ -680,11 +680,13 @@ export function mkStubRoom(
|
||||
maySendStateEvent: jest.fn().mockReturnValue(true),
|
||||
maySendRedactionForEvent: jest.fn().mockReturnValue(true),
|
||||
maySendEvent: jest.fn().mockReturnValue(true),
|
||||
maySendMessage: jest.fn().mockReturnValue(true),
|
||||
members: {},
|
||||
getHistoryVisibility: jest.fn().mockReturnValue(HistoryVisibility.Shared),
|
||||
getJoinRule: jest.fn().mockReturnValue(JoinRule.Invite),
|
||||
on: jest.fn(),
|
||||
off: jest.fn(),
|
||||
removeListener: jest.fn(),
|
||||
} as unknown as RoomState,
|
||||
eventShouldLiveIn: jest.fn().mockReturnValue({ shouldLiveInRoom: true, shouldLiveInThread: false }),
|
||||
fetchRoomThreads: jest.fn().mockReturnValue(Promise.resolve()),
|
||||
@@ -713,6 +715,7 @@ export function mkStubRoom(
|
||||
isKicked: () => false,
|
||||
}),
|
||||
getMembers: jest.fn().mockReturnValue([]),
|
||||
getEncryptionTargetMembers: jest.fn().mockReturnValue([]),
|
||||
getMembersWithMembership: jest.fn().mockReturnValue([]),
|
||||
getMxcAvatarUrl: () => "mxc://avatar.url/room.png",
|
||||
getMyMembership: jest.fn().mockReturnValue(KnownMembership.Join),
|
||||
|
||||
@@ -43,7 +43,7 @@ import { Action } from "../../../../src/dispatcher/actions";
|
||||
import { type ViewRoomPayload } from "../../../../src/dispatcher/payloads/ViewRoomPayload";
|
||||
import { TestSdkContext } from "../../TestSdkContext";
|
||||
import { RoomViewStore } from "../../../../src/stores/RoomViewStore";
|
||||
import { Container, WidgetLayoutStore } from "../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import WidgetStore from "../../../../src/stores/WidgetStore";
|
||||
import { WidgetType } from "../../../../src/widgets/WidgetType";
|
||||
import { SdkContextClass } from "../../../../src/contexts/SDKContext";
|
||||
@@ -234,7 +234,7 @@ describe("PipContainer", () => {
|
||||
// The return button should maximize the widget
|
||||
const moveSpy = jest.spyOn(WidgetLayoutStore.instance, "moveToContainer");
|
||||
await user.click(await screen.findByRole("button", { name: "Back" }));
|
||||
expect(moveSpy).toHaveBeenCalledWith(room, widget, Container.Center);
|
||||
expect(moveSpy).toHaveBeenCalledWith(room, widget, "center");
|
||||
|
||||
expect(screen.queryByRole("button", { name: "Leave" })).toBeNull();
|
||||
});
|
||||
|
||||
@@ -10,7 +10,7 @@ Please see LICENSE files in the repository root for full details.
|
||||
import React, { type JSX, type ComponentProps } from "react";
|
||||
import { screen, render } from "jest-matrix-react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { type Room, type MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import { MatrixWidgetType } from "matrix-widget-api";
|
||||
import {
|
||||
type ApprovalOpts,
|
||||
@@ -24,6 +24,10 @@ import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext
|
||||
import WidgetUtils from "../../../../../src/utils/WidgetUtils";
|
||||
import { ModuleRunner } from "../../../../../src/modules/ModuleRunner";
|
||||
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
||||
import { WidgetLayoutStore } from "../../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { mkStubRoom } from "../../../../test-utils/test-utils.ts";
|
||||
import { type RoomContextType } from "../../../../../src/contexts/RoomContext.ts";
|
||||
import { ScopedRoomContextProvider } from "../../../../../src/contexts/ScopedRoomContext.tsx";
|
||||
|
||||
describe("<WidgetContextMenu />", () => {
|
||||
const widgetId = "w1";
|
||||
@@ -44,8 +48,12 @@ describe("<WidgetContextMenu />", () => {
|
||||
|
||||
let mockClient: MatrixClient;
|
||||
|
||||
let room: Room;
|
||||
|
||||
let onFinished: () => void;
|
||||
|
||||
let roomContext: RoomContextType;
|
||||
|
||||
beforeEach(() => {
|
||||
onFinished = jest.fn();
|
||||
jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true);
|
||||
@@ -53,6 +61,13 @@ describe("<WidgetContextMenu />", () => {
|
||||
mockClient = {
|
||||
getUserId: jest.fn().mockReturnValue(userId),
|
||||
} as unknown as MatrixClient;
|
||||
|
||||
room = mkStubRoom(roomId, "Test Room", mockClient);
|
||||
|
||||
roomContext = {
|
||||
room,
|
||||
roomId,
|
||||
} as unknown as RoomContextType;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -62,7 +77,9 @@ describe("<WidgetContextMenu />", () => {
|
||||
function getComponent(props: Partial<ComponentProps<typeof WidgetContextMenu>> = {}): JSX.Element {
|
||||
return (
|
||||
<MatrixClientContext.Provider value={mockClient}>
|
||||
<WidgetContextMenu app={app} onFinished={onFinished} {...props} />
|
||||
<ScopedRoomContextProvider {...roomContext}>
|
||||
<WidgetContextMenu app={app} onFinished={onFinished} {...props} />
|
||||
</ScopedRoomContextProvider>
|
||||
</MatrixClientContext.Provider>
|
||||
);
|
||||
}
|
||||
@@ -89,4 +106,69 @@ describe("<WidgetContextMenu />", () => {
|
||||
expect(onFinished).toHaveBeenCalled();
|
||||
expect(SettingsStore.getValue("allowedWidgets", roomId)[eventId]).toBe(false);
|
||||
});
|
||||
|
||||
it("shows the move left button when the widget can be moved left", () => {
|
||||
// Place our widget second so it can move left but not right.
|
||||
jest.spyOn(WidgetLayoutStore.instance, "getContainerWidgets").mockReturnValue([
|
||||
{ id: "someOtherWidget", type: "m.custom", creatorUserId: userId, url: "" },
|
||||
{ id: widgetId, type: "m.custom", creatorUserId: userId, url: "" },
|
||||
]);
|
||||
|
||||
render(getComponent({ showUnpin: true }));
|
||||
|
||||
expect(screen.getByLabelText("Move left")).toBeInTheDocument();
|
||||
expect(screen.queryByLabelText("Move right")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("shows the move right button when the widget can be moved right", () => {
|
||||
// Place our widget first so it can move right but not left.
|
||||
jest.spyOn(WidgetLayoutStore.instance, "getContainerWidgets").mockReturnValue([
|
||||
{ id: widgetId, type: "m.custom", creatorUserId: userId, url: "" },
|
||||
{ id: "someOtherWidget", type: "m.custom", creatorUserId: userId, url: "" },
|
||||
]);
|
||||
|
||||
render(getComponent({ showUnpin: true }));
|
||||
|
||||
expect(screen.getByLabelText("Move right")).toBeInTheDocument();
|
||||
expect(screen.queryByLabelText("Move left")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("moves widget left when move left button is clicked", async () => {
|
||||
// Place our widget second so move left is visible.
|
||||
jest.spyOn(WidgetLayoutStore.instance, "getContainerWidgets").mockReturnValue([
|
||||
{ id: "someOtherWidget", type: "m.custom", creatorUserId: userId, url: "" },
|
||||
{ id: widgetId, type: "m.custom", creatorUserId: userId, url: "" },
|
||||
]);
|
||||
|
||||
// Mock moveWithinContainer to verify it's called with the correct arguments.
|
||||
const moveWithinContainerSpy = jest
|
||||
.spyOn(WidgetLayoutStore.instance, "moveWithinContainer")
|
||||
.mockImplementation();
|
||||
|
||||
render(getComponent({ showUnpin: true }));
|
||||
|
||||
await userEvent.click(screen.getByLabelText("Move left"));
|
||||
|
||||
expect(moveWithinContainerSpy).toHaveBeenCalledWith(room, "top", app, -1);
|
||||
expect(onFinished).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("moves widget right when move right button is clicked", async () => {
|
||||
// Place our widget first so move right is visible.
|
||||
jest.spyOn(WidgetLayoutStore.instance, "getContainerWidgets").mockReturnValue([
|
||||
{ id: widgetId, type: "m.custom", creatorUserId: userId, url: "" },
|
||||
{ id: "someOtherWidget", type: "m.custom", creatorUserId: userId, url: "" },
|
||||
]);
|
||||
|
||||
// Mock moveWithinContainer to verify it's called with the correct arguments.
|
||||
const moveWithinContainerSpy = jest
|
||||
.spyOn(WidgetLayoutStore.instance, "moveWithinContainer")
|
||||
.mockImplementation();
|
||||
|
||||
render(getComponent({ showUnpin: true }));
|
||||
await userEvent.click(screen.getByLabelText("Move right"));
|
||||
|
||||
expect(moveWithinContainerSpy).toHaveBeenCalledWith(room, "top", app, 1);
|
||||
expect(onFinished).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,7 +31,7 @@ import RightPanelStore from "../../../../../src/stores/right-panel/RightPanelSto
|
||||
import WidgetStore, { type IApp } from "../../../../../src/stores/WidgetStore";
|
||||
import ActiveWidgetStore from "../../../../../src/stores/ActiveWidgetStore";
|
||||
import AppTile from "../../../../../src/components/views/elements/AppTile";
|
||||
import { Container, WidgetLayoutStore } from "../../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { type Container, WidgetLayoutStore } from "../../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import AppsDrawer from "../../../../../src/components/views/rooms/AppsDrawer";
|
||||
import { ElementWidgetCapabilities } from "../../../../../src/stores/widgets/ElementWidgetCapabilities";
|
||||
import { ElementWidget, type WidgetMessaging } from "../../../../../src/stores/widgets/WidgetMessaging";
|
||||
@@ -303,7 +303,7 @@ describe("AppTile", () => {
|
||||
return {
|
||||
widgets: {
|
||||
1: {
|
||||
container: Container.Top,
|
||||
container: "top",
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -334,7 +334,7 @@ describe("AppTile", () => {
|
||||
mockSettings.mockRestore();
|
||||
act(() => {
|
||||
// Move widget to center
|
||||
WidgetLayoutStore.instance.moveToContainer(r1, app1, Container.Center);
|
||||
WidgetLayoutStore.instance.moveToContainer(r1, app1, "center");
|
||||
});
|
||||
|
||||
expect(renderResult.getByText("Example 1")).toBeInTheDocument();
|
||||
@@ -377,7 +377,7 @@ describe("AppTile", () => {
|
||||
);
|
||||
await waitForElementToBeRemoved(() => renderResult.queryByRole("progressbar"));
|
||||
await userEvent.click(renderResult.getByLabelText("Minimise"));
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, Container.Right);
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, "right");
|
||||
});
|
||||
|
||||
it("clicking 'maximise' should send the widget to the center", async () => {
|
||||
@@ -388,7 +388,7 @@ describe("AppTile", () => {
|
||||
);
|
||||
await waitForElementToBeRemoved(() => renderResult.queryByRole("progressbar"));
|
||||
await userEvent.click(renderResult.getByLabelText("Maximise"));
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, Container.Center);
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, "center");
|
||||
});
|
||||
|
||||
it("should render permission request", async () => {
|
||||
@@ -455,7 +455,7 @@ describe("AppTile", () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockImplementation(
|
||||
(room: Room | null, widget: IWidget, container: Container) => {
|
||||
return room === r1 && widget === app1 && container === Container.Center;
|
||||
return room === r1 && widget === app1 && container === "center";
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -472,7 +472,7 @@ describe("AppTile", () => {
|
||||
);
|
||||
await waitForElementToBeRemoved(() => renderResult.queryByRole("progressbar"));
|
||||
await userEvent.click(renderResult.getByLabelText("Un-maximise"));
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, Container.Top);
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(r1, app1, "top");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ import dispatcher from "../../../../../../src/dispatcher/dispatcher";
|
||||
import { CallStore } from "../../../../../../src/stores/CallStore";
|
||||
import { type Call } from "../../../../../../src/models/Call";
|
||||
import * as ShieldUtils from "../../../../../../src/utils/ShieldUtils";
|
||||
import { Container, WidgetLayoutStore } from "../../../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import MatrixClientContext from "../../../../../../src/contexts/MatrixClientContext";
|
||||
import { _t } from "../../../../../../src/languageHandler";
|
||||
import WidgetStore, { type IApp } from "../../../../../../src/stores/WidgetStore";
|
||||
@@ -504,7 +504,7 @@ describe("RoomHeader", () => {
|
||||
const videoButton = screen.getByRole("button", { name: "Video call" });
|
||||
expect(videoButton).not.toHaveAttribute("aria-disabled", "true");
|
||||
await user.click(videoButton);
|
||||
expect(spy).toHaveBeenCalledWith(room, widget, Container.Top);
|
||||
expect(spy).toHaveBeenCalledWith(room, widget, "top");
|
||||
});
|
||||
|
||||
it("disables calling if there's a jitsi call", () => {
|
||||
@@ -869,7 +869,7 @@ describe("RoomHeader", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("renders additionalButtons", async () => {
|
||||
it("renders legacy additionalButtons", async () => {
|
||||
const additionalButtons: ViewRoomOpts["buttons"] = [
|
||||
{
|
||||
icon: () => <>test-icon</>,
|
||||
@@ -878,11 +878,11 @@ describe("RoomHeader", () => {
|
||||
onClick: () => {},
|
||||
},
|
||||
];
|
||||
render(<RoomHeader room={room} additionalButtons={additionalButtons} />, getWrapper());
|
||||
render(<RoomHeader room={room} legacyAdditionalButtons={additionalButtons} />, getWrapper());
|
||||
expect(screen.getByRole("button", { name: "test-label" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("calls onClick-callback on additionalButtons", () => {
|
||||
it("calls onClick-callback on legacyAdditionalButtons", () => {
|
||||
const callback = jest.fn();
|
||||
const additionalButtons: ViewRoomOpts["buttons"] = [
|
||||
{
|
||||
@@ -893,7 +893,7 @@ describe("RoomHeader", () => {
|
||||
},
|
||||
];
|
||||
|
||||
render(<RoomHeader room={room} additionalButtons={additionalButtons} />, getWrapper());
|
||||
render(<RoomHeader room={room} legacyAdditionalButtons={additionalButtons} />, getWrapper());
|
||||
|
||||
const button = screen.getByRole("button", { name: "test-label" });
|
||||
const event = createEvent.click(button);
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
Copyright 2026 New Vector Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import React, { act } from "react";
|
||||
import { render, type RenderOptions } from "jest-matrix-react";
|
||||
import { type MatrixClient, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
import { stubClient } from "../../test-utils";
|
||||
import DMRoomMap from "../../../src/utils/DMRoomMap";
|
||||
import { SDKContext, SdkContextClass } from "../../../src/contexts/SDKContext";
|
||||
import { ScopedRoomContextProvider } from "../../../src/contexts/ScopedRoomContext";
|
||||
import RoomContext, { type RoomContextType } from "../../../src/contexts/RoomContext";
|
||||
import MatrixClientContext from "../../../src/contexts/MatrixClientContext";
|
||||
import { RoomView } from "../../../src/components/structures/RoomView";
|
||||
import { ModuleApi } from "../../../src/modules/Api";
|
||||
|
||||
describe("ExtrasApi", () => {
|
||||
let client: MatrixClient;
|
||||
let sdkContext: SdkContextClass;
|
||||
let room: Room;
|
||||
let roomContext: RoomContextType;
|
||||
|
||||
beforeEach(() => {
|
||||
client = stubClient();
|
||||
room = new Room("!test:room", client, "@alice:example.org", {
|
||||
pendingEventOrdering: PendingEventOrdering.Detached,
|
||||
});
|
||||
sdkContext = new SdkContextClass();
|
||||
sdkContext.client = client;
|
||||
jest.spyOn(sdkContext.roomViewStore, "getRoomId").mockReturnValue(room.roomId);
|
||||
|
||||
const mockRoomViewStore = new (class extends EventEmitter {
|
||||
isViewingCall = jest.fn().mockReturnValue(false);
|
||||
})();
|
||||
|
||||
roomContext = {
|
||||
...RoomContext,
|
||||
roomId: "!test:room",
|
||||
roomViewStore: mockRoomViewStore,
|
||||
} as unknown as RoomContextType;
|
||||
|
||||
DMRoomMap.setShared({
|
||||
getUserIdForRoomId: jest.fn(),
|
||||
getRoomIds: jest.fn().mockReturnValue(new Set()),
|
||||
} as unknown as DMRoomMap);
|
||||
});
|
||||
|
||||
function getWrapper(): RenderOptions {
|
||||
return {
|
||||
wrapper: ({ children }) => (
|
||||
<SDKContext.Provider value={sdkContext}>
|
||||
<ScopedRoomContextProvider {...roomContext}>
|
||||
<MatrixClientContext.Provider value={client}>{children}</MatrixClientContext.Provider>
|
||||
</ScopedRoomContextProvider>
|
||||
</SDKContext.Provider>
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
it("addRoomHeaderButtonCallback stores and uses the provided callback", () => {
|
||||
const callback = jest.fn();
|
||||
ModuleApi.instance.extras.addRoomHeaderButtonCallback(callback);
|
||||
|
||||
render(<RoomView />, getWrapper());
|
||||
|
||||
act(() => {
|
||||
sdkContext.roomViewStore.emit("update");
|
||||
});
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -23,7 +23,7 @@ import { registerMockModule } from "./MockModule";
|
||||
import defaultDispatcher from "../../../src/dispatcher/dispatcher";
|
||||
import { Action } from "../../../src/dispatcher/actions";
|
||||
import WidgetStore, { type IApp } from "../../../src/stores/WidgetStore";
|
||||
import { Container, WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import * as navigator from "../../../src/utils/permalinks/navigator.ts";
|
||||
|
||||
describe("ProxiedApiModule", () => {
|
||||
@@ -319,18 +319,18 @@ describe("ProxiedApiModule", () => {
|
||||
it("should return false if there is no room", () => {
|
||||
client.getRoom = jest.fn().mockReturnValue(null);
|
||||
|
||||
expect(api.isAppInContainer(app, Container.Top, roomId)).toBe(false);
|
||||
expect(api.isAppInContainer(app, "top", roomId)).toBe(false);
|
||||
expect(WidgetLayoutStore.instance.isInContainer).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should return false if the app is not in the container", () => {
|
||||
jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockReturnValue(false);
|
||||
expect(api.isAppInContainer(app, Container.Top, roomId)).toBe(false);
|
||||
expect(api.isAppInContainer(app, "top", roomId)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return true if the app is in the container", () => {
|
||||
jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockReturnValue(true);
|
||||
expect(api.isAppInContainer(app, Container.Top, roomId)).toBe(true);
|
||||
expect(api.isAppInContainer(app, "top", roomId)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -350,7 +350,7 @@ describe("ProxiedApiModule", () => {
|
||||
|
||||
it("should not move if there is no room", () => {
|
||||
client.getRoom = jest.fn().mockReturnValue(null);
|
||||
api.moveAppToContainer(app, Container.Top, roomId);
|
||||
api.moveAppToContainer(app, "top", roomId);
|
||||
expect(WidgetLayoutStore.instance.moveToContainer).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -358,8 +358,8 @@ describe("ProxiedApiModule", () => {
|
||||
const room = mkRoom(client, roomId);
|
||||
client.getRoom = jest.fn().mockReturnValue(room);
|
||||
|
||||
api.moveAppToContainer(app, Container.Top, roomId);
|
||||
expect(WidgetLayoutStore.instance.moveToContainer).toHaveBeenCalledWith(room, app, Container.Top);
|
||||
api.moveAppToContainer(app, "top", roomId);
|
||||
expect(WidgetLayoutStore.instance.moveToContainer).toHaveBeenCalledWith(room, app, "top");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
Copyright 2026 Element Creations Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
|
||||
import type { IWidget } from "matrix-widget-api";
|
||||
import type { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { WidgetApi } from "../../../src/modules/WidgetApi";
|
||||
import WidgetStore from "../../../src/stores/WidgetStore";
|
||||
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { stubClient } from "../../test-utils";
|
||||
|
||||
describe("WidgetApi", () => {
|
||||
let client: MatrixClient;
|
||||
let api: WidgetApi;
|
||||
|
||||
const mkWidget = (overrides: Partial<IWidget> = {}): IWidget => ({
|
||||
id: "widget-id",
|
||||
creatorUserId: "@alice:example.org",
|
||||
type: "m.custom",
|
||||
url: "https://example.org/widget",
|
||||
...overrides,
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
client = stubClient();
|
||||
|
||||
api = new WidgetApi();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("getWidgetsInRoom returns widgets from WidgetStore", () => {
|
||||
const widgets = [{ id: "w1" }, { id: "w2" }] as unknown as IWidget[];
|
||||
const getAppsSpy = jest.spyOn(WidgetStore.instance, "getApps").mockReturnValue(widgets as any);
|
||||
|
||||
expect(api.getWidgetsInRoom("!room:example.org")).toBe(widgets);
|
||||
expect(getAppsSpy).toHaveBeenCalledWith("!room:example.org");
|
||||
});
|
||||
|
||||
it("getAppAvatarUrl returns the http avatar URL for a widget if it has one", () => {
|
||||
const app = {
|
||||
...mkWidget(),
|
||||
roomId: "!room:example.org",
|
||||
avatar_url: "mxc://example.org/avatar",
|
||||
} as unknown as IWidget;
|
||||
|
||||
mocked(client.getHomeserverUrl).mockReturnValue("https://hs.example.org");
|
||||
const avatarUrl = api.getAppAvatarUrl(app, 32, 32, "scale");
|
||||
|
||||
expect(avatarUrl).toContain("https://hs.example.org/_matrix/media/");
|
||||
expect(avatarUrl).toContain("/thumbnail/example.org/avatar");
|
||||
expect(avatarUrl).toContain("width=32");
|
||||
expect(avatarUrl).toContain("height=32");
|
||||
expect(avatarUrl).toContain("method=scale");
|
||||
});
|
||||
|
||||
it("getAppAvatarUrl returns null when app is not an app widget", () => {
|
||||
const nonAppWidget = {
|
||||
...mkWidget(),
|
||||
avatar_url: "mxc://example.org/avatar",
|
||||
};
|
||||
|
||||
expect(api.getAppAvatarUrl(nonAppWidget)).toBeNull();
|
||||
});
|
||||
|
||||
it("getAppAvatarUrl returns null when app has no avatar URL", () => {
|
||||
const appWithoutAvatar = {
|
||||
...mkWidget(),
|
||||
roomId: "!room:example.org",
|
||||
} as unknown as IWidget;
|
||||
|
||||
expect(api.getAppAvatarUrl(appWithoutAvatar)).toBeNull();
|
||||
});
|
||||
|
||||
it("isAppInContainer returns false when room is not found", () => {
|
||||
const isInContainerSpy = jest.spyOn(WidgetLayoutStore.instance, "isInContainer");
|
||||
const app = mkWidget();
|
||||
|
||||
mocked(client.getRoom).mockReturnValue(null);
|
||||
expect(api.isAppInContainer(app, "top", "!missing:example.org")).toBe(false);
|
||||
expect(isInContainerSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("isAppInContainer delegates to WidgetLayoutStore when room exists", () => {
|
||||
const room = { roomId: "!room:example.org" } as Room;
|
||||
mocked(client.getRoom).mockReturnValue(room);
|
||||
const isInContainerSpy = jest.spyOn(WidgetLayoutStore.instance, "isInContainer").mockReturnValue(true);
|
||||
const app = mkWidget();
|
||||
|
||||
expect(api.isAppInContainer(app, "top", room.roomId)).toBe(true);
|
||||
expect(isInContainerSpy).toHaveBeenCalledWith(room, app, "top");
|
||||
});
|
||||
|
||||
it("moveAppToContainer does nothing when room is not found", () => {
|
||||
const moveToContainerSpy = jest.spyOn(WidgetLayoutStore.instance, "moveToContainer");
|
||||
const app = mkWidget();
|
||||
|
||||
mocked(client.getRoom).mockReturnValue(null);
|
||||
api.moveAppToContainer(app, "right", "!missing:example.org");
|
||||
|
||||
expect(moveToContainerSpy).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("moveAppToContainer delegates to WidgetLayoutStore when room exists", () => {
|
||||
const room = { roomId: "!room:example.org" } as Room;
|
||||
mocked(client.getRoom).mockReturnValue(room);
|
||||
const moveToContainerSpy = jest.spyOn(WidgetLayoutStore.instance, "moveToContainer").mockImplementation();
|
||||
const app = mkWidget();
|
||||
|
||||
api.moveAppToContainer(app, "right", room.roomId);
|
||||
|
||||
expect(moveToContainerSpy).toHaveBeenCalledWith(room, app, "right");
|
||||
});
|
||||
});
|
||||
@@ -10,7 +10,7 @@ import { type MatrixClient, type Room } from "matrix-js-sdk/src/matrix";
|
||||
import { mocked } from "jest-mock";
|
||||
|
||||
import WidgetStore, { type IApp } from "../../../src/stores/WidgetStore";
|
||||
import { Container, WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { stubClient } from "../../test-utils";
|
||||
import defaultDispatcher from "../../../src/dispatcher/dispatcher";
|
||||
import SettingsStore from "../../../src/settings/SettingsStore";
|
||||
@@ -74,14 +74,14 @@ describe("WidgetLayoutStore", () => {
|
||||
|
||||
it("all widgets should be in the right container by default", () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Right).length).toStrictEqual(mockApps.length);
|
||||
expect(store.getContainerWidgets(mockRoom, "right").length).toStrictEqual(mockApps.length);
|
||||
});
|
||||
|
||||
it("add widget to top container", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toStrictEqual([mockApps[0]]);
|
||||
expect(store.getContainerHeight(mockRoom, Container.Top)).toBeNull();
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toStrictEqual([mockApps[0]]);
|
||||
expect(store.getContainerHeight(mockRoom, "top")).toBeNull();
|
||||
});
|
||||
|
||||
it("ordering of top container widgets should be consistent even if no index specified", async () => {
|
||||
@@ -97,71 +97,71 @@ describe("WidgetLayoutStore", () => {
|
||||
};
|
||||
|
||||
store.recalculateRoom(mockRoom);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toStrictEqual([mockApps[0], mockApps[1]]);
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toStrictEqual([mockApps[0], mockApps[1]]);
|
||||
});
|
||||
|
||||
it("add three widgets to top container", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[2], Container.Top);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, Container.Top))).toEqual(
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[2], "top");
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, "top"))).toEqual(
|
||||
new Set([mockApps[0], mockApps[1], mockApps[2]]),
|
||||
);
|
||||
});
|
||||
|
||||
it("cannot add more than three widgets to top container", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[2], Container.Top);
|
||||
expect(store.canAddToContainer(mockRoom, Container.Top)).toEqual(false);
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[2], "top");
|
||||
expect(store.canAddToContainer(mockRoom, "top")).toEqual(false);
|
||||
});
|
||||
|
||||
it("remove pins when maximising (other widget)", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[2], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[3], Container.Center);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, Container.Right))).toEqual(
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[2], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[3], "center");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, "right"))).toEqual(
|
||||
new Set([mockApps[0], mockApps[1], mockApps[2]]),
|
||||
);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([mockApps[3]]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([mockApps[3]]);
|
||||
});
|
||||
|
||||
it("remove pins when maximising (one of the pinned widgets)", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[2], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Center);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([mockApps[0]]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, Container.Right))).toEqual(
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[2], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[0], "center");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([mockApps[0]]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, "right"))).toEqual(
|
||||
new Set([mockApps[1], mockApps[2], mockApps[3]]),
|
||||
);
|
||||
});
|
||||
|
||||
it("remove maximised when pinning (other widget)", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Center);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([mockApps[1]]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, Container.Right))).toEqual(
|
||||
store.moveToContainer(mockRoom, mockApps[0], "center");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([mockApps[1]]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, "right"))).toEqual(
|
||||
new Set([mockApps[2], mockApps[3], mockApps[0]]),
|
||||
);
|
||||
});
|
||||
|
||||
it("remove maximised when pinning (same widget)", async () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Center);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([mockApps[0]]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, Container.Right))).toEqual(
|
||||
store.moveToContainer(mockRoom, mockApps[0], "center");
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([mockApps[0]]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([]);
|
||||
expect(new Set(store.getContainerWidgets(mockRoom, "right"))).toEqual(
|
||||
new Set([mockApps[2], mockApps[3], mockApps[1]]),
|
||||
);
|
||||
});
|
||||
@@ -171,9 +171,9 @@ describe("WidgetLayoutStore", () => {
|
||||
await store.start();
|
||||
|
||||
expect(roomUpdateListener).toHaveBeenCalled();
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "right")).toEqual([
|
||||
mockApps[0],
|
||||
mockApps[1],
|
||||
mockApps[2],
|
||||
@@ -190,58 +190,50 @@ describe("WidgetLayoutStore", () => {
|
||||
));
|
||||
store.recalculateRoom(mockRoom);
|
||||
expect(roomUpdateListener).toHaveBeenCalled();
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "right")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should clear the layout if the client is not viable", () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
defaultDispatcher.dispatch({ action: Action.ClientNotViable }, true);
|
||||
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "center")).toEqual([]);
|
||||
expect(store.getContainerWidgets(mockRoom, "right")).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return the expected resizer distributions", () => {
|
||||
// this only works for top widgets
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
expect(store.getResizerDistributions(mockRoom, Container.Top)).toEqual(["50.0%"]);
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
expect(store.getResizerDistributions(mockRoom, "top")).toEqual(["50.0%"]);
|
||||
});
|
||||
|
||||
it("should set and return container height", () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.setContainerHeight(mockRoom, Container.Top, 23);
|
||||
expect(store.getContainerHeight(mockRoom, Container.Top)).toBe(23);
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.setContainerHeight(mockRoom, "top", 23);
|
||||
expect(store.getContainerHeight(mockRoom, "top")).toBe(23);
|
||||
});
|
||||
|
||||
it("should move a widget within a container", () => {
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[1], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[2], Container.Top);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toStrictEqual([
|
||||
mockApps[0],
|
||||
mockApps[1],
|
||||
mockApps[2],
|
||||
]);
|
||||
store.moveWithinContainer(mockRoom, Container.Top, mockApps[0], 1);
|
||||
expect(store.getContainerWidgets(mockRoom, Container.Top)).toStrictEqual([
|
||||
mockApps[1],
|
||||
mockApps[0],
|
||||
mockApps[2],
|
||||
]);
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[1], "top");
|
||||
store.moveToContainer(mockRoom, mockApps[2], "top");
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toStrictEqual([mockApps[0], mockApps[1], mockApps[2]]);
|
||||
store.moveWithinContainer(mockRoom, "top", mockApps[0], 1);
|
||||
expect(store.getContainerWidgets(mockRoom, "top")).toStrictEqual([mockApps[1], mockApps[0], mockApps[2]]);
|
||||
});
|
||||
|
||||
it("should copy the layout to the room", async () => {
|
||||
await store.start();
|
||||
store.recalculateRoom(mockRoom);
|
||||
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
||||
store.moveToContainer(mockRoom, mockApps[0], "top");
|
||||
store.copyLayoutToRoom(mockRoom);
|
||||
|
||||
expect(mocked(client.sendStateEvent).mock.calls).toMatchInlineSnapshot(`
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
import { stubClient } from "../../test-utils";
|
||||
import WidgetUtils from "../../../src/utils/WidgetUtils";
|
||||
import { type IApp } from "../../../src/utils/WidgetUtils-types";
|
||||
import { Container, WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import * as livestream from "../../../src/Livestream";
|
||||
import Modal from "../../../src/Modal";
|
||||
import SettingsStore from "../../../src/settings/SettingsStore";
|
||||
@@ -138,12 +138,7 @@ describe("WidgetContextMenuViewModel", () => {
|
||||
const vm = new WidgetContextMenuViewModel(props);
|
||||
vm.onMoveButton(1);
|
||||
|
||||
expect(WidgetLayoutStore.instance.moveWithinContainer).toHaveBeenCalledWith(
|
||||
props.room,
|
||||
Container.Top,
|
||||
props.app,
|
||||
1,
|
||||
);
|
||||
expect(WidgetLayoutStore.instance.moveWithinContainer).toHaveBeenCalledWith(props.room, "top", props.app, 1);
|
||||
expect(props.onFinished).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import { WidgetPipViewModel } from "../../../src/viewmodels/room/WidgetPipViewMo
|
||||
import WidgetStore, { type IApp } from "../../../src/stores/WidgetStore";
|
||||
import defaultDispatcher from "../../../src/dispatcher/dispatcher";
|
||||
import { Action } from "../../../src/dispatcher/actions";
|
||||
import { Container, WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { WidgetLayoutStore } from "../../../src/stores/widgets/WidgetLayoutStore";
|
||||
import { CallStore, CallStoreEvent } from "../../../src/stores/CallStore";
|
||||
import { type Call } from "../../../src/models/Call";
|
||||
|
||||
@@ -96,7 +96,7 @@ describe("WidgetPipViewModel", () => {
|
||||
|
||||
vm.setViewingRoom(true);
|
||||
vm.onBackClick(createBackClickEvent());
|
||||
expect(moveSpy).toHaveBeenCalledWith(room, widget, Container.Center);
|
||||
expect(moveSpy).toHaveBeenCalledWith(room, widget, "center");
|
||||
|
||||
moveSpy.mockClear();
|
||||
vm.setViewingRoom(false);
|
||||
|
||||
Reference in New Issue
Block a user