@@ -17,7 +17,11 @@ limitations under the License.
17
17
import React from 'react' ;
18
18
import ReactTestUtils from 'react-dom/test-utils' ;
19
19
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' ;
21
25
22
26
import * as TestUtils from '../../../test-utils' ;
23
27
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg' ;
@@ -29,6 +33,8 @@ import RoomListLayoutStore from "../../../../src/stores/room-list/RoomListLayout
29
33
import RoomList from "../../../../src/components/views/rooms/RoomList" ;
30
34
import RoomSublist from "../../../../src/components/views/rooms/RoomSublist" ;
31
35
import RoomTile from "../../../../src/components/views/rooms/RoomTile" ;
36
+ import { getMockClientWithEventEmitter , mockClientMethodsUser } from '../../../test-utils' ;
37
+ import ResizeNotifier from '../../../../src/utils/ResizeNotifier' ;
32
38
33
39
function generateRoomId ( ) {
34
40
return '!' + Math . random ( ) . toString ( ) . slice ( 2 , 10 ) + ':domain' ;
@@ -38,7 +44,7 @@ describe('RoomList', () => {
38
44
function createRoom ( opts ) {
39
45
const room = new Room ( generateRoomId ( ) , MatrixClientPeg . get ( ) , client . getUserId ( ) , {
40
46
// The room list now uses getPendingEvents(), so we need a detached ordering.
41
- pendingEventOrdering : "detached" ,
47
+ pendingEventOrdering : PendingEventOrdering . Detached ,
42
48
} ) ;
43
49
if ( opts ) {
44
50
Object . assign ( room , opts ) ;
@@ -47,25 +53,38 @@ describe('RoomList', () => {
47
53
}
48
54
49
55
let parentDiv = null ;
50
- let client = null ;
51
56
let root = null ;
52
57
const myUserId = '@me:domain' ;
53
58
54
59
const movingRoomId = '!someroomid' ;
55
- let movingRoom ;
56
- let otherRoom ;
60
+ let movingRoom : Room | undefined ;
61
+ let otherRoom : Room | undefined ;
57
62
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
+ } ;
60
82
61
83
beforeEach ( async function ( done ) {
62
84
RoomListStoreClass . TEST_MODE = true ;
85
+ jest . clearAllMocks ( ) ;
63
86
64
- TestUtils . stubClient ( ) ;
65
- client = MatrixClientPeg . get ( ) ;
66
87
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 ;
69
88
70
89
DMRoomMap . makeShared ( ) ;
71
90
@@ -74,7 +93,7 @@ describe('RoomList', () => {
74
93
75
94
const WrappedRoomList = TestUtils . wrapInMatrixClientContext ( RoomList ) ;
76
95
root = ReactDOM . render (
77
- < WrappedRoomList searchFilter = "" onResize = { ( ) => { } } /> ,
96
+ < WrappedRoomList { ... defaultProps } /> ,
78
97
parentDiv ,
79
98
) ;
80
99
ReactTestUtils . findRenderedComponentWithType ( root , RoomList ) ;
@@ -99,22 +118,23 @@ describe('RoomList', () => {
99
118
} [ userId ] ) ;
100
119
101
120
// Mock the matrix client
102
- client . getRooms = ( ) => [
121
+ const mockRooms = [
103
122
movingRoom ,
104
123
otherRoom ,
105
124
createRoom ( { tags : { 'm.favourite' : { order : 0.1 } } , name : 'Some other room' } ) ,
106
125
createRoom ( { tags : { 'm.favourite' : { order : 0.2 } } , name : 'Some other room 2' } ) ,
107
126
createRoom ( { tags : { 'm.lowpriority' : { } } , name : 'Some unimportant room' } ) ,
108
127
createRoom ( { tags : { 'custom.tag' : { } } , name : 'Some room customly tagged' } ) ,
109
128
] ;
110
- client . getVisibleRooms = client . getRooms ;
129
+ client . getRooms . mockReturnValue ( mockRooms ) ;
130
+ client . getVisibleRooms . mockReturnValue ( mockRooms ) ;
111
131
112
132
const roomMap = { } ;
113
133
client . getRooms ( ) . forEach ( ( r ) => {
114
134
roomMap [ r . roomId ] = r ;
115
135
} ) ;
116
136
117
- client . getRoom = ( roomId ) => roomMap [ roomId ] ;
137
+ client . getRoom . mockImplementation ( ( roomId ) => roomMap [ roomId ] ) ;
118
138
119
139
// Now that everything has been set up, prepare and update the store
120
140
await RoomListStore . instance . makeReady ( client ) ;
@@ -171,6 +191,7 @@ describe('RoomList', () => {
171
191
movingRoom . tags = { [ oldTagId ] : { } } ;
172
192
} else if ( oldTagId === DefaultTagID . DM ) {
173
193
// Mock inverse m.direct
194
+ // @ts -ignore forcing private property
174
195
DMRoomMap . shared ( ) . roomToUser = {
175
196
[ movingRoom . roomId ] : '@someotheruser:domain' ,
176
197
} ;
0 commit comments