2021-11-16 14:43:18 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-11-16 14:43:18 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-11-16 14:43:18 +00:00
|
|
|
*/
|
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { mocked } from "jest-mock";
|
2021-12-09 09:10:23 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
import WidgetStore, { IApp } from "../../src/stores/WidgetStore";
|
|
|
|
import { Container, WidgetLayoutStore } from "../../src/stores/widgets/WidgetLayoutStore";
|
|
|
|
import { stubClient } from "../test-utils";
|
2023-01-18 14:49:34 +00:00
|
|
|
import defaultDispatcher from "../../src/dispatcher/dispatcher";
|
2023-03-09 10:18:23 +00:00
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
2021-11-16 14:43:18 +00:00
|
|
|
|
|
|
|
// setup test env values
|
|
|
|
const roomId = "!room:server";
|
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
describe("WidgetLayoutStore", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let store: WidgetLayoutStore;
|
|
|
|
let roomUpdateListener: (event: string) => void;
|
|
|
|
let mockApps: IApp[];
|
2024-07-17 13:51:42 +00:00
|
|
|
let mockRoom: Room;
|
|
|
|
let layoutEventContent: Record<string, any> | null;
|
2021-11-16 14:43:18 +00:00
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
beforeEach(() => {
|
2024-07-17 13:51:42 +00:00
|
|
|
layoutEventContent = null;
|
|
|
|
mockRoom = <Room>{
|
|
|
|
roomId: roomId,
|
|
|
|
currentState: {
|
|
|
|
getStateEvents: (_l, _x) => {
|
|
|
|
return {
|
|
|
|
getId: () => "$layoutEventId",
|
|
|
|
getContent: () => layoutEventContent,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
mockApps = [
|
|
|
|
<IApp>{ roomId: roomId, id: "1" },
|
|
|
|
<IApp>{ roomId: roomId, id: "2" },
|
|
|
|
<IApp>{ roomId: roomId, id: "3" },
|
|
|
|
<IApp>{ roomId: roomId, id: "4" },
|
|
|
|
];
|
2021-11-16 14:43:18 +00:00
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
// fake the WidgetStore.instance to just return an object with `getApps`
|
|
|
|
jest.spyOn(WidgetStore, "instance", "get").mockReturnValue({
|
|
|
|
on: jest.fn(),
|
|
|
|
off: jest.fn(),
|
|
|
|
getApps: () => mockApps,
|
|
|
|
} as unknown as WidgetStore);
|
2024-07-17 09:58:07 +00:00
|
|
|
|
|
|
|
SettingsStore.reset();
|
2023-01-18 14:49:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
// we need to init a client so it does not error, when asking for DeviceStorage handlers (SettingsStore.setValue("Widgets.layout"))
|
|
|
|
client = stubClient();
|
2021-11-16 14:43:18 +00:00
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
roomUpdateListener = jest.fn();
|
|
|
|
// @ts-ignore bypass private ctor for tests
|
|
|
|
store = new WidgetLayoutStore();
|
|
|
|
store.addListener(`update_${roomId}`, roomUpdateListener);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
store.removeListener(`update_${roomId}`, roomUpdateListener);
|
|
|
|
});
|
2021-11-16 14:43:18 +00:00
|
|
|
|
2023-01-18 14:49:34 +00:00
|
|
|
it("all widgets should be in the right container by default", () => {
|
2021-11-16 14:43:18 +00:00
|
|
|
store.recalculateRoom(mockRoom);
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Right).length).toStrictEqual(mockApps.length);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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]]);
|
2023-01-18 14:49:34 +00:00
|
|
|
expect(store.getContainerHeight(mockRoom, Container.Top)).toBeNull();
|
2021-11-16 14:43:18 +00:00
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2024-07-17 13:51:42 +00:00
|
|
|
it("ordering of top container widgets should be consistent even if no index specified", async () => {
|
|
|
|
layoutEventContent = {
|
|
|
|
widgets: {
|
|
|
|
"1": {
|
|
|
|
container: "top",
|
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
container: "top",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
store.recalculateRoom(mockRoom);
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Top)).toStrictEqual([mockApps[0], mockApps[1]]);
|
|
|
|
});
|
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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(
|
|
|
|
new Set([mockApps[0], mockApps[1], mockApps[2]]),
|
|
|
|
);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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(
|
|
|
|
new Set([mockApps[0], mockApps[1], mockApps[2]]),
|
|
|
|
);
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([mockApps[3]]);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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(
|
|
|
|
new Set([mockApps[1], mockApps[2], mockApps[3]]),
|
|
|
|
);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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(
|
|
|
|
new Set([mockApps[2], mockApps[3], mockApps[0]]),
|
|
|
|
);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
2021-11-16 14:43:18 +00:00
|
|
|
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(
|
|
|
|
new Set([mockApps[2], mockApps[3], mockApps[1]]),
|
|
|
|
);
|
|
|
|
});
|
2023-01-18 14:49:34 +00:00
|
|
|
|
|
|
|
it("should recalculate all rooms when the client is ready", async () => {
|
|
|
|
mocked(client.getVisibleRooms).mockReturnValue([mockRoom]);
|
2024-10-08 09:12:21 +00:00
|
|
|
await store.start(client);
|
2023-01-18 14:49:34 +00:00
|
|
|
|
|
|
|
expect(roomUpdateListener).toHaveBeenCalled();
|
2024-07-17 09:58:07 +00:00
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
2023-01-18 14:49:34 +00:00
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
2024-07-17 09:58:07 +00:00
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Right)).toEqual([
|
|
|
|
mockApps[0],
|
|
|
|
mockApps[1],
|
|
|
|
mockApps[2],
|
|
|
|
mockApps[3],
|
|
|
|
]);
|
2023-01-18 14:49:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear the layout and emit an update if there are no longer apps in the room", () => {
|
|
|
|
store.recalculateRoom(mockRoom);
|
|
|
|
mocked(roomUpdateListener).mockClear();
|
|
|
|
|
|
|
|
jest.spyOn(WidgetStore, "instance", "get").mockReturnValue(<WidgetStore>(
|
|
|
|
({ getApps: (): IApp[] => [] } as unknown as WidgetStore)
|
|
|
|
));
|
|
|
|
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([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should clear the layout if the client is not viable", () => {
|
|
|
|
store.recalculateRoom(mockRoom);
|
|
|
|
defaultDispatcher.dispatch(
|
|
|
|
{
|
|
|
|
action: "on_client_not_viable",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Top)).toEqual([]);
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.Center)).toEqual([]);
|
|
|
|
expect(store.getContainerWidgets(mockRoom, Container.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%"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
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],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should copy the layout to the room", async () => {
|
2024-10-08 09:12:21 +00:00
|
|
|
await store.start(client);
|
2023-01-18 14:49:34 +00:00
|
|
|
store.recalculateRoom(mockRoom);
|
|
|
|
store.moveToContainer(mockRoom, mockApps[0], Container.Top);
|
|
|
|
store.copyLayoutToRoom(mockRoom);
|
|
|
|
|
|
|
|
expect(mocked(client.sendStateEvent).mock.calls).toMatchInlineSnapshot(`
|
|
|
|
[
|
|
|
|
[
|
|
|
|
"!room:server",
|
|
|
|
"io.element.widgets.layout",
|
|
|
|
{
|
|
|
|
"widgets": {
|
|
|
|
"1": {
|
|
|
|
"container": "top",
|
2024-07-17 09:58:07 +00:00
|
|
|
"height": undefined,
|
|
|
|
"index": 0,
|
|
|
|
"width": 100,
|
2023-01-18 14:49:34 +00:00
|
|
|
},
|
|
|
|
"2": {
|
2024-07-17 09:58:07 +00:00
|
|
|
"container": "right",
|
2023-01-18 14:49:34 +00:00
|
|
|
},
|
|
|
|
"3": {
|
2024-07-17 09:58:07 +00:00
|
|
|
"container": "right",
|
2023-01-18 14:49:34 +00:00
|
|
|
},
|
|
|
|
"4": {
|
|
|
|
"container": "right",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
],
|
|
|
|
]
|
|
|
|
`);
|
|
|
|
});
|
2023-03-09 10:18:23 +00:00
|
|
|
|
|
|
|
it("Can call onNotReady before onReady has been called", () => {
|
|
|
|
// Just to quieten SonarCloud :-(
|
|
|
|
|
|
|
|
// @ts-ignore bypass private ctor for tests
|
|
|
|
const store = new WidgetLayoutStore();
|
|
|
|
// @ts-ignore calling private method
|
|
|
|
store.onNotReady();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when feature_dynamic_room_predecessors is not enabled", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("passes the flag in to getVisibleRooms", async () => {
|
|
|
|
mocked(client.getVisibleRooms).mockRestore();
|
|
|
|
mocked(client.getVisibleRooms).mockReturnValue([]);
|
|
|
|
// @ts-ignore bypass private ctor for tests
|
|
|
|
const store = new WidgetLayoutStore();
|
2024-10-08 09:12:21 +00:00
|
|
|
await store.start(client);
|
2023-03-09 10:18:23 +00:00
|
|
|
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when feature_dynamic_room_predecessors is enabled", () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation(
|
|
|
|
(settingName) => settingName === "feature_dynamic_room_predecessors",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("passes the flag in to getVisibleRooms", async () => {
|
|
|
|
mocked(client.getVisibleRooms).mockRestore();
|
|
|
|
mocked(client.getVisibleRooms).mockReturnValue([]);
|
|
|
|
// @ts-ignore bypass private ctor for tests
|
|
|
|
const store = new WidgetLayoutStore();
|
2024-10-08 09:12:21 +00:00
|
|
|
await store.start(client);
|
2023-03-09 10:18:23 +00:00
|
|
|
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
|
|
|
|
});
|
|
|
|
});
|
2021-11-16 14:43:18 +00:00
|
|
|
});
|