@@ -28,13 +28,15 @@ import { Relations } from "matrix-js-sdk/src/models/relations";
2828import { RelationsContainer } from "matrix-js-sdk/src/models/relations-container" ;
2929
3030import { RelationsHelper , RelationsHelperEvent } from "../../src/events/RelationsHelper" ;
31- import { mkEvent , mkStubRoom , stubClient } from "../test-utils" ;
31+ import { mkEvent , mkRelationsContainer , mkStubRoom , stubClient } from "../test-utils" ;
3232
3333describe ( "RelationsHelper" , ( ) => {
3434 const roomId = "!room:example.com" ;
35+ let userId : string ;
3536 let event : MatrixEvent ;
3637 let relatedEvent1 : MatrixEvent ;
3738 let relatedEvent2 : MatrixEvent ;
39+ let relatedEvent3 : MatrixEvent ;
3840 let room : Room ;
3941 let client : MatrixClient ;
4042 let relationsHelper : RelationsHelper ;
@@ -46,47 +48,81 @@ describe("RelationsHelper", () => {
4648
4749 beforeEach ( ( ) => {
4850 client = stubClient ( ) ;
51+ userId = client . getUserId ( ) || "" ;
52+ mocked ( client . relations ) . mockClear ( ) ;
4953 room = mkStubRoom ( roomId , "test room" , client ) ;
50- mocked ( client . getRoom ) . mockImplementation ( ( getRoomId : string ) => {
54+ mocked ( client . getRoom ) . mockImplementation ( ( getRoomId ? : string ) => {
5155 if ( getRoomId === roomId ) {
5256 return room ;
5357 }
58+
59+ return null ;
5460 } ) ;
5561 event = mkEvent ( {
5662 event : true ,
5763 type : EventType . RoomMessage ,
5864 room : roomId ,
59- user : client . getUserId ( ) ,
65+ user : userId ,
6066 content : { } ,
6167 } ) ;
6268 relatedEvent1 = mkEvent ( {
6369 event : true ,
6470 type : EventType . RoomMessage ,
6571 room : roomId ,
66- user : client . getUserId ( ) ,
67- content : { } ,
72+ user : userId ,
73+ content : { relatedEvent : 1 } ,
6874 } ) ;
6975 relatedEvent2 = mkEvent ( {
7076 event : true ,
7177 type : EventType . RoomMessage ,
7278 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 } ,
7588 } ) ;
7689 onAdd = jest . fn ( ) ;
90+ relationsContainer = mkRelationsContainer ( ) ;
7791 // TODO Michael W: create test utils, remove casts
78- relationsContainer = {
79- getChildEventsForEvent : jest . fn ( ) ,
80- } as unknown as RelationsContainer ;
8192 relations = {
8293 getRelations : jest . fn ( ) ,
8394 on : jest . fn ( ) . mockImplementation ( ( type , l ) => relationsOnAdd = l ) ,
95+ off : jest . fn ( ) ,
8496 } as unknown as Relations ;
8597 timelineSet = {
8698 relations : relationsContainer ,
8799 } as unknown as EventTimelineSet ;
88100 } ) ;
89101
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+
90126 describe ( "when there is an event without relations" , ( ) => {
91127 beforeEach ( ( ) => {
92128 relationsHelper = new RelationsHelper ( event , RelationType . Reference , EventType . RoomMessage , client ) ;
@@ -118,6 +154,34 @@ describe("RelationsHelper", () => {
118154 } ) ;
119155 } ) ;
120156
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+
121185 describe ( "when there is an event with relations" , ( ) => {
122186 beforeEach ( ( ) => {
123187 mocked ( room . getUnfilteredTimelineSet ) . mockReturnValue ( timelineSet ) ;
0 commit comments