@@ -14,13 +14,15 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- import { EventType , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
17+ import { EventType , MatrixEvent , PendingEventOrdering , Room } from "matrix-js-sdk/src/matrix" ;
1818
1919import { 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" ;
2122import { ListAlgorithm , SortAlgorithm } from "../../../src/stores/room-list/algorithms/models" ;
2223import { OrderedDefaultTagIDs , RoomUpdateCause } from "../../../src/stores/room-list/models" ;
2324import RoomListStore , { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore" ;
25+ import DMRoomMap from "../../../src/utils/DMRoomMap" ;
2426import { stubClient , upsertRoomStateEvents } from "../../test-utils" ;
2527
2628describe ( "RoomListStore" , ( ) => {
@@ -62,7 +64,9 @@ describe("RoomListStore", () => {
6264 upsertRoomStateEvents ( roomWithPredecessorEvent , [ predecessor ] ) ;
6365 const roomWithCreatePredecessor = new Room ( newRoomId , client , userId , { } ) ;
6466 upsertRoomStateEvents ( roomWithCreatePredecessor , [ createWithPredecessor ] ) ;
65- const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , { } ) ;
67+ const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , {
68+ pendingEventOrdering : PendingEventOrdering . Detached ,
69+ } ) ;
6670 upsertRoomStateEvents ( roomNoPredecessor , [ createNoPredecessor ] ) ;
6771 const oldRoom = new Room ( oldRoomId , client , userId , { } ) ;
6872 client . getRoom = jest . fn ( ) . mockImplementation ( ( roomId ) => {
@@ -138,6 +142,93 @@ describe("RoomListStore", () => {
138142 expect ( handleRoomUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
139143 } ) ;
140144
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+
141232 describe ( "When feature_dynamic_room_predecessors = true" , ( ) => {
142233 beforeEach ( ( ) => {
143234 jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
@@ -168,5 +259,19 @@ describe("RoomListStore", () => {
168259 // And the new room is added
169260 expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( roomWithPredecessorEvent , RoomUpdateCause . NewRoom ) ;
170261 } ) ;
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+ } ) ;
171276 } ) ;
172277} ) ;
0 commit comments