@@ -14,20 +14,30 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { MockedObject } from "jest-mock" ;
18
- import { MatrixClient } from "matrix-js-sdk/src/client" ;
17
+ import { mocked , MockedObject } from "jest-mock" ;
18
+ import { ClientEvent , MatrixClient } from "matrix-js-sdk/src/client" ;
19
19
import { Room } from "matrix-js-sdk/src/models/room" ;
20
20
import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
21
+ import { SyncState } from "matrix-js-sdk/src/sync" ;
21
22
22
23
import BasePlatform from "../src/BasePlatform" ;
23
24
import { ElementCall } from "../src/models/Call" ;
24
25
import Notifier from "../src/Notifier" ;
25
26
import SettingsStore from "../src/settings/SettingsStore" ;
26
27
import ToastStore from "../src/stores/ToastStore" ;
27
- import { getLocalNotificationAccountDataEventType } from "../src/utils/notifications" ;
28
+ import {
29
+ createLocalNotificationSettingsIfNeeded ,
30
+ getLocalNotificationAccountDataEventType ,
31
+ } from "../src/utils/notifications" ;
28
32
import { getMockClientWithEventEmitter , mkEvent , mkRoom , mockPlatformPeg } from "./test-utils" ;
29
33
import { IncomingCallToast } from "../src/toasts/IncomingCallToast" ;
30
34
35
+ jest . mock ( "../src/utils/notifications" , ( ) => ( {
36
+ // @ts -ignore
37
+ ...jest . requireActual ( "../src/utils/notifications" ) ,
38
+ createLocalNotificationSettingsIfNeeded : jest . fn ( ) ,
39
+ } ) ) ;
40
+
31
41
describe ( "Notifier" , ( ) => {
32
42
const roomId = "!room1:server" ;
33
43
const testEvent = mkEvent ( {
@@ -111,7 +121,7 @@ describe("Notifier", () => {
111
121
tweaks : { } ,
112
122
} ) ;
113
123
114
- Notifier . onSyncStateChange ( "SYNCING" ) ;
124
+ Notifier . onSyncStateChange ( SyncState . Syncing ) ;
115
125
} ) ;
116
126
117
127
afterEach ( ( ) => {
@@ -169,4 +179,46 @@ describe("Notifier", () => {
169
179
expect ( ToastStore . sharedInstance ( ) . addOrReplaceToast ) . not . toHaveBeenCalled ( ) ;
170
180
} ) ;
171
181
} ) ;
182
+
183
+ describe ( 'local notification settings' , ( ) => {
184
+ const createLocalNotificationSettingsIfNeededMock = mocked ( createLocalNotificationSettingsIfNeeded ) ;
185
+ let hasStartedNotiferBefore = false ;
186
+ beforeEach ( ( ) => {
187
+ // notifier defines some listener functions in start
188
+ // and references them in stop
189
+ // so blows up if stopped before it was started
190
+ if ( hasStartedNotiferBefore ) {
191
+ Notifier . stop ( ) ;
192
+ }
193
+ Notifier . start ( ) ;
194
+ hasStartedNotiferBefore = true ;
195
+ createLocalNotificationSettingsIfNeededMock . mockClear ( ) ;
196
+ } ) ;
197
+
198
+ afterAll ( ( ) => {
199
+ Notifier . stop ( ) ;
200
+ } ) ;
201
+
202
+ it ( 'does not create local notifications event after a sync error' , ( ) => {
203
+ mockClient . emit ( ClientEvent . Sync , SyncState . Error , SyncState . Syncing ) ;
204
+ expect ( createLocalNotificationSettingsIfNeededMock ) . not . toHaveBeenCalled ( ) ;
205
+ } ) ;
206
+
207
+ it ( 'does not create local notifications event after sync stops' , ( ) => {
208
+ mockClient . emit ( ClientEvent . Sync , SyncState . Stopped , SyncState . Syncing ) ;
209
+ expect ( createLocalNotificationSettingsIfNeededMock ) . not . toHaveBeenCalled ( ) ;
210
+ } ) ;
211
+
212
+ it ( 'does not create local notifications event after a cached sync' , ( ) => {
213
+ mockClient . emit ( ClientEvent . Sync , SyncState . Syncing , SyncState . Syncing , {
214
+ fromCache : true ,
215
+ } ) ;
216
+ expect ( createLocalNotificationSettingsIfNeededMock ) . not . toHaveBeenCalled ( ) ;
217
+ } ) ;
218
+
219
+ it ( 'creates local notifications event after a non-cached sync' , ( ) => {
220
+ mockClient . emit ( ClientEvent . Sync , SyncState . Syncing , SyncState . Syncing , { } ) ;
221
+ expect ( createLocalNotificationSettingsIfNeededMock ) . toHaveBeenCalled ( ) ;
222
+ } ) ;
223
+ } ) ;
172
224
} ) ;
0 commit comments