@@ -17,21 +17,22 @@ limitations under the License.
17
17
import { EventType , MatrixEvent , 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
21
import { ListAlgorithm , SortAlgorithm } from "../../../src/stores/room-list/algorithms/models" ;
21
22
import { OrderedDefaultTagIDs , RoomUpdateCause } from "../../../src/stores/room-list/models" ;
22
23
import RoomListStore , { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore" ;
23
24
import { stubClient , upsertRoomStateEvents } from "../../test-utils" ;
24
25
25
26
describe ( "RoomListStore" , ( ) => {
26
27
const client = stubClient ( ) ;
27
- const roomWithCreatePredecessorId = "!roomid:example.com" ;
28
+ const newRoomId = "!roomid:example.com" ;
28
29
const roomNoPredecessorId = "!roomnopreid:example.com" ;
29
30
const oldRoomId = "!oldroomid:example.com" ;
30
31
const userId = "@user:example.com" ;
31
32
const createWithPredecessor = new MatrixEvent ( {
32
33
type : EventType . RoomCreate ,
33
34
sender : userId ,
34
- room_id : roomWithCreatePredecessorId ,
35
+ room_id : newRoomId ,
35
36
content : {
36
37
predecessor : { room_id : oldRoomId , event_id : "tombstone_event_id" } ,
37
38
} ,
@@ -41,19 +42,31 @@ describe("RoomListStore", () => {
41
42
const createNoPredecessor = new MatrixEvent ( {
42
43
type : EventType . RoomCreate ,
43
44
sender : userId ,
44
- room_id : roomWithCreatePredecessorId ,
45
+ room_id : newRoomId ,
45
46
content : { } ,
46
47
event_id : "$create" ,
47
48
state_key : "" ,
48
49
} ) ;
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 : { 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 , { } ) ;
50
63
upsertRoomStateEvents ( roomWithCreatePredecessor , [ createWithPredecessor ] ) ;
51
64
const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , { } ) ;
52
65
upsertRoomStateEvents ( roomNoPredecessor , [ createNoPredecessor ] ) ;
53
66
const oldRoom = new Room ( oldRoomId , client , userId , { } ) ;
54
67
client . getRoom = jest . fn ( ) . mockImplementation ( ( roomId ) => {
55
68
switch ( roomId ) {
56
- case roomWithCreatePredecessorId :
69
+ case newRoomId :
57
70
return roomWithCreatePredecessor ;
58
71
case oldRoomId :
59
72
return oldRoom ;
@@ -123,4 +136,36 @@ describe("RoomListStore", () => {
123
136
// And no other updates happen
124
137
expect ( handleRoomUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
125
138
} ) ;
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 ( roomWithCreatePredecessor , RoomUpdateCause . NewRoom ) ;
169
+ } ) ;
170
+ } ) ;
126
171
} ) ;
0 commit comments