@@ -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" ] ,
@@ -76,26 +71,20 @@ describe('<Notifications />', () => {
76
71
77
72
const findByTestId = ( component , id ) => component . find ( `[data-test-id="${ id } "]` ) ;
78
73
74
+ const setValueSpy = jest . spyOn ( SettingsStore , "setValue" ) ;
75
+
79
76
beforeEach ( ( ) => {
80
77
mockClient . getPushRules . mockClear ( ) . mockResolvedValue ( pushRules ) ;
81
78
mockClient . getPushers . mockClear ( ) . mockResolvedValue ( { pushers : [ ] } ) ;
82
79
mockClient . getThreePids . mockClear ( ) . mockResolvedValue ( { threepids : [ ] } ) ;
83
80
mockClient . setPusher . mockClear ( ) . mockResolvedValue ( { } ) ;
84
-
85
- ( SettingsStore . getValue as jest . Mock ) . mockClear ( ) . mockReturnValue ( true ) ;
86
- ( SettingsStore . setValue as jest . Mock ) . mockClear ( ) . mockResolvedValue ( true ) ;
87
81
} ) ;
88
82
89
83
it ( 'renders spinner while loading' , ( ) => {
90
84
const component = getComponent ( ) ;
91
85
expect ( component . find ( '.mx_Spinner' ) . length ) . toBeTruthy ( ) ;
92
86
} ) ;
93
87
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
88
it ( 'renders error message when fetching push rules fails' , async ( ) => {
100
89
mockClient . getPushRules . mockRejectedValue ( { } ) ;
101
90
const component = await getComponentAndWait ( ) ;
@@ -221,17 +210,23 @@ describe('<Notifications />', () => {
221
210
} ) ;
222
211
} ) ;
223
212
224
- it ( 'sets settings value on toggle click ' , async ( ) => {
213
+ it ( 'toggles and sets settings correctly ' , async ( ) => {
225
214
const component = await getComponentAndWait ( ) ;
215
+ let audioNotifsToggle : ReactWrapper ;
216
+
217
+ const update = ( ) => {
218
+ audioNotifsToggle = findByTestId ( component , 'notif-setting-audioNotificationsEnabled' )
219
+ . find ( 'div[role="switch"]' ) ;
220
+ } ;
221
+ update ( ) ;
226
222
227
- const audioNotifsToggle = findByTestId ( component , 'notif-setting-audioNotificationsEnabled' )
228
- . find ( 'div[role="switch"]' ) ;
223
+ expect ( audioNotifsToggle . getDOMNode < HTMLElement > ( ) . getAttribute ( "aria-checked" ) ) . toEqual ( "true" ) ;
229
224
230
- await act ( async ( ) => {
231
- audioNotifsToggle . simulate ( 'click' ) ;
232
- } ) ;
225
+ act ( ( ) => { audioNotifsToggle . simulate ( 'click' ) ; } ) ;
226
+ update ( ) ;
233
227
234
- expect ( SettingsStore . setValue ) . toHaveBeenCalledWith ( 'audioNotificationsEnabled' , null , "device" , false ) ;
228
+ expect ( audioNotifsToggle . getDOMNode < HTMLElement > ( ) . getAttribute ( "aria-checked" ) ) . toEqual ( "false" ) ;
229
+ expect ( setValueSpy ) . toHaveBeenCalledWith ( 'audioNotificationsEnabled' , null , "device" , false ) ;
235
230
} ) ;
236
231
} ) ;
237
232
0 commit comments