@@ -14,14 +14,55 @@ 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" ;
18
+
19
+ import { MatrixDispatcher } from "../../../src/dispatcher/dispatcher" ;
17
20
import { ListAlgorithm , SortAlgorithm } from "../../../src/stores/room-list/algorithms/models" ;
18
- import { OrderedDefaultTagIDs } from "../../../src/stores/room-list/models" ;
21
+ import { OrderedDefaultTagIDs , RoomUpdateCause } from "../../../src/stores/room-list/models" ;
19
22
import RoomListStore , { RoomListStoreClass } from "../../../src/stores/room-list/RoomListStore" ;
20
- import { stubClient } from "../../test-utils" ;
23
+ import { stubClient , upsertRoomStateEvents } from "../../test-utils" ;
21
24
22
25
describe ( "RoomListStore" , ( ) => {
26
+ const client = stubClient ( ) ;
27
+ const roomWithCreatePredecessorId = "!roomid:example.com" ;
28
+ const roomNoPredecessorId = "!roomnopreid:example.com" ;
29
+ const oldRoomId = "!oldroomid:example.com" ;
30
+ const userId = "@user:example.com" ;
31
+ const createWithPredecessor = new MatrixEvent ( {
32
+ type : EventType . RoomCreate ,
33
+ sender : userId ,
34
+ room_id : roomWithCreatePredecessorId ,
35
+ content : {
36
+ predecessor : { room_id : oldRoomId , event_id : "tombstone_event_id" } ,
37
+ } ,
38
+ event_id : "$create" ,
39
+ state_key : "" ,
40
+ } ) ;
41
+ const createNoPredecessor = new MatrixEvent ( {
42
+ type : EventType . RoomCreate ,
43
+ sender : userId ,
44
+ room_id : roomWithCreatePredecessorId ,
45
+ content : { } ,
46
+ event_id : "$create" ,
47
+ state_key : "" ,
48
+ } ) ;
49
+ const roomWithCreatePredecessor = new Room ( roomWithCreatePredecessorId , client , userId , { } ) ;
50
+ upsertRoomStateEvents ( roomWithCreatePredecessor , [ createWithPredecessor ] ) ;
51
+ const roomNoPredecessor = new Room ( roomNoPredecessorId , client , userId , { } ) ;
52
+ upsertRoomStateEvents ( roomNoPredecessor , [ createNoPredecessor ] ) ;
53
+ const oldRoom = new Room ( oldRoomId , client , userId , { } ) ;
54
+ client . getRoom = jest . fn ( ) . mockImplementation ( ( roomId ) => {
55
+ switch ( roomId ) {
56
+ case roomWithCreatePredecessorId :
57
+ return roomWithCreatePredecessor ;
58
+ case oldRoomId :
59
+ return oldRoom ;
60
+ default :
61
+ return null ;
62
+ }
63
+ } ) ;
64
+
23
65
beforeAll ( async ( ) => {
24
- const client = stubClient ( ) ;
25
66
await ( RoomListStore . instance as RoomListStoreClass ) . makeReady ( client ) ;
26
67
} ) ;
27
68
@@ -32,4 +73,54 @@ describe("RoomListStore", () => {
32
73
it . each ( OrderedDefaultTagIDs ) ( "defaults to activity ordering for %s=" , ( tagId ) => {
33
74
expect ( RoomListStore . instance . getListOrder ( tagId ) ) . toBe ( ListAlgorithm . Importance ) ;
34
75
} ) ;
76
+
77
+ function createStore ( ) : { store : RoomListStoreClass ; handleRoomUpdate : jest . Mock < any , any > } {
78
+ const fakeDispatcher = { register : jest . fn ( ) } as unknown as MatrixDispatcher ;
79
+ const store = new RoomListStoreClass ( fakeDispatcher ) ;
80
+ // @ts -ignore accessing private member to set client
81
+ store . readyStore . matrixClient = client ;
82
+ const handleRoomUpdate = jest . fn ( ) ;
83
+ // @ts -ignore accessing private member to mock it
84
+ store . algorithm . handleRoomUpdate = handleRoomUpdate ;
85
+
86
+ return { store, handleRoomUpdate } ;
87
+ }
88
+
89
+ it ( "Removes old room if it finds a predecessor in the create event" , ( ) => {
90
+ // Given a store we can spy on
91
+ const { store, handleRoomUpdate } = createStore ( ) ;
92
+
93
+ // When we tell it we joined a new room that has an old room as
94
+ // predecessor in the create event
95
+ const payload = {
96
+ oldMembership : "invite" ,
97
+ membership : "join" ,
98
+ room : roomWithCreatePredecessor ,
99
+ } ;
100
+ store . onDispatchMyMembership ( payload ) ;
101
+
102
+ // Then the old room is removed
103
+ expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( oldRoom , RoomUpdateCause . RoomRemoved ) ;
104
+
105
+ // And the new room is added
106
+ expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( oldRoom , RoomUpdateCause . RoomRemoved ) ;
107
+ } ) ;
108
+
109
+ it ( "Does not remove old room if there is no predecessor in the create event" , ( ) => {
110
+ // Given a store we can spy on
111
+ const { store, handleRoomUpdate } = createStore ( ) ;
112
+
113
+ // When we tell it we joined a new room with no predecessor
114
+ const payload = {
115
+ oldMembership : "invite" ,
116
+ membership : "join" ,
117
+ room : roomNoPredecessor ,
118
+ } ;
119
+ store . onDispatchMyMembership ( payload ) ;
120
+
121
+ // Then the new room is added
122
+ expect ( handleRoomUpdate ) . toHaveBeenCalledWith ( roomNoPredecessor , RoomUpdateCause . NewRoom ) ;
123
+ // And no other updates happen
124
+ expect ( handleRoomUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
125
+ } ) ;
35
126
} ) ;
0 commit comments