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