@@ -13,7 +13,7 @@ limitations under the License.
13
13
*/
14
14
15
15
import React from 'react' ;
16
- import { mount } from 'enzyme' ;
16
+ import { mount , ReactWrapper } from 'enzyme' ;
17
17
import { IPushRule , IPushRules , RuleId , IPusher } from 'matrix-js-sdk/src/matrix' ;
18
18
import { IThreepid , ThreepidMedium } from 'matrix-js-sdk/src/@types/threepids' ;
19
19
import { act } from 'react-dom/test-utils' ;
@@ -23,16 +23,11 @@ import SettingsStore from "../../../../src/settings/SettingsStore";
23
23
import { StandardActions } from '../../../../src/notifications/StandardActions' ;
24
24
import { getMockClientWithEventEmitter } from '../../../test-utils' ;
25
25
26
- jest . mock ( '../../../../src/settings/SettingsStore' , ( ) => ( {
27
- monitorSetting : jest . fn ( ) ,
28
- getValue : jest . fn ( ) ,
29
- setValue : jest . fn ( ) ,
30
- } ) ) ;
31
-
32
26
// don't pollute test output with error logs from mock rejections
33
27
jest . mock ( "matrix-js-sdk/src/logger" ) ;
34
28
35
- jest . useRealTimers ( ) ;
29
+ // Avoid indirectly importing any eagerly created stores that would require extra setup
30
+ jest . mock ( "../../../../src/Notifier" ) ;
36
31
37
32
const masterRule = {
38
33
actions : [ "dont_notify" ] ,
@@ -81,21 +76,13 @@ describe('<Notifications />', () => {
81
76
mockClient . getPushers . mockClear ( ) . mockResolvedValue ( { pushers : [ ] } ) ;
82
77
mockClient . getThreePids . mockClear ( ) . mockResolvedValue ( { threepids : [ ] } ) ;
83
78
mockClient . setPusher . mockClear ( ) . mockResolvedValue ( { } ) ;
84
-
85
- ( SettingsStore . getValue as jest . Mock ) . mockClear ( ) . mockReturnValue ( true ) ;
86
- ( SettingsStore . setValue as jest . Mock ) . mockClear ( ) . mockResolvedValue ( true ) ;
87
79
} ) ;
88
80
89
81
it ( 'renders spinner while loading' , ( ) => {
90
82
const component = getComponent ( ) ;
91
83
expect ( component . find ( '.mx_Spinner' ) . length ) . toBeTruthy ( ) ;
92
84
} ) ;
93
85
94
- it ( 'renders error message when fetching push rules fails' , async ( ) => {
95
- mockClient . getPushRules . mockRejectedValue ( { } ) ;
96
- const component = await getComponentAndWait ( ) ;
97
- expect ( findByTestId ( component , 'error-message' ) . length ) . toBeTruthy ( ) ;
98
- } ) ;
99
86
it ( 'renders error message when fetching push rules fails' , async ( ) => {
100
87
mockClient . getPushRules . mockRejectedValue ( { } ) ;
101
88
const component = await getComponentAndWait ( ) ;
@@ -221,17 +208,24 @@ describe('<Notifications />', () => {
221
208
} ) ;
222
209
} ) ;
223
210
224
- it ( 'sets settings value on toggle click ' , async ( ) => {
211
+ it ( 'toggles and sets settings correctly ' , async ( ) => {
225
212
const component = await getComponentAndWait ( ) ;
213
+ let audioNotifsToggle : ReactWrapper ;
226
214
227
- const audioNotifsToggle = findByTestId ( component , 'notif-setting-audioNotificationsEnabled' )
228
- . find ( 'div[role="switch"]' ) ;
215
+ const update = ( ) => {
216
+ audioNotifsToggle = findByTestId ( component , 'notif-setting-audioNotificationsEnabled' )
217
+ . find ( 'div[role="switch"]' ) ;
218
+ } ;
219
+ update ( ) ;
229
220
230
- await act ( async ( ) => {
231
- audioNotifsToggle . simulate ( 'click' ) ;
232
- } ) ;
221
+ expect ( audioNotifsToggle . getDOMNode < HTMLElement > ( ) . getAttribute ( "aria-checked" ) ) . toEqual ( "true" ) ;
222
+ expect ( SettingsStore . getValue ( "audioNotificationsEnabled" ) ) . toEqual ( true ) ;
223
+
224
+ act ( ( ) => { audioNotifsToggle . simulate ( 'click' ) ; } ) ;
225
+ update ( ) ;
233
226
234
- expect ( SettingsStore . setValue ) . toHaveBeenCalledWith ( 'audioNotificationsEnabled' , null , "device" , false ) ;
227
+ expect ( audioNotifsToggle . getDOMNode < HTMLElement > ( ) . getAttribute ( "aria-checked" ) ) . toEqual ( "false" ) ;
228
+ expect ( SettingsStore . getValue ( "audioNotificationsEnabled" ) ) . toEqual ( false ) ;
235
229
} ) ;
236
230
} ) ;
237
231
0 commit comments