@@ -13,7 +13,7 @@ limitations under the License.
1313*/
1414
1515import React from 'react' ;
16- import { mount } from 'enzyme' ;
16+ import { mount , ReactWrapper } from 'enzyme' ;
1717import { IPushRule , IPushRules , RuleId , IPusher } from 'matrix-js-sdk/src/matrix' ;
1818import { IThreepid , ThreepidMedium } from 'matrix-js-sdk/src/@types/threepids' ;
1919import { act } from 'react-dom/test-utils' ;
@@ -23,16 +23,11 @@ import SettingsStore from "../../../../src/settings/SettingsStore";
2323import { StandardActions } from '../../../../src/notifications/StandardActions' ;
2424import { getMockClientWithEventEmitter } from '../../../test-utils' ;
2525
26- jest . mock ( '../../../../src/settings/SettingsStore' , ( ) => ( {
27- monitorSetting : jest . fn ( ) ,
28- getValue : jest . fn ( ) ,
29- setValue : jest . fn ( ) ,
30- } ) ) ;
31-
3226// don't pollute test output with error logs from mock rejections
3327jest . mock ( "matrix-js-sdk/src/logger" ) ;
3428
35- jest . useRealTimers ( ) ;
29+ // Avoid indirectly importing any eagerly created stores that would require extra setup
30+ jest . mock ( "../../../../src/Notifier" ) ;
3631
3732const masterRule = {
3833 actions : [ "dont_notify" ] ,
@@ -81,21 +76,13 @@ describe('<Notifications />', () => {
8176 mockClient . getPushers . mockClear ( ) . mockResolvedValue ( { pushers : [ ] } ) ;
8277 mockClient . getThreePids . mockClear ( ) . mockResolvedValue ( { threepids : [ ] } ) ;
8378 mockClient . setPusher . mockClear ( ) . mockResolvedValue ( { } ) ;
84-
85- ( SettingsStore . getValue as jest . Mock ) . mockClear ( ) . mockReturnValue ( true ) ;
86- ( SettingsStore . setValue as jest . Mock ) . mockClear ( ) . mockResolvedValue ( true ) ;
8779 } ) ;
8880
8981 it ( 'renders spinner while loading' , ( ) => {
9082 const component = getComponent ( ) ;
9183 expect ( component . find ( '.mx_Spinner' ) . length ) . toBeTruthy ( ) ;
9284 } ) ;
9385
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- } ) ;
9986 it ( 'renders error message when fetching push rules fails' , async ( ) => {
10087 mockClient . getPushRules . mockRejectedValue ( { } ) ;
10188 const component = await getComponentAndWait ( ) ;
@@ -221,17 +208,24 @@ describe('<Notifications />', () => {
221208 } ) ;
222209 } ) ;
223210
224- it ( 'sets settings value on toggle click ' , async ( ) => {
211+ it ( 'toggles and sets settings correctly ' , async ( ) => {
225212 const component = await getComponentAndWait ( ) ;
213+ let audioNotifsToggle : ReactWrapper ;
226214
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 ( ) ;
229220
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 ( ) ;
233226
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 ) ;
235229 } ) ;
236230 } ) ;
237231
0 commit comments