@@ -10,28 +10,17 @@ import React from "react";
1010import { fireEvent , render , screen , waitFor , within } from "jest-matrix-react" ;
1111import { EventType , GuestAccess , HistoryVisibility , JoinRule , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
1212import { logger } from "matrix-js-sdk/src/logger" ;
13+ import { mocked } from "jest-mock" ;
1314
1415import SecurityRoomSettingsTab from "../../../../../../../src/components/views/settings/tabs/room/SecurityRoomSettingsTab" ;
1516import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext" ;
1617import SettingsStore from "../../../../../../../src/settings/SettingsStore" ;
17- import {
18- clearAllModals ,
19- flushPromises ,
20- getMockClientWithEventEmitter ,
21- mockClientMethodsUser ,
22- } from "../../../../../../test-utils" ;
18+ import { clearAllModals , flushPromises , stubClient } from "../../../../../../test-utils" ;
2319import { filterBoolean } from "../../../../../../../src/utils/arrays" ;
2420
2521describe ( "<SecurityRoomSettingsTab />" , ( ) => {
2622 const userId = "@alice:server.org" ;
27- const client = getMockClientWithEventEmitter ( {
28- ...mockClientMethodsUser ( userId ) ,
29- getRoom : jest . fn ( ) ,
30- isRoomEncrypted : jest . fn ( ) ,
31- getLocalAliases : jest . fn ( ) . mockReturnValue ( [ ] ) ,
32- sendStateEvent : jest . fn ( ) ,
33- getClientWellKnown : jest . fn ( ) ,
34- } ) ;
23+ const client = mocked ( stubClient ( ) ) ;
3524 const roomId = "!room:server.org" ;
3625
3726 const getComponent = ( room : Room , closeSettingsFn = jest . fn ( ) ) =>
@@ -96,11 +85,12 @@ describe("<SecurityRoomSettingsTab />", () => {
9685 describe ( "join rule" , ( ) => {
9786 it ( "warns when trying to make an encrypted room public" , async ( ) => {
9887 const room = new Room ( roomId , client , userId ) ;
99- client . isRoomEncrypted . mockReturnValue ( true ) ;
88+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
10089 setRoomStateEvents ( room , JoinRule . Invite ) ;
10190
10291 getComponent ( room ) ;
10392
93+ await waitFor ( ( ) => expect ( screen . getByLabelText ( "Encrypted" ) ) . toBeChecked ( ) ) ;
10494 fireEvent . click ( screen . getByLabelText ( "Public" ) ) ;
10595
10696 const modal = await screen . findByRole ( "dialog" ) ;
@@ -244,19 +234,21 @@ describe("<SecurityRoomSettingsTab />", () => {
244234 expect ( screen . getByDisplayValue ( HistoryVisibility . Shared ) ) . toBeChecked ( ) ;
245235 } ) ;
246236
247- it ( "does not render world readable option when room is encrypted" , ( ) => {
237+ it ( "does not render world readable option when room is encrypted" , async ( ) => {
248238 const room = new Room ( roomId , client , userId ) ;
249- client . isRoomEncrypted . mockReturnValue ( true ) ;
239+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
250240 setRoomStateEvents ( room ) ;
251241
252242 getComponent ( room ) ;
253243
254- expect ( screen . queryByDisplayValue ( HistoryVisibility . WorldReadable ) ) . not . toBeInTheDocument ( ) ;
244+ await waitFor ( ( ) =>
245+ expect ( screen . queryByDisplayValue ( HistoryVisibility . WorldReadable ) ) . not . toBeInTheDocument ( ) ,
246+ ) ;
255247 } ) ;
256248
257249 it ( "renders world readable option when room is encrypted and history is already set to world readable" , ( ) => {
258250 const room = new Room ( roomId , client , userId ) ;
259- client . isRoomEncrypted . mockReturnValue ( true ) ;
251+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
260252 setRoomStateEvents ( room , undefined , undefined , HistoryVisibility . WorldReadable ) ;
261253
262254 getComponent ( room ) ;
@@ -305,13 +297,13 @@ describe("<SecurityRoomSettingsTab />", () => {
305297 } ) ;
306298
307299 describe ( "encryption" , ( ) => {
308- it ( "displays encryption as enabled" , ( ) => {
300+ it ( "displays encryption as enabled" , async ( ) => {
309301 const room = new Room ( roomId , client , userId ) ;
310- client . isRoomEncrypted . mockReturnValue ( true ) ;
302+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
311303 setRoomStateEvents ( room ) ;
312304 getComponent ( room ) ;
313305
314- expect ( screen . getByLabelText ( "Encrypted" ) ) . toBeChecked ( ) ;
306+ await waitFor ( ( ) => expect ( screen . getByLabelText ( "Encrypted" ) ) . toBeChecked ( ) ) ;
315307 // can't disable encryption once enabled
316308 expect ( screen . getByLabelText ( "Encrypted" ) . getAttribute ( "aria-disabled" ) ) . toEqual ( "true" ) ;
317309 } ) ;
@@ -356,7 +348,7 @@ describe("<SecurityRoomSettingsTab />", () => {
356348
357349 it ( "renders world readable option when room is encrypted and history is already set to world readable" , ( ) => {
358350 const room = new Room ( roomId , client , userId ) ;
359- client . isRoomEncrypted . mockReturnValue ( true ) ;
351+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
360352 setRoomStateEvents ( room , undefined , undefined , HistoryVisibility . WorldReadable ) ;
361353
362354 getComponent ( room ) ;
@@ -412,21 +404,20 @@ describe("<SecurityRoomSettingsTab />", () => {
412404 } ) ;
413405 } ) ;
414406
415- it ( "displays encrypted rooms as encrypted" , ( ) => {
407+ it ( "displays encrypted rooms as encrypted" , async ( ) => {
416408 // rooms that are already encrypted still show encrypted
417409 const room = new Room ( roomId , client , userId ) ;
418- client . isRoomEncrypted . mockReturnValue ( true ) ;
410+ jest . spyOn ( client . getCrypto ( ) ! , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( true ) ;
419411 setRoomStateEvents ( room ) ;
420412 getComponent ( room ) ;
421413
422- expect ( screen . getByLabelText ( "Encrypted" ) ) . toBeChecked ( ) ;
414+ await waitFor ( ( ) => expect ( screen . getByLabelText ( "Encrypted" ) ) . toBeChecked ( ) ) ;
423415 expect ( screen . getByLabelText ( "Encrypted" ) . getAttribute ( "aria-disabled" ) ) . toEqual ( "true" ) ;
424416 expect ( screen . getByText ( "Once enabled, encryption cannot be disabled." ) ) . toBeInTheDocument ( ) ;
425417 } ) ;
426418
427419 it ( "displays unencrypted rooms with toggle disabled" , ( ) => {
428420 const room = new Room ( roomId , client , userId ) ;
429- client . isRoomEncrypted . mockReturnValue ( false ) ;
430421 setRoomStateEvents ( room ) ;
431422 getComponent ( room ) ;
432423
0 commit comments