@@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { EventType , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
17
+ import { EventType , MatrixEvent , PendingEventOrdering , Room } from "matrix-js-sdk/src/matrix" ;
18
18
19
19
import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher" ;
20
- import SettingsStore from "../../../src/settings/SettingsStore" ;
20
+ import { SettingLevel } from "../../../src/settings/SettingLevel" ;
21
+ import SettingsStore , { CallbackFn } from "../../../src/settings/SettingsStore" ;
21
22
import { ListAlgorithm , SortAlgorithm } from "../../../src/stores/room-list/algorithms/models" ;
22
23
import { OrderedDefaultTagIDs , RoomUpdateCause } from "../../../src/stores/room-list/models" ;
23
24
import RoomListStore , { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore" ;
25
+ import DMRoomMap from "../../../src/utils/DMRoomMap" ;
24
26
import { stubClient , upsertRoomStateEvents } from "../../test-utils" ;
25
27
26
28
describe ( "RoomListStore" , ( ) => {
@@ -62,7 +64,9 @@ describe("RoomListStore", () => {
62
64
upsertRoomStateEvents ( roomWithPredecessorEvent , [ predecessor ] ) ;
63
65
const roomWithCreatePredecessor = new Room ( newRoomId , client , userId , { } ) ;
64
66
upsertRoomStateEvents ( roomWithCreatePredecessor , [ createWithPredecessor ] ) ;
65
- const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , { } ) ;
67
+ const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , {
68
+ pendingEventOrdering : PendingEventOrdering . Detached ,
69
+ } ) ;
66
70
upsertRoomStateEvents ( roomNoPredecessor , [ createNoPredecessor ] ) ;
67
71
const oldRoom = new Room ( oldRoomId , client , userId , { } ) ;
68
72
client . getRoom = jest . fn ( ) . mockImplementation ( ( roomId ) => {
@@ -138,6 +142,93 @@ describe("RoomListStore", () => {
138
142
expect ( handleRoomUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
139
143
} ) ;
140
144
145
+ it ( "Lists all rooms that the client says are visible" , ( ) => {
146
+ // Given 3 rooms that are visible according to the client
147
+ const room1 = new Room ( "!r1:e.com" , client , userId , { pendingEventOrdering : PendingEventOrdering . Detached } ) ;
148
+ const room2 = new Room ( "!r2:e.com" , client , userId , { pendingEventOrdering : PendingEventOrdering . Detached } ) ;
149
+ const room3 = new Room ( "!r3:e.com" , client , userId , { pendingEventOrdering : PendingEventOrdering . Detached } ) ;
150
+ room1 . updateMyMembership ( "join" ) ;
151
+ room2 . updateMyMembership ( "join" ) ;
152
+ room3 . updateMyMembership ( "join" ) ;
153
+ DMRoomMap . makeShared ( ) ;
154
+ const { store } = createStore ( ) ;
155
+ client . getVisibleRooms = jest . fn ( ) . mockReturnValue ( [ room1 , room2 , room3 ] ) ;
156
+
157
+ // When we make the list of rooms
158
+ store . regenerateAllLists ( { trigger : false } ) ;
159
+
160
+ // Then the list contains all 3
161
+ expect ( store . orderedLists ) . toMatchObject ( {
162
+ "im.vector.fake.recent" : [ room1 , room2 , room3 ] ,
163
+ } ) ;
164
+
165
+ // We asked not to use MSC3946 when we asked the client for the visible rooms
166
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledWith ( false ) ;
167
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledTimes ( 1 ) ;
168
+ } ) ;
169
+
170
+ it ( "Watches the feature flag setting" , ( ) => {
171
+ jest . spyOn ( SettingsStore , "watchSetting" ) . mockReturnValue ( "dyn_pred_ref" ) ;
172
+ jest . spyOn ( SettingsStore , "unwatchSetting" ) ;
173
+
174
+ // When we create a store
175
+ const { store } = createStore ( ) ;
176
+
177
+ // Then we watch the feature flag
178
+ expect ( SettingsStore . watchSetting ) . toHaveBeenCalledWith (
179
+ "feature_dynamic_room_predecessors" ,
180
+ null ,
181
+ expect . any ( Function ) ,
182
+ ) ;
183
+
184
+ // And when we unmount it
185
+ store . componentWillUnmount ( ) ;
186
+
187
+ // Then we unwatch it.
188
+ expect ( SettingsStore . unwatchSetting ) . toHaveBeenCalledWith ( "dyn_pred_ref" ) ;
189
+ } ) ;
190
+
191
+ it ( "Regenerates all lists when the feature flag is set" , ( ) => {
192
+ // Given a store allowing us to spy on any use of SettingsStore
193
+ let featureFlagValue = false ;
194
+ jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation ( ( ) => featureFlagValue ) ;
195
+
196
+ let watchCallback : CallbackFn | undefined ;
197
+ jest . spyOn ( SettingsStore , "watchSetting" ) . mockImplementation (
198
+ ( _settingName : string , _roomId : string | null , callbackFn : CallbackFn ) => {
199
+ watchCallback = callbackFn ;
200
+ return "dyn_pred_ref" ;
201
+ } ,
202
+ ) ;
203
+ jest . spyOn ( SettingsStore , "unwatchSetting" ) ;
204
+
205
+ const { store } = createStore ( ) ;
206
+ client . getVisibleRooms = jest . fn ( ) . mockReturnValue ( [ ] ) ;
207
+ // Sanity: no calculation has happened yet
208
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledTimes ( 0 ) ;
209
+
210
+ // When we calculate for the first time
211
+ store . regenerateAllLists ( { trigger : false } ) ;
212
+
213
+ // Then we use the current feature flag value (false)
214
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledWith ( false ) ;
215
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledTimes ( 1 ) ;
216
+
217
+ // But when we update the feature flag
218
+ featureFlagValue = true ;
219
+ watchCallback (
220
+ "feature_dynamic_room_predecessors" ,
221
+ "" ,
222
+ SettingLevel . DEFAULT ,
223
+ featureFlagValue ,
224
+ featureFlagValue ,
225
+ ) ;
226
+
227
+ // Then we recalculate and passed the updated value (true)
228
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledWith ( true ) ;
229
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledTimes ( 2 ) ;
230
+ } ) ;
231
+
141
232
describe ( "When feature_dynamic_room_predecessors = true" , ( ) => {
142
233
beforeEach ( ( ) => {
143
234
jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
@@ -168,5 +259,19 @@ describe("RoomListStore", () => {
168
259
// And the new room is added
169
260
expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( roomWithPredecessorEvent , RoomUpdateCause . NewRoom ) ;
170
261
} ) ;
262
+
263
+ it ( "Passes the feature flag on to the client when asking for visible rooms" , ( ) => {
264
+ // Given a store that we can ask for a room list
265
+ DMRoomMap . makeShared ( ) ;
266
+ const { store } = createStore ( ) ;
267
+ client . getVisibleRooms = jest . fn ( ) . mockReturnValue ( [ ] ) ;
268
+
269
+ // When we make the list of rooms
270
+ store . regenerateAllLists ( { trigger : false } ) ;
271
+
272
+ // We asked to use MSC3946 when we asked the client for the visible rooms
273
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledWith ( true ) ;
274
+ expect ( client . getVisibleRooms ) . toHaveBeenCalledTimes ( 1 ) ;
275
+ } ) ;
171
276
} ) ;
172
277
} ) ;
0 commit comments