Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 333dd59

Browse files
author
Kerry
authored
test typescriptification - RoomList (#8540)
* test/components/views/rooms/RoomList-test.js -> test/components/views/rooms/RoomList-test.tsx Signed-off-by: Kerry Archibald <[email protected]> * fix ts issues Signed-off-by: Kerry Archibald <[email protected]>
1 parent 7d1ecfd commit 333dd59

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

test/components/views/rooms/RoomList-test.js renamed to test/components/views/rooms/RoomList-test.tsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ limitations under the License.
1717
import React from 'react';
1818
import ReactTestUtils from 'react-dom/test-utils';
1919
import ReactDOM from 'react-dom';
20-
import { MatrixClient, Room, RoomMember } from 'matrix-js-sdk/src/matrix';
20+
import {
21+
PendingEventOrdering,
22+
Room,
23+
RoomMember,
24+
} from 'matrix-js-sdk/src/matrix';
2125

2226
import * as TestUtils from '../../../test-utils';
2327
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
@@ -29,6 +33,8 @@ import RoomListLayoutStore from "../../../../src/stores/room-list/RoomListLayout
2933
import RoomList from "../../../../src/components/views/rooms/RoomList";
3034
import RoomSublist from "../../../../src/components/views/rooms/RoomSublist";
3135
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
36+
import { getMockClientWithEventEmitter, mockClientMethodsUser } from '../../../test-utils';
37+
import ResizeNotifier from '../../../../src/utils/ResizeNotifier';
3238

3339
function generateRoomId() {
3440
return '!' + Math.random().toString().slice(2, 10) + ':domain';
@@ -38,7 +44,7 @@ describe('RoomList', () => {
3844
function createRoom(opts) {
3945
const room = new Room(generateRoomId(), MatrixClientPeg.get(), client.getUserId(), {
4046
// The room list now uses getPendingEvents(), so we need a detached ordering.
41-
pendingEventOrdering: "detached",
47+
pendingEventOrdering: PendingEventOrdering.Detached,
4248
});
4349
if (opts) {
4450
Object.assign(room, opts);
@@ -47,25 +53,38 @@ describe('RoomList', () => {
4753
}
4854

4955
let parentDiv = null;
50-
let client = null;
5156
let root = null;
5257
const myUserId = '@me:domain';
5358

5459
const movingRoomId = '!someroomid';
55-
let movingRoom;
56-
let otherRoom;
60+
let movingRoom: Room | undefined;
61+
let otherRoom: Room | undefined;
5762

58-
let myMember;
59-
let myOtherMember;
63+
let myMember: RoomMember | undefined;
64+
let myOtherMember: RoomMember | undefined;
65+
66+
const client = getMockClientWithEventEmitter({
67+
...mockClientMethodsUser(myUserId),
68+
getRooms: jest.fn(),
69+
getVisibleRooms: jest.fn(),
70+
getRoom: jest.fn(),
71+
});
72+
73+
const defaultProps = {
74+
onKeyDown: jest.fn(),
75+
onFocus: jest.fn(),
76+
onBlur: jest.fn(),
77+
onResize: jest.fn(),
78+
resizeNotifier: {} as unknown as ResizeNotifier,
79+
isMinimized: false,
80+
activeSpace: '',
81+
};
6082

6183
beforeEach(async function(done) {
6284
RoomListStoreClass.TEST_MODE = true;
85+
jest.clearAllMocks();
6386

64-
TestUtils.stubClient();
65-
client = MatrixClientPeg.get();
6687
client.credentials = { userId: myUserId };
67-
//revert this to prototype method as the test-utils monkey-patches this to return a hardcoded value
68-
client.getUserId = MatrixClient.prototype.getUserId;
6988

7089
DMRoomMap.makeShared();
7190

@@ -74,7 +93,7 @@ describe('RoomList', () => {
7493

7594
const WrappedRoomList = TestUtils.wrapInMatrixClientContext(RoomList);
7695
root = ReactDOM.render(
77-
<WrappedRoomList searchFilter="" onResize={() => {}} />,
96+
<WrappedRoomList {...defaultProps} />,
7897
parentDiv,
7998
);
8099
ReactTestUtils.findRenderedComponentWithType(root, RoomList);
@@ -99,22 +118,23 @@ describe('RoomList', () => {
99118
}[userId]);
100119

101120
// Mock the matrix client
102-
client.getRooms = () => [
121+
const mockRooms = [
103122
movingRoom,
104123
otherRoom,
105124
createRoom({ tags: { 'm.favourite': { order: 0.1 } }, name: 'Some other room' }),
106125
createRoom({ tags: { 'm.favourite': { order: 0.2 } }, name: 'Some other room 2' }),
107126
createRoom({ tags: { 'm.lowpriority': {} }, name: 'Some unimportant room' }),
108127
createRoom({ tags: { 'custom.tag': {} }, name: 'Some room customly tagged' }),
109128
];
110-
client.getVisibleRooms = client.getRooms;
129+
client.getRooms.mockReturnValue(mockRooms);
130+
client.getVisibleRooms.mockReturnValue(mockRooms);
111131

112132
const roomMap = {};
113133
client.getRooms().forEach((r) => {
114134
roomMap[r.roomId] = r;
115135
});
116136

117-
client.getRoom = (roomId) => roomMap[roomId];
137+
client.getRoom.mockImplementation((roomId) => roomMap[roomId]);
118138

119139
// Now that everything has been set up, prepare and update the store
120140
await RoomListStore.instance.makeReady(client);
@@ -171,6 +191,7 @@ describe('RoomList', () => {
171191
movingRoom.tags = { [oldTagId]: {} };
172192
} else if (oldTagId === DefaultTagID.DM) {
173193
// Mock inverse m.direct
194+
// @ts-ignore forcing private property
174195
DMRoomMap.shared().roomToUser = {
175196
[movingRoom.roomId]: '@someotheruser:domain',
176197
};

0 commit comments

Comments
 (0)