@@ -16,8 +16,8 @@ limitations under the License.
16
16
17
17
import { mocked , MockedObject } from "jest-mock" ;
18
18
import { ClientEvent , MatrixClient } from "matrix-js-sdk/src/client" ;
19
- import { Room } from "matrix-js-sdk/src/models/room" ;
20
- import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
19
+ import { Room , RoomEvent } from "matrix-js-sdk/src/models/room" ;
20
+ import { IContent , MatrixEvent } from "matrix-js-sdk/src/models/event" ;
21
21
import { SyncState } from "matrix-js-sdk/src/sync" ;
22
22
import { waitFor } from "@testing-library/react" ;
23
23
@@ -60,12 +60,19 @@ describe("Notifier", () => {
60
60
let mockClient : MockedObject < MatrixClient > ;
61
61
let testRoom : Room ;
62
62
let accountDataEventKey : string ;
63
- let accountDataStore = { } ;
63
+ let accountDataStore : Record < string , MatrixEvent | undefined > = { } ;
64
64
65
65
let mockSettings : Record < string , boolean > = { } ;
66
66
67
67
const userId = "@bob:example.org" ;
68
68
69
+ const emitLiveEvent = ( event : MatrixEvent ) : void => {
70
+ mockClient ! . emit ( RoomEvent . Timeline , event , testRoom , false , false , {
71
+ liveEvent : true ,
72
+ timeline : testRoom . getLiveTimeline ( ) ,
73
+ } ) ;
74
+ } ;
75
+
69
76
beforeEach ( ( ) => {
70
77
accountDataStore = { } ;
71
78
mockClient = getMockClientWithEventEmitter ( {
@@ -150,7 +157,7 @@ describe("Notifier", () => {
150
157
} ) ;
151
158
152
159
it ( 'does not create notifications before syncing has started' , ( ) => {
153
- mockClient ! . emit ( ClientEvent . Event , event ) ;
160
+ emitLiveEvent ( event ) ;
154
161
155
162
expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
156
163
expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
@@ -160,7 +167,30 @@ describe("Notifier", () => {
160
167
const ownEvent = new MatrixEvent ( { sender : userId } ) ;
161
168
162
169
mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
163
- mockClient ! . emit ( ClientEvent . Event , ownEvent ) ;
170
+ emitLiveEvent ( ownEvent ) ;
171
+
172
+ expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
173
+ expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
174
+ } ) ;
175
+
176
+ it ( 'does not create notifications for non-live events (scrollback)' , ( ) => {
177
+ mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
178
+ mockClient ! . emit ( RoomEvent . Timeline , event , testRoom , false , false , {
179
+ liveEvent : false ,
180
+ timeline : testRoom . getLiveTimeline ( ) ,
181
+ } ) ;
182
+
183
+ expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
184
+ expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
185
+ } ) ;
186
+
187
+ it ( 'does not create notifications for rooms which cannot be obtained via client.getRoom' , ( ) => {
188
+ mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
189
+ mockClient . getRoom . mockReturnValue ( null ) ;
190
+ mockClient ! . emit ( RoomEvent . Timeline , event , testRoom , false , false , {
191
+ liveEvent : true ,
192
+ timeline : testRoom . getLiveTimeline ( ) ,
193
+ } ) ;
164
194
165
195
expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
166
196
expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
@@ -175,15 +205,15 @@ describe("Notifier", () => {
175
205
} ) ;
176
206
177
207
mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
178
- mockClient ! . emit ( ClientEvent . Event , event ) ;
208
+ emitLiveEvent ( event ) ;
179
209
180
210
expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
181
211
expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
182
212
} ) ;
183
213
184
214
it ( 'creates desktop notification when enabled' , ( ) => {
185
215
mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
186
- mockClient ! . emit ( ClientEvent . Event , event ) ;
216
+ emitLiveEvent ( event ) ;
187
217
188
218
expect ( MockPlatform . displayNotification ) . toHaveBeenCalledWith (
189
219
testRoom . name ,
@@ -196,7 +226,7 @@ describe("Notifier", () => {
196
226
197
227
it ( 'creates a loud notification when enabled' , ( ) => {
198
228
mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
199
- mockClient ! . emit ( ClientEvent . Event , event ) ;
229
+ emitLiveEvent ( event ) ;
200
230
201
231
expect ( MockPlatform . loudNotification ) . toHaveBeenCalledWith (
202
232
event , testRoom ,
@@ -212,7 +242,7 @@ describe("Notifier", () => {
212
242
} ) ;
213
243
214
244
mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
215
- mockClient ! . emit ( ClientEvent . Event , event ) ;
245
+ emitLiveEvent ( event ) ;
216
246
217
247
// desktop notification created
218
248
expect ( MockPlatform . displayNotification ) . toHaveBeenCalled ( ) ;
@@ -222,12 +252,13 @@ describe("Notifier", () => {
222
252
} ) ;
223
253
224
254
describe ( "_displayPopupNotification" , ( ) => {
225
- it . each ( [
255
+ const testCases : { event : IContent | undefined , count : number } [ ] = [
226
256
{ event : { is_silenced : true } , count : 0 } ,
227
257
{ event : { is_silenced : false } , count : 1 } ,
228
258
{ event : undefined , count : 1 } ,
229
- ] ) ( "does not dispatch when notifications are silenced" , ( { event, count } ) => {
230
- mockClient . setAccountData ( accountDataEventKey , event ) ;
259
+ ] ;
260
+ it . each ( testCases ) ( "does not dispatch when notifications are silenced" , ( { event, count } ) => {
261
+ mockClient . setAccountData ( accountDataEventKey , event ! ) ;
231
262
Notifier . _displayPopupNotification ( testEvent , testRoom ) ;
232
263
expect ( MockPlatform . displayNotification ) . toHaveBeenCalledTimes ( count ) ;
233
264
} ) ;
@@ -243,16 +274,17 @@ describe("Notifier", () => {
243
274
} ) ;
244
275
245
276
describe ( "_playAudioNotification" , ( ) => {
246
- it . each ( [
277
+ const testCases : { event : IContent | undefined , count : number } [ ] = [
247
278
{ event : { is_silenced : true } , count : 0 } ,
248
279
{ event : { is_silenced : false } , count : 1 } ,
249
280
{ event : undefined , count : 1 } ,
250
- ] ) ( "does not dispatch when notifications are silenced" , ( { event, count } ) => {
281
+ ] ;
282
+ it . each ( testCases ) ( "does not dispatch when notifications are silenced" , ( { event, count } ) => {
251
283
// It's not ideal to only look at whether this function has been called
252
284
// but avoids starting to look into DOM stuff
253
285
Notifier . getSoundForRoom = jest . fn ( ) ;
254
286
255
- mockClient . setAccountData ( accountDataEventKey , event ) ;
287
+ mockClient . setAccountData ( accountDataEventKey , event ! ) ;
256
288
Notifier . _playAudioNotification ( testEvent , testRoom ) ;
257
289
expect ( Notifier . getSoundForRoom ) . toHaveBeenCalledTimes ( count ) ;
258
290
} ) ;
@@ -267,7 +299,7 @@ describe("Notifier", () => {
267
299
notify : true ,
268
300
tweaks : { } ,
269
301
} ) ;
270
-
302
+ Notifier . start ( ) ;
271
303
Notifier . onSyncStateChange ( SyncState . Syncing ) ;
272
304
} ) ;
273
305
@@ -283,7 +315,7 @@ describe("Notifier", () => {
283
315
content : { } ,
284
316
event : true ,
285
317
} ) ;
286
- Notifier . onEvent ( callEvent ) ;
318
+ emitLiveEvent ( callEvent ) ;
287
319
return callEvent ;
288
320
} ;
289
321
0 commit comments