test typescriptification - RoomList (#8540)
* test/components/views/rooms/RoomList-test.js -> test/components/views/rooms/RoomList-test.tsx Signed-off-by: Kerry Archibald <kerrya@element.io> * fix ts issues Signed-off-by: Kerry Archibald <kerrya@element.io>pull/28788/head^2
parent
7d1ecfdcbc
commit
333dd59f06
|
@ -17,7 +17,11 @@ limitations under the License.
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactTestUtils from 'react-dom/test-utils';
|
import ReactTestUtils from 'react-dom/test-utils';
|
||||||
import ReactDOM from 'react-dom';
|
import ReactDOM from 'react-dom';
|
||||||
import { MatrixClient, Room, RoomMember } from 'matrix-js-sdk/src/matrix';
|
import {
|
||||||
|
PendingEventOrdering,
|
||||||
|
Room,
|
||||||
|
RoomMember,
|
||||||
|
} from 'matrix-js-sdk/src/matrix';
|
||||||
|
|
||||||
import * as TestUtils from '../../../test-utils';
|
import * as TestUtils from '../../../test-utils';
|
||||||
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
||||||
|
@ -29,6 +33,8 @@ import RoomListLayoutStore from "../../../../src/stores/room-list/RoomListLayout
|
||||||
import RoomList from "../../../../src/components/views/rooms/RoomList";
|
import RoomList from "../../../../src/components/views/rooms/RoomList";
|
||||||
import RoomSublist from "../../../../src/components/views/rooms/RoomSublist";
|
import RoomSublist from "../../../../src/components/views/rooms/RoomSublist";
|
||||||
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
|
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
|
||||||
|
import { getMockClientWithEventEmitter, mockClientMethodsUser } from '../../../test-utils';
|
||||||
|
import ResizeNotifier from '../../../../src/utils/ResizeNotifier';
|
||||||
|
|
||||||
function generateRoomId() {
|
function generateRoomId() {
|
||||||
return '!' + Math.random().toString().slice(2, 10) + ':domain';
|
return '!' + Math.random().toString().slice(2, 10) + ':domain';
|
||||||
|
@ -38,7 +44,7 @@ describe('RoomList', () => {
|
||||||
function createRoom(opts) {
|
function createRoom(opts) {
|
||||||
const room = new Room(generateRoomId(), MatrixClientPeg.get(), client.getUserId(), {
|
const room = new Room(generateRoomId(), MatrixClientPeg.get(), client.getUserId(), {
|
||||||
// The room list now uses getPendingEvents(), so we need a detached ordering.
|
// The room list now uses getPendingEvents(), so we need a detached ordering.
|
||||||
pendingEventOrdering: "detached",
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
||||||
});
|
});
|
||||||
if (opts) {
|
if (opts) {
|
||||||
Object.assign(room, opts);
|
Object.assign(room, opts);
|
||||||
|
@ -47,25 +53,38 @@ describe('RoomList', () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
let parentDiv = null;
|
let parentDiv = null;
|
||||||
let client = null;
|
|
||||||
let root = null;
|
let root = null;
|
||||||
const myUserId = '@me:domain';
|
const myUserId = '@me:domain';
|
||||||
|
|
||||||
const movingRoomId = '!someroomid';
|
const movingRoomId = '!someroomid';
|
||||||
let movingRoom;
|
let movingRoom: Room | undefined;
|
||||||
let otherRoom;
|
let otherRoom: Room | undefined;
|
||||||
|
|
||||||
let myMember;
|
let myMember: RoomMember | undefined;
|
||||||
let myOtherMember;
|
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,
|
||||||
|
activeSpace: '',
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async function(done) {
|
beforeEach(async function(done) {
|
||||||
RoomListStoreClass.TEST_MODE = true;
|
RoomListStoreClass.TEST_MODE = true;
|
||||||
|
jest.clearAllMocks();
|
||||||
|
|
||||||
TestUtils.stubClient();
|
|
||||||
client = MatrixClientPeg.get();
|
|
||||||
client.credentials = { userId: myUserId };
|
client.credentials = { userId: myUserId };
|
||||||
//revert this to prototype method as the test-utils monkey-patches this to return a hardcoded value
|
|
||||||
client.getUserId = MatrixClient.prototype.getUserId;
|
|
||||||
|
|
||||||
DMRoomMap.makeShared();
|
DMRoomMap.makeShared();
|
||||||
|
|
||||||
|
@ -74,7 +93,7 @@ describe('RoomList', () => {
|
||||||
|
|
||||||
const WrappedRoomList = TestUtils.wrapInMatrixClientContext(RoomList);
|
const WrappedRoomList = TestUtils.wrapInMatrixClientContext(RoomList);
|
||||||
root = ReactDOM.render(
|
root = ReactDOM.render(
|
||||||
<WrappedRoomList searchFilter="" onResize={() => {}} />,
|
<WrappedRoomList {...defaultProps} />,
|
||||||
parentDiv,
|
parentDiv,
|
||||||
);
|
);
|
||||||
ReactTestUtils.findRenderedComponentWithType(root, RoomList);
|
ReactTestUtils.findRenderedComponentWithType(root, RoomList);
|
||||||
|
@ -99,7 +118,7 @@ describe('RoomList', () => {
|
||||||
}[userId]);
|
}[userId]);
|
||||||
|
|
||||||
// Mock the matrix client
|
// Mock the matrix client
|
||||||
client.getRooms = () => [
|
const mockRooms = [
|
||||||
movingRoom,
|
movingRoom,
|
||||||
otherRoom,
|
otherRoom,
|
||||||
createRoom({ tags: { 'm.favourite': { order: 0.1 } }, name: 'Some other room' }),
|
createRoom({ tags: { 'm.favourite': { order: 0.1 } }, name: 'Some other room' }),
|
||||||
|
@ -107,14 +126,15 @@ describe('RoomList', () => {
|
||||||
createRoom({ tags: { 'm.lowpriority': {} }, name: 'Some unimportant room' }),
|
createRoom({ tags: { 'm.lowpriority': {} }, name: 'Some unimportant room' }),
|
||||||
createRoom({ tags: { 'custom.tag': {} }, name: 'Some room customly tagged' }),
|
createRoom({ tags: { 'custom.tag': {} }, name: 'Some room customly tagged' }),
|
||||||
];
|
];
|
||||||
client.getVisibleRooms = client.getRooms;
|
client.getRooms.mockReturnValue(mockRooms);
|
||||||
|
client.getVisibleRooms.mockReturnValue(mockRooms);
|
||||||
|
|
||||||
const roomMap = {};
|
const roomMap = {};
|
||||||
client.getRooms().forEach((r) => {
|
client.getRooms().forEach((r) => {
|
||||||
roomMap[r.roomId] = r;
|
roomMap[r.roomId] = r;
|
||||||
});
|
});
|
||||||
|
|
||||||
client.getRoom = (roomId) => roomMap[roomId];
|
client.getRoom.mockImplementation((roomId) => roomMap[roomId]);
|
||||||
|
|
||||||
// Now that everything has been set up, prepare and update the store
|
// Now that everything has been set up, prepare and update the store
|
||||||
await RoomListStore.instance.makeReady(client);
|
await RoomListStore.instance.makeReady(client);
|
||||||
|
@ -171,6 +191,7 @@ describe('RoomList', () => {
|
||||||
movingRoom.tags = { [oldTagId]: {} };
|
movingRoom.tags = { [oldTagId]: {} };
|
||||||
} else if (oldTagId === DefaultTagID.DM) {
|
} else if (oldTagId === DefaultTagID.DM) {
|
||||||
// Mock inverse m.direct
|
// Mock inverse m.direct
|
||||||
|
// @ts-ignore forcing private property
|
||||||
DMRoomMap.shared().roomToUser = {
|
DMRoomMap.shared().roomToUser = {
|
||||||
[movingRoom.roomId]: '@someotheruser:domain',
|
[movingRoom.roomId]: '@someotheruser:domain',
|
||||||
};
|
};
|
Loading…
Reference in New Issue