2018-05-03 17:41:35 +03:00
|
|
|
import React from 'react';
|
2019-08-24 13:47:07 +03:00
|
|
|
import ReactTestUtils from 'react-dom/test-utils';
|
2018-05-03 17:41:35 +03:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
import * as TestUtils from '../../../test-utils';
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../../../src/MatrixClientPeg';
|
2019-12-17 14:47:01 +03:00
|
|
|
import sdk from '../../../skinned-sdk';
|
2018-05-03 17:41:35 +03:00
|
|
|
import { DragDropContext } from 'react-beautiful-dnd';
|
|
|
|
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from '../../../../src/dispatcher/dispatcher';
|
2021-01-13 19:02:35 +03:00
|
|
|
import DMRoomMap from '../../../../src/utils/DMRoomMap';
|
|
|
|
import GroupStore from '../../../../src/stores/GroupStore';
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-07-18 13:14:26 +03:00
|
|
|
import { MatrixClient, Room, RoomMember } from 'matrix-js-sdk';
|
2020-03-20 23:38:20 +03:00
|
|
|
import {DefaultTagID} from "../../../../src/stores/room-list/models";
|
2020-07-18 01:28:49 +03:00
|
|
|
import RoomListStore, { LISTS_UPDATE_EVENT, RoomListStoreClass } from "../../../../src/stores/room-list/RoomListStore";
|
2020-07-10 07:40:34 +03:00
|
|
|
import RoomListLayoutStore from "../../../../src/stores/room-list/RoomListLayoutStore";
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
function generateRoomId() {
|
|
|
|
return '!' + Math.random().toString().slice(2, 10) + ':domain';
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
function waitForRoomListStoreUpdate() {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
RoomListStore.instance.once(LISTS_UPDATE_EVENT, () => resolve());
|
|
|
|
});
|
|
|
|
}
|
2018-05-03 20:04:01 +03:00
|
|
|
|
2018-05-03 17:41:35 +03:00
|
|
|
describe('RoomList', () => {
|
2018-08-02 12:40:25 +03:00
|
|
|
function createRoom(opts) {
|
2018-08-07 16:33:37 +03:00
|
|
|
const room = new Room(generateRoomId(), null, client.getUserId());
|
2018-08-02 12:40:25 +03:00
|
|
|
if (opts) {
|
|
|
|
Object.assign(room, opts);
|
|
|
|
}
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
|
2018-05-03 17:41:35 +03:00
|
|
|
let parentDiv = null;
|
|
|
|
let client = null;
|
|
|
|
let root = null;
|
|
|
|
const myUserId = '@me:domain';
|
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
const movingRoomId = '!someroomid';
|
|
|
|
let movingRoom;
|
|
|
|
let otherRoom;
|
|
|
|
|
|
|
|
let myMember;
|
|
|
|
let myOtherMember;
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
beforeEach(async function(done) {
|
2020-07-18 00:10:30 +03:00
|
|
|
RoomListStoreClass.TEST_MODE = true;
|
2020-07-10 20:05:56 +03:00
|
|
|
|
2019-12-16 14:12:48 +03:00
|
|
|
TestUtils.stubClient();
|
2018-05-03 17:41:35 +03:00
|
|
|
client = MatrixClientPeg.get();
|
|
|
|
client.credentials = {userId: myUserId};
|
2018-07-18 13:14:26 +03:00
|
|
|
//revert this to prototype method as the test-utils monkey-patches this to return a hardcoded value
|
|
|
|
client.getUserId = MatrixClient.prototype.getUserId;
|
2018-05-03 17:41:35 +03:00
|
|
|
|
|
|
|
DMRoomMap.makeShared();
|
|
|
|
|
|
|
|
parentDiv = document.createElement('div');
|
|
|
|
document.body.appendChild(parentDiv);
|
|
|
|
|
2020-07-18 00:27:49 +03:00
|
|
|
const RoomList = sdk.getComponent('views.rooms.RoomList');
|
2018-05-03 17:41:35 +03:00
|
|
|
const WrappedRoomList = TestUtils.wrapInMatrixClientContext(RoomList);
|
|
|
|
root = ReactDOM.render(
|
|
|
|
<DragDropContext>
|
2020-07-08 00:06:06 +03:00
|
|
|
<WrappedRoomList searchFilter="" onResize={() => {}} />
|
2018-05-03 17:41:35 +03:00
|
|
|
</DragDropContext>
|
|
|
|
, parentDiv);
|
|
|
|
ReactTestUtils.findRenderedComponentWithType(root, RoomList);
|
2018-05-03 20:04:01 +03:00
|
|
|
|
|
|
|
movingRoom = createRoom({name: 'Moving room'});
|
2018-12-25 05:13:09 +03:00
|
|
|
expect(movingRoom.roomId).not.toBe(null);
|
2018-05-03 20:04:01 +03:00
|
|
|
|
|
|
|
// Mock joined member
|
|
|
|
myMember = new RoomMember(movingRoomId, myUserId);
|
|
|
|
myMember.membership = 'join';
|
2018-09-18 18:09:14 +03:00
|
|
|
movingRoom.updateMyMembership('join');
|
2018-05-03 20:04:01 +03:00
|
|
|
movingRoom.getMember = (userId) => ({
|
|
|
|
[client.credentials.userId]: myMember,
|
|
|
|
}[userId]);
|
|
|
|
|
|
|
|
otherRoom = createRoom({name: 'Other room'});
|
|
|
|
myOtherMember = new RoomMember(otherRoom.roomId, myUserId);
|
|
|
|
myOtherMember.membership = 'join';
|
2018-09-18 18:09:14 +03:00
|
|
|
otherRoom.updateMyMembership('join');
|
2018-05-03 20:04:01 +03:00
|
|
|
otherRoom.getMember = (userId) => ({
|
|
|
|
[client.credentials.userId]: myOtherMember,
|
|
|
|
}[userId]);
|
|
|
|
|
|
|
|
// Mock the matrix client
|
|
|
|
client.getRooms = () => [
|
|
|
|
movingRoom,
|
|
|
|
otherRoom,
|
|
|
|
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-08-23 14:14:52 +03:00
|
|
|
client.getVisibleRooms = client.getRooms;
|
2018-05-03 20:04:01 +03:00
|
|
|
|
|
|
|
const roomMap = {};
|
|
|
|
client.getRooms().forEach((r) => {
|
|
|
|
roomMap[r.roomId] = r;
|
|
|
|
});
|
|
|
|
|
|
|
|
client.getRoom = (roomId) => roomMap[roomId];
|
2020-07-08 00:45:59 +03:00
|
|
|
|
|
|
|
// Now that everything has been set up, prepare and update the store
|
|
|
|
await RoomListStore.instance.makeReady(client);
|
|
|
|
|
|
|
|
done();
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
afterEach(async (done) => {
|
2018-05-03 17:41:35 +03:00
|
|
|
if (parentDiv) {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
|
|
|
parentDiv.remove();
|
|
|
|
parentDiv = null;
|
|
|
|
}
|
|
|
|
|
2020-07-10 07:40:34 +03:00
|
|
|
await RoomListLayoutStore.instance.resetLayouts();
|
2020-07-08 00:45:59 +03:00
|
|
|
await RoomListStore.instance.resetStore();
|
2018-05-03 17:41:35 +03:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
function expectRoomInSubList(room, subListTest) {
|
2020-07-18 00:46:46 +03:00
|
|
|
const RoomSubList = sdk.getComponent('views.rooms.RoomSublist');
|
2020-07-18 00:43:29 +03:00
|
|
|
const RoomTile = sdk.getComponent('views.rooms.RoomTile');
|
2018-05-03 20:04:01 +03:00
|
|
|
|
|
|
|
const subLists = ReactTestUtils.scryRenderedComponentsWithType(root, RoomSubList);
|
|
|
|
const containingSubList = subLists.find(subListTest);
|
|
|
|
|
|
|
|
let expectedRoomTile;
|
|
|
|
try {
|
|
|
|
const roomTiles = ReactTestUtils.scryRenderedComponentsWithType(containingSubList, RoomTile);
|
|
|
|
console.info({roomTiles: roomTiles.length});
|
|
|
|
expectedRoomTile = roomTiles.find((tile) => tile.props.room === room);
|
|
|
|
} catch (err) {
|
|
|
|
// truncate the error message because it's spammy
|
|
|
|
err.message = 'Error finding RoomTile for ' + room.roomId + ' in ' +
|
|
|
|
subListTest + ': ' +
|
|
|
|
err.message.split('componentType')[0] + '...';
|
|
|
|
throw err;
|
|
|
|
}
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-12-25 05:13:09 +03:00
|
|
|
expect(expectedRoomTile).toBeTruthy();
|
2018-05-03 20:04:01 +03:00
|
|
|
expect(expectedRoomTile.props.room).toBe(room);
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:06:06 +03:00
|
|
|
function expectCorrectMove(oldTagId, newTagId) {
|
|
|
|
const getTagSubListTest = (tagId) => {
|
|
|
|
return (s) => s.props.tagId === tagId;
|
2018-05-03 20:04:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Default to finding the destination sublist with newTag
|
2020-07-08 00:06:06 +03:00
|
|
|
const destSubListTest = getTagSubListTest(newTagId);
|
|
|
|
const srcSubListTest = getTagSubListTest(oldTagId);
|
2018-05-03 20:04:01 +03:00
|
|
|
|
|
|
|
// Set up the room that will be moved such that it has the correct state for a room in
|
2020-07-08 00:06:06 +03:00
|
|
|
// the section for oldTagId
|
|
|
|
if (oldTagId === DefaultTagID.Favourite || oldTagId === DefaultTagID.LowPriority) {
|
|
|
|
movingRoom.tags = {[oldTagId]: {}};
|
|
|
|
} else if (oldTagId === DefaultTagID.DM) {
|
2018-05-03 20:04:01 +03:00
|
|
|
// Mock inverse m.direct
|
|
|
|
DMRoomMap.shared().roomToUser = {
|
|
|
|
[movingRoom.roomId]: '@someotheruser:domain',
|
|
|
|
};
|
|
|
|
}
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
dis.dispatch({action: 'MatrixActions.sync', prevState: null, state: 'PREPARED', matrixClient: client});
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
expectRoomInSubList(movingRoom, srcSubListTest);
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
dis.dispatch({action: 'RoomListActions.tagRoom.pending', request: {
|
2020-07-08 00:06:06 +03:00
|
|
|
oldTagId, newTagId, room: movingRoom,
|
2018-05-03 20:04:01 +03:00
|
|
|
}});
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
expectRoomInSubList(movingRoom, destSubListTest);
|
|
|
|
}
|
2018-05-03 17:41:35 +03:00
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
function itDoesCorrectOptimisticUpdatesForDraggedRoomTiles() {
|
2019-02-14 05:51:34 +03:00
|
|
|
// TODO: Re-enable dragging tests when we support dragging again.
|
2019-12-17 14:47:01 +03:00
|
|
|
describe.skip('does correct optimistic update when dragging from', () => {
|
2018-05-03 17:41:35 +03:00
|
|
|
it('rooms to people', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove(undefined, DefaultTagID.DM);
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('rooms to favourites', () => {
|
|
|
|
expectCorrectMove(undefined, 'm.favourite');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rooms to low priority', () => {
|
|
|
|
expectCorrectMove(undefined, 'm.lowpriority');
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
xit('people to rooms', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove(DefaultTagID.DM, undefined);
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('people to favourites', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove(DefaultTagID.DM, 'm.favourite');
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('people to lowpriority', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove(DefaultTagID.DM, 'm.lowpriority');
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('low priority to rooms', () => {
|
|
|
|
expectCorrectMove('m.lowpriority', undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('low priority to people', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove('m.lowpriority', DefaultTagID.DM);
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('low priority to low priority', () => {
|
|
|
|
expectCorrectMove('m.lowpriority', 'm.lowpriority');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('favourites to rooms', () => {
|
|
|
|
expectCorrectMove('m.favourite', undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('favourites to people', () => {
|
2020-03-20 23:38:20 +03:00
|
|
|
expectCorrectMove('m.favourite', DefaultTagID.DM);
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('favourites to low priority', () => {
|
|
|
|
expectCorrectMove('m.favourite', 'm.lowpriority');
|
|
|
|
});
|
|
|
|
});
|
2018-05-03 20:04:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('when no tags are selected', () => {
|
|
|
|
itDoesCorrectOptimisticUpdatesForDraggedRoomTiles();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when tags are selected', () => {
|
|
|
|
function setupSelectedTag() {
|
|
|
|
// Simulate a complete sync BEFORE dispatching anything else
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'MatrixActions.sync',
|
|
|
|
prevState: null,
|
|
|
|
state: 'PREPARED',
|
|
|
|
matrixClient: client,
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Simulate joined groups being received
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'GroupActions.fetchJoinedGroups.success',
|
|
|
|
result: {
|
|
|
|
groups: ['+group:domain'],
|
|
|
|
},
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Simulate receiving tag ordering account data
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'MatrixActions.accountData',
|
|
|
|
event_type: 'im.vector.web.tag_ordering',
|
|
|
|
event_content: {
|
|
|
|
tags: ['+group:domain'],
|
|
|
|
},
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// GroupStore is not flux, mock and notify
|
|
|
|
GroupStore.getGroupRooms = (groupId) => {
|
|
|
|
return [movingRoom];
|
|
|
|
};
|
|
|
|
GroupStore._notifyListeners();
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
// We also have to mock the client's getGroup function for the room list to filter it.
|
|
|
|
// It's not smart enough to tell the difference between a real group and a template though.
|
|
|
|
client.getGroup = (groupId) => {
|
|
|
|
return {groupId};
|
|
|
|
};
|
|
|
|
|
2018-05-03 20:04:01 +03:00
|
|
|
// Select tag
|
|
|
|
dis.dispatch({action: 'select_tag', tag: '+group:domain'}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
setupSelectedTag();
|
|
|
|
});
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
it('displays the correct rooms when the groups rooms are changed', async () => {
|
2018-05-03 20:04:01 +03:00
|
|
|
GroupStore.getGroupRooms = (groupId) => {
|
|
|
|
return [movingRoom, otherRoom];
|
|
|
|
};
|
|
|
|
GroupStore._notifyListeners();
|
|
|
|
|
2020-07-08 00:45:59 +03:00
|
|
|
await waitForRoomListStoreUpdate();
|
2021-04-01 08:51:17 +03:00
|
|
|
|
|
|
|
// XXX: Even though the store updated, it can take a bit before the update makes
|
|
|
|
// it to the components. This gives it plenty of time to figure out what to do.
|
|
|
|
await (new Promise(resolve => setTimeout(resolve, 500)));
|
|
|
|
|
2020-07-08 00:06:06 +03:00
|
|
|
expectRoomInSubList(otherRoom, (s) => s.props.tagId === DefaultTagID.Untagged);
|
2018-05-03 20:04:01 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
itDoesCorrectOptimisticUpdatesForDraggedRoomTiles();
|
2018-05-03 17:41:35 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|