@@ -16,7 +16,7 @@ limitations under the License.
16
16
17
17
import { mocked , MockedObject } from "jest-mock" ;
18
18
import { last } from "lodash" ;
19
- import { MatrixEvent , MatrixClient , ClientEvent } from "matrix-js-sdk/src/matrix" ;
19
+ import { MatrixEvent , MatrixClient , ClientEvent , EventTimeline } from "matrix-js-sdk/src/matrix" ;
20
20
import { ClientWidgetApi , WidgetApiFromWidgetAction } from "matrix-widget-api" ;
21
21
import { waitFor } from "@testing-library/react" ;
22
22
@@ -88,6 +88,105 @@ describe("StopGapWidget", () => {
88
88
expect ( messaging . feedToDevice ) . toHaveBeenCalledWith ( event . getEffectiveEvent ( ) , false ) ;
89
89
} ) ;
90
90
91
+ describe ( "feed event" , ( ) => {
92
+ let event1 : MatrixEvent ;
93
+ let event2 : MatrixEvent ;
94
+
95
+ beforeEach ( ( ) => {
96
+ event1 = mkEvent ( {
97
+ event : true ,
98
+ id : "$event-id1" ,
99
+ type : "org.example.foo" ,
100
+ user : "@alice:example.org" ,
101
+ content : { hello : "world" } ,
102
+ room : "!1:example.org" ,
103
+ } ) ;
104
+
105
+ event2 = mkEvent ( {
106
+ event : true ,
107
+ id : "$event-id2" ,
108
+ type : "org.example.foo" ,
109
+ user : "@alice:example.org" ,
110
+ content : { hello : "world" } ,
111
+ room : "!1:example.org" ,
112
+ } ) ;
113
+
114
+ const room = mkRoom ( client , "!1:example.org" ) ;
115
+ client . getRoom . mockImplementation ( ( roomId ) => ( roomId === "!1:example.org" ? room : null ) ) ;
116
+ room . getLiveTimeline . mockReturnValue ( {
117
+ getEvents : ( ) : MatrixEvent [ ] => [ event1 , event2 ] ,
118
+ } as unknown as EventTimeline ) ;
119
+
120
+ messaging . feedEvent . mockResolvedValue ( ) ;
121
+ } ) ;
122
+
123
+ it ( "feeds incoming event to the widget" , async ( ) => {
124
+ client . emit ( ClientEvent . Event , event1 ) ;
125
+ expect ( messaging . feedEvent ) . toHaveBeenCalledWith ( event1 . getEffectiveEvent ( ) , "!1:example.org" ) ;
126
+
127
+ client . emit ( ClientEvent . Event , event2 ) ;
128
+ expect ( messaging . feedEvent ) . toHaveBeenCalledTimes ( 2 ) ;
129
+ expect ( messaging . feedEvent ) . toHaveBeenLastCalledWith ( event2 . getEffectiveEvent ( ) , "!1:example.org" ) ;
130
+ } ) ;
131
+
132
+ it ( "should not feed incoming event to the widget if seen already" , async ( ) => {
133
+ client . emit ( ClientEvent . Event , event1 ) ;
134
+ expect ( messaging . feedEvent ) . toHaveBeenCalledWith ( event1 . getEffectiveEvent ( ) , "!1:example.org" ) ;
135
+
136
+ client . emit ( ClientEvent . Event , event2 ) ;
137
+ expect ( messaging . feedEvent ) . toHaveBeenCalledTimes ( 2 ) ;
138
+ expect ( messaging . feedEvent ) . toHaveBeenLastCalledWith ( event2 . getEffectiveEvent ( ) , "!1:example.org" ) ;
139
+
140
+ client . emit ( ClientEvent . Event , event1 ) ;
141
+ expect ( messaging . feedEvent ) . toHaveBeenCalledTimes ( 2 ) ;
142
+ expect ( messaging . feedEvent ) . toHaveBeenLastCalledWith ( event2 . getEffectiveEvent ( ) , "!1:example.org" ) ;
143
+ } ) ;
144
+
145
+ it ( "should not feed incoming event if not in timeline" , ( ) => {
146
+ const event = mkEvent ( {
147
+ event : true ,
148
+ id : "$event-id" ,
149
+ type : "org.example.foo" ,
150
+ user : "@alice:example.org" ,
151
+ content : {
152
+ hello : "world" ,
153
+ } ,
154
+ room : "!1:example.org" ,
155
+ } ) ;
156
+
157
+ client . emit ( ClientEvent . Event , event ) ;
158
+ expect ( messaging . feedEvent ) . toHaveBeenCalledWith ( event . getEffectiveEvent ( ) , "!1:example.org" ) ;
159
+ } ) ;
160
+
161
+ it ( "feeds incoming event that is not in timeline but relates to unknown parent to the widget" , async ( ) => {
162
+ const event = mkEvent ( {
163
+ event : true ,
164
+ id : "$event-idRelation" ,
165
+ type : "org.example.foo" ,
166
+ user : "@alice:example.org" ,
167
+ content : {
168
+ "hello" : "world" ,
169
+ "m.relates_to" : {
170
+ event_id : "$unknown-parent" ,
171
+ rel_type : "m.reference" ,
172
+ } ,
173
+ } ,
174
+ room : "!1:example.org" ,
175
+ } ) ;
176
+
177
+ client . emit ( ClientEvent . Event , event1 ) ;
178
+ expect ( messaging . feedEvent ) . toHaveBeenCalledWith ( event1 . getEffectiveEvent ( ) , "!1:example.org" ) ;
179
+
180
+ client . emit ( ClientEvent . Event , event ) ;
181
+ expect ( messaging . feedEvent ) . toHaveBeenCalledTimes ( 2 ) ;
182
+ expect ( messaging . feedEvent ) . toHaveBeenLastCalledWith ( event . getEffectiveEvent ( ) , "!1:example.org" ) ;
183
+
184
+ client . emit ( ClientEvent . Event , event1 ) ;
185
+ expect ( messaging . feedEvent ) . toHaveBeenCalledTimes ( 2 ) ;
186
+ expect ( messaging . feedEvent ) . toHaveBeenLastCalledWith ( event . getEffectiveEvent ( ) , "!1:example.org" ) ;
187
+ } ) ;
188
+ } ) ;
189
+
91
190
describe ( "when there is a voice broadcast recording" , ( ) => {
92
191
let voiceBroadcastInfoEvent : MatrixEvent ;
93
192
let voiceBroadcastRecording : VoiceBroadcastRecording ;
0 commit comments