2022-05-06 09:09:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
|
|
|
import ReactTestUtils from "react-dom/test-utils";
|
|
|
|
import ReactDOM from "react-dom";
|
|
|
|
import { PendingEventOrdering, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import * as TestUtils from "../../../test-utils";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
|
|
|
import dis from "../../../../src/dispatcher/dispatcher";
|
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { DefaultTagID } from "../../../../src/stores/room-list/models";
|
2021-08-11 15:12:08 +00:00
|
|
|
import RoomListStore, { RoomListStoreClass } from "../../../../src/stores/room-list/RoomListStore";
|
2020-07-10 04:40:34 +00:00
|
|
|
import RoomListLayoutStore from "../../../../src/stores/room-list/RoomListLayoutStore";
|
2022-03-02 21:09:24 +00:00
|
|
|
import RoomList from "../../../../src/components/views/rooms/RoomList";
|
|
|
|
import RoomSublist from "../../../../src/components/views/rooms/RoomSublist";
|
2022-12-06 09:56:29 +00:00
|
|
|
import { RoomTile } from "../../../../src/components/views/rooms/RoomTile";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
|
|
|
|
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2018-05-03 17:04:01 +00:00
|
|
|
function generateRoomId() {
|
2022-12-12 11:24:14 +00:00
|
|
|
return "!" + Math.random().toString().slice(2, 10) + ":domain";
|
2018-05-03 17:04:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("RoomList", () => {
|
2018-08-02 09:40:25 +00:00
|
|
|
function createRoom(opts) {
|
2021-04-21 22:45:21 +00:00
|
|
|
const room = new Room(generateRoomId(), MatrixClientPeg.get(), client.getUserId(), {
|
|
|
|
// The room list now uses getPendingEvents(), so we need a detached ordering.
|
2022-05-09 13:24:12 +00:00
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
2021-04-21 22:45:21 +00:00
|
|
|
});
|
2018-08-02 09:40:25 +00:00
|
|
|
if (opts) {
|
|
|
|
Object.assign(room, opts);
|
|
|
|
}
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:41:35 +00:00
|
|
|
let parentDiv = null;
|
|
|
|
let root = null;
|
2022-12-12 11:24:14 +00:00
|
|
|
const myUserId = "@me:domain";
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const movingRoomId = "!someroomid";
|
2022-05-09 13:24:12 +00:00
|
|
|
let movingRoom: Room | undefined;
|
|
|
|
let otherRoom: Room | undefined;
|
2018-05-03 17:04:01 +00:00
|
|
|
|
2022-05-09 13:24:12 +00:00
|
|
|
let myMember: RoomMember | undefined;
|
|
|
|
let myOtherMember: RoomMember | undefined;
|
|
|
|
|
|
|
|
const client = getMockClientWithEventEmitter({
|
|
|
|
...mockClientMethodsUser(myUserId),
|
|
|
|
getRooms: jest.fn(),
|
|
|
|
getVisibleRooms: jest.fn(),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
onKeyDown: jest.fn(),
|
|
|
|
onFocus: jest.fn(),
|
|
|
|
onBlur: jest.fn(),
|
|
|
|
onResize: jest.fn(),
|
|
|
|
resizeNotifier: {} as unknown as ResizeNotifier,
|
|
|
|
isMinimized: false,
|
2022-12-12 11:24:14 +00:00
|
|
|
activeSpace: "",
|
2022-05-09 13:24:12 +00:00
|
|
|
};
|
2018-05-03 17:04:01 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
beforeEach(async function (done) {
|
2020-07-17 21:10:30 +00:00
|
|
|
RoomListStoreClass.TEST_MODE = true;
|
2022-05-09 13:24:12 +00:00
|
|
|
jest.clearAllMocks();
|
2020-07-10 17:05:56 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
client.credentials = { userId: myUserId };
|
2018-05-03 14:41:35 +00:00
|
|
|
|
|
|
|
DMRoomMap.makeShared();
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
parentDiv = document.createElement("div");
|
2018-05-03 14:41:35 +00:00
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
|
|
|
|
const WrappedRoomList = TestUtils.wrapInMatrixClientContext(RoomList);
|
2022-12-12 11:24:14 +00:00
|
|
|
root = ReactDOM.render(<WrappedRoomList {...defaultProps} />, parentDiv);
|
2018-05-03 14:41:35 +00:00
|
|
|
ReactTestUtils.findRenderedComponentWithType(root, RoomList);
|
2018-05-03 17:04:01 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
movingRoom = createRoom({ name: "Moving room" });
|
2018-12-25 02:13:09 +00:00
|
|
|
expect(movingRoom.roomId).not.toBe(null);
|
2018-05-03 17:04:01 +00:00
|
|
|
|
|
|
|
// Mock joined member
|
|
|
|
myMember = new RoomMember(movingRoomId, myUserId);
|
2022-12-12 11:24:14 +00:00
|
|
|
myMember.membership = "join";
|
|
|
|
movingRoom.updateMyMembership("join");
|
|
|
|
movingRoom.getMember = (userId) =>
|
|
|
|
({
|
|
|
|
[client.credentials.userId]: myMember,
|
|
|
|
}[userId]);
|
|
|
|
|
|
|
|
otherRoom = createRoom({ name: "Other room" });
|
2018-05-03 17:04:01 +00:00
|
|
|
myOtherMember = new RoomMember(otherRoom.roomId, myUserId);
|
2022-12-12 11:24:14 +00:00
|
|
|
myOtherMember.membership = "join";
|
|
|
|
otherRoom.updateMyMembership("join");
|
|
|
|
otherRoom.getMember = (userId) =>
|
|
|
|
({
|
|
|
|
[client.credentials.userId]: myOtherMember,
|
|
|
|
}[userId]);
|
2018-05-03 17:04:01 +00:00
|
|
|
|
|
|
|
// Mock the matrix client
|
2022-05-09 13:24:12 +00:00
|
|
|
const mockRooms = [
|
2018-05-03 17:04:01 +00:00
|
|
|
movingRoom,
|
|
|
|
otherRoom,
|
2022-12-12 11:24:14 +00:00
|
|
|
createRoom({ tags: { "m.favourite": { order: 0.1 } }, name: "Some other room" }),
|
|
|
|
createRoom({ tags: { "m.favourite": { order: 0.2 } }, name: "Some other room 2" }),
|
|
|
|
createRoom({ tags: { "m.lowpriority": {} }, name: "Some unimportant room" }),
|
|
|
|
createRoom({ tags: { "custom.tag": {} }, name: "Some room customly tagged" }),
|
2018-05-03 17:04:01 +00:00
|
|
|
];
|
2022-05-09 13:24:12 +00:00
|
|
|
client.getRooms.mockReturnValue(mockRooms);
|
|
|
|
client.getVisibleRooms.mockReturnValue(mockRooms);
|
2018-05-03 17:04:01 +00:00
|
|
|
|
|
|
|
const roomMap = {};
|
|
|
|
client.getRooms().forEach((r) => {
|
|
|
|
roomMap[r.roomId] = r;
|
|
|
|
});
|
|
|
|
|
2022-05-09 13:24:12 +00:00
|
|
|
client.getRoom.mockImplementation((roomId) => roomMap[roomId]);
|
2020-07-07 21:45:59 +00:00
|
|
|
|
|
|
|
// Now that everything has been set up, prepare and update the store
|
2022-08-09 19:46:59 +00:00
|
|
|
await (RoomListStore.instance as RoomListStoreClass).makeReady(client);
|
2020-07-07 21:45:59 +00:00
|
|
|
|
|
|
|
done();
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2020-07-07 21:45:59 +00:00
|
|
|
afterEach(async (done) => {
|
2018-05-03 14:41:35 +00:00
|
|
|
if (parentDiv) {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
parentDiv = null;
|
|
|
|
}
|
|
|
|
|
2020-07-10 04:40:34 +00:00
|
|
|
await RoomListLayoutStore.instance.resetLayouts();
|
2022-08-09 19:46:59 +00:00
|
|
|
await (RoomListStore.instance as RoomListStoreClass).resetStore();
|
2018-05-03 14:41:35 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-05-03 17:04:01 +00:00
|
|
|
function expectRoomInSubList(room, subListTest) {
|
2022-03-02 21:09:24 +00:00
|
|
|
const subLists = ReactTestUtils.scryRenderedComponentsWithType(root, RoomSublist);
|
2018-05-03 17:04:01 +00:00
|
|
|
const containingSubList = subLists.find(subListTest);
|
|
|
|
|
|
|
|
let expectedRoomTile;
|
|
|
|
try {
|
|
|
|
const roomTiles = ReactTestUtils.scryRenderedComponentsWithType(containingSubList, RoomTile);
|
2021-06-29 12:11:58 +00:00
|
|
|
console.info({ roomTiles: roomTiles.length });
|
2018-05-03 17:04:01 +00:00
|
|
|
expectedRoomTile = roomTiles.find((tile) => tile.props.room === room);
|
|
|
|
} catch (err) {
|
|
|
|
// truncate the error message because it's spammy
|
2022-12-12 11:24:14 +00:00
|
|
|
err.message =
|
|
|
|
"Error finding RoomTile for " +
|
|
|
|
room.roomId +
|
|
|
|
" in " +
|
|
|
|
subListTest +
|
|
|
|
": " +
|
|
|
|
err.message.split("componentType")[0] +
|
|
|
|
"...";
|
2018-05-03 17:04:01 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2018-12-25 02:13:09 +00:00
|
|
|
expect(expectedRoomTile).toBeTruthy();
|
2018-05-03 17:04:01 +00:00
|
|
|
expect(expectedRoomTile.props.room).toBe(room);
|
|
|
|
}
|
|
|
|
|
2020-07-07 21:06:06 +00:00
|
|
|
function expectCorrectMove(oldTagId, newTagId) {
|
|
|
|
const getTagSubListTest = (tagId) => {
|
|
|
|
return (s) => s.props.tagId === tagId;
|
2018-05-03 17:04:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Default to finding the destination sublist with newTag
|
2020-07-07 21:06:06 +00:00
|
|
|
const destSubListTest = getTagSubListTest(newTagId);
|
|
|
|
const srcSubListTest = getTagSubListTest(oldTagId);
|
2018-05-03 17:04:01 +00:00
|
|
|
|
|
|
|
// Set up the room that will be moved such that it has the correct state for a room in
|
2020-07-07 21:06:06 +00:00
|
|
|
// the section for oldTagId
|
|
|
|
if (oldTagId === DefaultTagID.Favourite || oldTagId === DefaultTagID.LowPriority) {
|
2021-06-29 12:11:58 +00:00
|
|
|
movingRoom.tags = { [oldTagId]: {} };
|
2020-07-07 21:06:06 +00:00
|
|
|
} else if (oldTagId === DefaultTagID.DM) {
|
2018-05-03 17:04:01 +00:00
|
|
|
// Mock inverse m.direct
|
2022-05-09 13:24:12 +00:00
|
|
|
// @ts-ignore forcing private property
|
2018-05-03 17:04:01 +00:00
|
|
|
DMRoomMap.shared().roomToUser = {
|
2022-12-12 11:24:14 +00:00
|
|
|
[movingRoom.roomId]: "@someotheruser:domain",
|
2018-05-03 17:04:01 +00:00
|
|
|
};
|
|
|
|
}
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
dis.dispatch({ action: "MatrixActions.sync", prevState: null, state: "PREPARED", matrixClient: client });
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2018-05-03 17:04:01 +00:00
|
|
|
expectRoomInSubList(movingRoom, srcSubListTest);
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
dis.dispatch({
|
|
|
|
action: "RoomListActions.tagRoom.pending",
|
|
|
|
request: {
|
|
|
|
oldTagId,
|
|
|
|
newTagId,
|
|
|
|
room: movingRoom,
|
|
|
|
},
|
|
|
|
});
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2018-05-03 17:04:01 +00:00
|
|
|
expectRoomInSubList(movingRoom, destSubListTest);
|
|
|
|
}
|
2018-05-03 14:41:35 +00:00
|
|
|
|
2018-05-03 17:04:01 +00:00
|
|
|
function itDoesCorrectOptimisticUpdatesForDraggedRoomTiles() {
|
2019-02-14 02:51:34 +00:00
|
|
|
// TODO: Re-enable dragging tests when we support dragging again.
|
2022-12-12 11:24:14 +00:00
|
|
|
describe.skip("does correct optimistic update when dragging from", () => {
|
|
|
|
it("rooms to people", () => {
|
2020-03-20 20:38:20 +00:00
|
|
|
expectCorrectMove(undefined, DefaultTagID.DM);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("rooms to favourites", () => {
|
|
|
|
expectCorrectMove(undefined, "m.favourite");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("rooms to low priority", () => {
|
|
|
|
expectCorrectMove(undefined, "m.lowpriority");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// XXX: Known to fail - the view does not update immediately to reflect the change.
|
|
|
|
// Whe running the app live, it updates when some other event occurs (likely the
|
|
|
|
// m.direct arriving) that these tests do not fire.
|
2022-12-12 11:24:14 +00:00
|
|
|
xit("people to rooms", () => {
|
2020-03-20 20:38:20 +00:00
|
|
|
expectCorrectMove(DefaultTagID.DM, undefined);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("people to favourites", () => {
|
|
|
|
expectCorrectMove(DefaultTagID.DM, "m.favourite");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("people to lowpriority", () => {
|
|
|
|
expectCorrectMove(DefaultTagID.DM, "m.lowpriority");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("low priority to rooms", () => {
|
|
|
|
expectCorrectMove("m.lowpriority", undefined);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("low priority to people", () => {
|
|
|
|
expectCorrectMove("m.lowpriority", DefaultTagID.DM);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("low priority to low priority", () => {
|
|
|
|
expectCorrectMove("m.lowpriority", "m.lowpriority");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("favourites to rooms", () => {
|
|
|
|
expectCorrectMove("m.favourite", undefined);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("favourites to people", () => {
|
|
|
|
expectCorrectMove("m.favourite", DefaultTagID.DM);
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("favourites to low priority", () => {
|
|
|
|
expectCorrectMove("m.favourite", "m.lowpriority");
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|
|
|
|
});
|
2018-05-03 17:04:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 14:36:35 +00:00
|
|
|
itDoesCorrectOptimisticUpdatesForDraggedRoomTiles();
|
2018-05-03 14:41:35 +00:00
|
|
|
});
|