@@ -28,13 +28,15 @@ import { Relations } from "matrix-js-sdk/src/models/relations";
28
28
import { RelationsContainer } from "matrix-js-sdk/src/models/relations-container" ;
29
29
30
30
import { RelationsHelper , RelationsHelperEvent } from "../../src/events/RelationsHelper" ;
31
- import { mkEvent , mkStubRoom , stubClient } from "../test-utils" ;
31
+ import { mkEvent , mkRelationsContainer , mkStubRoom , stubClient } from "../test-utils" ;
32
32
33
33
describe ( "RelationsHelper" , ( ) => {
34
34
const roomId = "!room:example.com" ;
35
+ let userId : string ;
35
36
let event : MatrixEvent ;
36
37
let relatedEvent1 : MatrixEvent ;
37
38
let relatedEvent2 : MatrixEvent ;
39
+ let relatedEvent3 : MatrixEvent ;
38
40
let room : Room ;
39
41
let client : MatrixClient ;
40
42
let relationsHelper : RelationsHelper ;
@@ -46,47 +48,81 @@ describe("RelationsHelper", () => {
46
48
47
49
beforeEach ( ( ) => {
48
50
client = stubClient ( ) ;
51
+ userId = client . getUserId ( ) || "" ;
52
+ mocked ( client . relations ) . mockClear ( ) ;
49
53
room = mkStubRoom ( roomId , "test room" , client ) ;
50
- mocked ( client . getRoom ) . mockImplementation ( ( getRoomId : string ) => {
54
+ mocked ( client . getRoom ) . mockImplementation ( ( getRoomId ? : string ) => {
51
55
if ( getRoomId === roomId ) {
52
56
return room ;
53
57
}
58
+
59
+ return null ;
54
60
} ) ;
55
61
event = mkEvent ( {
56
62
event : true ,
57
63
type : EventType . RoomMessage ,
58
64
room : roomId ,
59
- user : client . getUserId ( ) ,
65
+ user : userId ,
60
66
content : { } ,
61
67
} ) ;
62
68
relatedEvent1 = mkEvent ( {
63
69
event : true ,
64
70
type : EventType . RoomMessage ,
65
71
room : roomId ,
66
- user : client . getUserId ( ) ,
67
- content : { } ,
72
+ user : userId ,
73
+ content : { relatedEvent : 1 } ,
68
74
} ) ;
69
75
relatedEvent2 = mkEvent ( {
70
76
event : true ,
71
77
type : EventType . RoomMessage ,
72
78
room : roomId ,
73
- user : client . getUserId ( ) ,
74
- content : { } ,
79
+ user : userId ,
80
+ content : { relatedEvent : 2 } ,
81
+ } ) ;
82
+ relatedEvent3 = mkEvent ( {
83
+ event : true ,
84
+ type : EventType . RoomMessage ,
85
+ room : roomId ,
86
+ user : userId ,
87
+ content : { relatedEvent : 3 } ,
75
88
} ) ;
76
89
onAdd = jest . fn ( ) ;
90
+ relationsContainer = mkRelationsContainer ( ) ;
77
91
// TODO Michael W: create test utils, remove casts
78
- relationsContainer = {
79
- getChildEventsForEvent : jest . fn ( ) ,
80
- } as unknown as RelationsContainer ;
81
92
relations = {
82
93
getRelations : jest . fn ( ) ,
83
94
on : jest . fn ( ) . mockImplementation ( ( type , l ) => relationsOnAdd = l ) ,
95
+ off : jest . fn ( ) ,
84
96
} as unknown as Relations ;
85
97
timelineSet = {
86
98
relations : relationsContainer ,
87
99
} as unknown as EventTimelineSet ;
88
100
} ) ;
89
101
102
+ afterEach ( ( ) => {
103
+ relationsHelper ?. destroy ( ) ;
104
+ } ) ;
105
+
106
+ describe ( "when there is an event without ID" , ( ) => {
107
+ it ( "should raise an error" , ( ) => {
108
+ jest . spyOn ( event , "getId" ) . mockReturnValue ( undefined ) ;
109
+
110
+ expect ( ( ) => {
111
+ new RelationsHelper ( event , RelationType . Reference , EventType . RoomMessage , client ) ;
112
+ } ) . toThrowError ( "unable to create RelationsHelper: missing event ID" ) ;
113
+ } ) ;
114
+ } ) ;
115
+
116
+ describe ( "when there is an event without room ID" , ( ) => {
117
+ it ( "should raise an error" , ( ) => {
118
+ jest . spyOn ( event , "getRoomId" ) . mockReturnValue ( undefined ) ;
119
+
120
+ expect ( ( ) => {
121
+ new RelationsHelper ( event , RelationType . Reference , EventType . RoomMessage , client ) ;
122
+ } ) . toThrowError ( "unable to create RelationsHelper: missing room ID" ) ;
123
+ } ) ;
124
+ } ) ;
125
+
90
126
describe ( "when there is an event without relations" , ( ) => {
91
127
beforeEach ( ( ) => {
92
128
relationsHelper = new RelationsHelper ( event , RelationType . Reference , EventType . RoomMessage , client ) ;
@@ -118,6 +154,34 @@ describe("RelationsHelper", () => {
118
154
} ) ;
119
155
} ) ;
120
156
157
+ describe ( "when there is an event with two pages server side relations" , ( ) => {
158
+ beforeEach ( ( ) => {
159
+ mocked ( client . relations )
160
+ . mockResolvedValueOnce ( {
161
+ events : [ relatedEvent1 , relatedEvent2 ] ,
162
+ nextBatch : "next" ,
163
+ } )
164
+ . mockResolvedValueOnce ( {
165
+ events : [ relatedEvent3 ] ,
166
+ nextBatch : null ,
167
+ } ) ;
168
+ relationsHelper = new RelationsHelper ( event , RelationType . Reference , EventType . RoomMessage , client ) ;
169
+ relationsHelper . on ( RelationsHelperEvent . Add , onAdd ) ;
170
+ } ) ;
171
+
172
+ describe ( "emitFetchCurrent" , ( ) => {
173
+ beforeEach ( async ( ) => {
174
+ await relationsHelper . emitFetchCurrent ( ) ;
175
+ } ) ;
176
+
177
+ it ( "should emit the server side events" , ( ) => {
178
+ expect ( onAdd ) . toHaveBeenCalledWith ( relatedEvent1 ) ;
179
+ expect ( onAdd ) . toHaveBeenCalledWith ( relatedEvent2 ) ;
180
+ expect ( onAdd ) . toHaveBeenCalledWith ( relatedEvent3 ) ;
181
+ } ) ;
182
+ } ) ;
183
+ } ) ;
184
+
121
185
describe ( "when there is an event with relations" , ( ) => {
122
186
beforeEach ( ( ) => {
123
187
mocked ( room . getUnfilteredTimelineSet ) . mockReturnValue ( timelineSet ) ;
0 commit comments