@@ -17,21 +17,22 @@ limitations under the License.
1717import { EventType , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
1818
1919import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher" ;
20+ import SettingsStore from "../../../src/settings/SettingsStore" ;
2021import { ListAlgorithm , SortAlgorithm } from "../../../src/stores/room-list/algorithms/models" ;
2122import { OrderedDefaultTagIDs , RoomUpdateCause } from "../../../src/stores/room-list/models" ;
2223import RoomListStore , { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore" ;
2324import { stubClient , upsertRoomStateEvents } from "../../test-utils" ;
2425
2526describe ( "RoomListStore" , ( ) => {
2627 const client = stubClient ( ) ;
27- const roomWithCreatePredecessorId = "!roomid:example.com" ;
28+ const newRoomId = "!roomid:example.com" ;
2829 const roomNoPredecessorId = "!roomnopreid:example.com" ;
2930 const oldRoomId = "!oldroomid:example.com" ;
3031 const userId = "@user:example.com" ;
3132 const createWithPredecessor = new MatrixEvent ( {
3233 type : EventType . RoomCreate ,
3334 sender : userId ,
34- room_id : roomWithCreatePredecessorId ,
35+ room_id : newRoomId ,
3536 content : {
3637 predecessor : { room_id : oldRoomId , event_id : "tombstone_event_id" } ,
3738 } ,
@@ -41,19 +42,31 @@ describe("RoomListStore", () => {
4142 const createNoPredecessor = new MatrixEvent ( {
4243 type : EventType . RoomCreate ,
4344 sender : userId ,
44- room_id : roomWithCreatePredecessorId ,
45+ room_id : newRoomId ,
4546 content : { } ,
4647 event_id : "$create" ,
4748 state_key : "" ,
4849 } ) ;
49- const roomWithCreatePredecessor = new Room ( roomWithCreatePredecessorId , client , userId , { } ) ;
50+ const predecessor = new MatrixEvent ( {
51+ type : EventType . RoomPredecessor ,
52+ sender : userId ,
53+ room_id : newRoomId ,
54+ content : {
55+ predecessor_room_id : oldRoomId , last_known_event_id : "tombstone_event_id" ,
56+ } ,
57+ event_id : "$pred" ,
58+ state_key : "" ,
59+ } ) ;
60+ const roomWithPredecessorEvent = new Room ( newRoomId , client , userId , { } ) ;
61+ upsertRoomStateEvents ( roomWithPredecessorEvent , [ predecessor ] ) ;
62+ const roomWithCreatePredecessor = new Room ( newRoomId , client , userId , { } ) ;
5063 upsertRoomStateEvents ( roomWithCreatePredecessor , [ createWithPredecessor ] ) ;
5164 const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , { } ) ;
5265 upsertRoomStateEvents ( roomNoPredecessor , [ createNoPredecessor ] ) ;
5366 const oldRoom = new Room ( oldRoomId , client , userId , { } ) ;
5467 client . getRoom = jest . fn ( ) . mockImplementation ( ( roomId ) => {
5568 switch ( roomId ) {
56- case roomWithCreatePredecessorId :
69+ case newRoomId :
5770 return roomWithCreatePredecessor ;
5871 case oldRoomId :
5972 return oldRoom ;
@@ -123,4 +136,36 @@ describe("RoomListStore", () => {
123136 // And no other updates happen
124137 expect ( handleRoomUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
125138 } ) ;
139+
140+ describe ( "When feature_dynamic_room_predecessors = true" , ( ) => {
141+ beforeEach ( ( ) => {
142+ jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
143+ ( settingName ) => settingName === "feature_dynamic_room_predecessors" ,
144+ ) ;
145+ } ) ;
146+
147+ afterEach ( ( ) => {
148+ jest . spyOn ( SettingsStore , "getValue" ) . mockReset ( ) ;
149+ } ) ;
150+
151+ it ( "Removes old room if it finds a predecessor in the m.predecessor event" , ( ) => {
152+ // Given a store we can spy on
153+ const { store, handleRoomUpdate } = createStore ( ) ;
154+
155+ // When we tell it we joined a new room that has an old room as
156+ // predecessor in the create event
157+ const payload = {
158+ oldMembership : "invite" ,
159+ membership : "join" ,
160+ room : roomWithPredecessorEvent ,
161+ } ;
162+ store . onDispatchMyMembership ( payload ) ;
163+
164+ // Then the old room is removed
165+ expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( oldRoom , RoomUpdateCause . RoomRemoved ) ;
166+
167+ // And the new room is added
168+ expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( roomWithPredecessorEvent , RoomUpdateCause . NewRoom ) ;
169+ } ) ;
170+ } ) ;
126171} ) ;
0 commit comments