@@ -16,8 +16,8 @@ limitations under the License.
1616
1717import { mocked , MockedObject } from "jest-mock" ;
1818import { 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" ;
2121import { SyncState } from "matrix-js-sdk/src/sync" ;
2222import { waitFor } from "@testing-library/react" ;
2323
@@ -60,12 +60,19 @@ describe("Notifier", () => {
6060 let mockClient : MockedObject < MatrixClient > ;
6161 let testRoom : Room ;
6262 let accountDataEventKey : string ;
63- let accountDataStore = { } ;
63+ let accountDataStore : Record < string , MatrixEvent | undefined > = { } ;
6464
6565 let mockSettings : Record < string , boolean > = { } ;
6666
6767 const userId = "@bob:example.org" ;
6868
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+
6976 beforeEach ( ( ) => {
7077 accountDataStore = { } ;
7178 mockClient = getMockClientWithEventEmitter ( {
@@ -150,7 +157,7 @@ describe("Notifier", () => {
150157 } ) ;
151158
152159 it ( 'does not create notifications before syncing has started' , ( ) => {
153- mockClient ! . emit ( ClientEvent . Event , event ) ;
160+ emitLiveEvent ( event ) ;
154161
155162 expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
156163 expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
@@ -160,7 +167,30 @@ describe("Notifier", () => {
160167 const ownEvent = new MatrixEvent ( { sender : userId } ) ;
161168
162169 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+ } ) ;
164194
165195 expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
166196 expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
@@ -175,15 +205,15 @@ describe("Notifier", () => {
175205 } ) ;
176206
177207 mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
178- mockClient ! . emit ( ClientEvent . Event , event ) ;
208+ emitLiveEvent ( event ) ;
179209
180210 expect ( MockPlatform . displayNotification ) . not . toHaveBeenCalled ( ) ;
181211 expect ( MockPlatform . loudNotification ) . not . toHaveBeenCalled ( ) ;
182212 } ) ;
183213
184214 it ( 'creates desktop notification when enabled' , ( ) => {
185215 mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
186- mockClient ! . emit ( ClientEvent . Event , event ) ;
216+ emitLiveEvent ( event ) ;
187217
188218 expect ( MockPlatform . displayNotification ) . toHaveBeenCalledWith (
189219 testRoom . name ,
@@ -196,7 +226,7 @@ describe("Notifier", () => {
196226
197227 it ( 'creates a loud notification when enabled' , ( ) => {
198228 mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
199- mockClient ! . emit ( ClientEvent . Event , event ) ;
229+ emitLiveEvent ( event ) ;
200230
201231 expect ( MockPlatform . loudNotification ) . toHaveBeenCalledWith (
202232 event , testRoom ,
@@ -212,7 +242,7 @@ describe("Notifier", () => {
212242 } ) ;
213243
214244 mockClient ! . emit ( ClientEvent . Sync , SyncState . Syncing , null ) ;
215- mockClient ! . emit ( ClientEvent . Event , event ) ;
245+ emitLiveEvent ( event ) ;
216246
217247 // desktop notification created
218248 expect ( MockPlatform . displayNotification ) . toHaveBeenCalled ( ) ;
@@ -222,12 +252,13 @@ describe("Notifier", () => {
222252 } ) ;
223253
224254 describe ( "_displayPopupNotification" , ( ) => {
225- it . each ( [
255+ const testCases : { event : IContent | undefined , count : number } [ ] = [
226256 { event : { is_silenced : true } , count : 0 } ,
227257 { event : { is_silenced : false } , count : 1 } ,
228258 { 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 ! ) ;
231262 Notifier . _displayPopupNotification ( testEvent , testRoom ) ;
232263 expect ( MockPlatform . displayNotification ) . toHaveBeenCalledTimes ( count ) ;
233264 } ) ;
@@ -243,16 +274,17 @@ describe("Notifier", () => {
243274 } ) ;
244275
245276 describe ( "_playAudioNotification" , ( ) => {
246- it . each ( [
277+ const testCases : { event : IContent | undefined , count : number } [ ] = [
247278 { event : { is_silenced : true } , count : 0 } ,
248279 { event : { is_silenced : false } , count : 1 } ,
249280 { 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 } ) => {
251283 // It's not ideal to only look at whether this function has been called
252284 // but avoids starting to look into DOM stuff
253285 Notifier . getSoundForRoom = jest . fn ( ) ;
254286
255- mockClient . setAccountData ( accountDataEventKey , event ) ;
287+ mockClient . setAccountData ( accountDataEventKey , event ! ) ;
256288 Notifier . _playAudioNotification ( testEvent , testRoom ) ;
257289 expect ( Notifier . getSoundForRoom ) . toHaveBeenCalledTimes ( count ) ;
258290 } ) ;
@@ -267,7 +299,7 @@ describe("Notifier", () => {
267299 notify : true ,
268300 tweaks : { } ,
269301 } ) ;
270-
302+ Notifier . start ( ) ;
271303 Notifier . onSyncStateChange ( SyncState . Syncing ) ;
272304 } ) ;
273305
@@ -283,7 +315,7 @@ describe("Notifier", () => {
283315 content : { } ,
284316 event : true ,
285317 } ) ;
286- Notifier . onEvent ( callEvent ) ;
318+ emitLiveEvent ( callEvent ) ;
287319 return callEvent ;
288320 } ;
289321
0 commit comments