@@ -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" ] ,
@@ -76,26 +71,20 @@ describe('<Notifications />', () => {
7671
7772 const findByTestId = ( component , id ) => component . find ( `[data-test-id="${ id } "]` ) ;
7873
74+ const setValueSpy = jest . spyOn ( SettingsStore , "setValue" ) ;
75+
7976 beforeEach ( ( ) => {
8077 mockClient . getPushRules . mockClear ( ) . mockResolvedValue ( pushRules ) ;
8178 mockClient . getPushers . mockClear ( ) . mockResolvedValue ( { pushers : [ ] } ) ;
8279 mockClient . getThreePids . mockClear ( ) . mockResolvedValue ( { threepids : [ ] } ) ;
8380 mockClient . setPusher . mockClear ( ) . mockResolvedValue ( { } ) ;
84-
85- ( SettingsStore . getValue as jest . Mock ) . mockClear ( ) . mockReturnValue ( true ) ;
86- ( SettingsStore . setValue as jest . Mock ) . mockClear ( ) . mockResolvedValue ( true ) ;
8781 } ) ;
8882
8983 it ( 'renders spinner while loading' , ( ) => {
9084 const component = getComponent ( ) ;
9185 expect ( component . find ( '.mx_Spinner' ) . length ) . toBeTruthy ( ) ;
9286 } ) ;
9387
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- } ) ;
9988 it ( 'renders error message when fetching push rules fails' , async ( ) => {
10089 mockClient . getPushRules . mockRejectedValue ( { } ) ;
10190 const component = await getComponentAndWait ( ) ;
@@ -221,17 +210,23 @@ describe('<Notifications />', () => {
221210 } ) ;
222211 } ) ;
223212
224- it ( 'sets settings value on toggle click ' , async ( ) => {
213+ it ( 'toggles and sets settings correctly ' , async ( ) => {
225214 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 ( ) ;
226222
227- const audioNotifsToggle = findByTestId ( component , 'notif-setting-audioNotificationsEnabled' )
228- . find ( 'div[role="switch"]' ) ;
223+ expect ( audioNotifsToggle . getDOMNode < HTMLElement > ( ) . getAttribute ( "aria-checked" ) ) . toEqual ( "true" ) ;
229224
230- await act ( async ( ) => {
231- audioNotifsToggle . simulate ( 'click' ) ;
232- } ) ;
225+ act ( ( ) => { audioNotifsToggle . simulate ( 'click' ) ; } ) ;
226+ update ( ) ;
233227
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 ) ;
235230 } ) ;
236231 } ) ;
237232
0 commit comments