@@ -15,8 +15,9 @@ limitations under the License.
1515*/
1616
1717import React from "react" ;
18- import { act , render , RenderResult } from "@testing-library/react" ;
19- import { MatrixClient , MatrixEvent } from "matrix-js-sdk/src/matrix" ;
18+ import { act , render , RenderResult , screen , waitFor } from "@testing-library/react" ;
19+ import { MatrixClient , MatrixEvent , Room } from "matrix-js-sdk/src/matrix" ;
20+ import { mocked } from "jest-mock" ;
2021
2122import UnwrappedUserMenu from "../../../src/components/structures/UserMenu" ;
2223import { stubClient , wrapInSdkContext } from "../../test-utils" ;
@@ -27,65 +28,153 @@ import {
2728} from "../../../src/voice-broadcast" ;
2829import { mkVoiceBroadcastInfoStateEvent } from "../../voice-broadcast/utils/test-utils" ;
2930import { TestSdkContext } from "../../TestSdkContext" ;
31+ import defaultDispatcher from "../../../src/dispatcher/dispatcher" ;
32+ import LogoutDialog from "../../../src/components/views/dialogs/LogoutDialog" ;
33+ import Modal from "../../../src/Modal" ;
3034
3135describe ( "<UserMenu>" , ( ) => {
3236 let client : MatrixClient ;
3337 let renderResult : RenderResult ;
3438 let sdkContext : TestSdkContext ;
35- let voiceBroadcastInfoEvent : MatrixEvent ;
36- let voiceBroadcastRecording : VoiceBroadcastRecording ;
37- let voiceBroadcastRecordingsStore : VoiceBroadcastRecordingsStore ;
38-
39- beforeAll ( ( ) => {
40- client = stubClient ( ) ;
41- voiceBroadcastInfoEvent = mkVoiceBroadcastInfoStateEvent (
42- "!room:example.com" ,
43- VoiceBroadcastInfoState . Started ,
44- client . getUserId ( ) || "" ,
45- client . getDeviceId ( ) || "" ,
46- ) ;
47- } ) ;
4839
4940 beforeEach ( ( ) => {
5041 sdkContext = new TestSdkContext ( ) ;
51- voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore ( ) ;
52- sdkContext . _VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore ;
53-
54- voiceBroadcastRecording = new VoiceBroadcastRecording ( voiceBroadcastInfoEvent , client ) ;
5542 } ) ;
5643
57- describe ( "when rendered" , ( ) => {
58- beforeEach ( ( ) => {
59- const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
60- renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
44+ describe ( "<UserMenu> when video broadcast" , ( ) => {
45+ let voiceBroadcastInfoEvent : MatrixEvent ;
46+ let voiceBroadcastRecording : VoiceBroadcastRecording ;
47+ let voiceBroadcastRecordingsStore : VoiceBroadcastRecordingsStore ;
48+
49+ beforeAll ( ( ) => {
50+ client = stubClient ( ) ;
51+ voiceBroadcastInfoEvent = mkVoiceBroadcastInfoStateEvent (
52+ "!room:example.com" ,
53+ VoiceBroadcastInfoState . Started ,
54+ client . getUserId ( ) || "" ,
55+ client . getDeviceId ( ) || "" ,
56+ ) ;
6157 } ) ;
6258
63- it ( "should render as expected" , ( ) => {
64- expect ( renderResult . container ) . toMatchSnapshot ( ) ;
59+ beforeEach ( ( ) => {
60+ voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore ( ) ;
61+ sdkContext . _VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore ;
62+
63+ voiceBroadcastRecording = new VoiceBroadcastRecording ( voiceBroadcastInfoEvent , client ) ;
6564 } ) ;
6665
67- describe ( "and a live voice broadcast starts " , ( ) => {
66+ describe ( "when rendered " , ( ) => {
6867 beforeEach ( ( ) => {
69- act ( ( ) => {
70- voiceBroadcastRecordingsStore . setCurrent ( voiceBroadcastRecording ) ;
71- } ) ;
68+ const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
69+ renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
7270 } ) ;
7371
74- it ( "should render the live voice broadcast avatar addon " , ( ) => {
75- expect ( renderResult . queryByTestId ( "user-menu-live-vb" ) ) . toBeInTheDocument ( ) ;
72+ it ( "should render as expected " , ( ) => {
73+ expect ( renderResult . container ) . toMatchSnapshot ( ) ;
7674 } ) ;
7775
78- describe ( "and the broadcast ends " , ( ) => {
76+ describe ( "and a live voice broadcast starts " , ( ) => {
7977 beforeEach ( ( ) => {
8078 act ( ( ) => {
81- voiceBroadcastRecordingsStore . clearCurrent ( ) ;
79+ voiceBroadcastRecordingsStore . setCurrent ( voiceBroadcastRecording ) ;
8280 } ) ;
8381 } ) ;
8482
85- it ( "should not render the live voice broadcast avatar addon" , ( ) => {
86- expect ( renderResult . queryByTestId ( "user-menu-live-vb" ) ) . not . toBeInTheDocument ( ) ;
83+ it ( "should render the live voice broadcast avatar addon" , ( ) => {
84+ expect ( renderResult . queryByTestId ( "user-menu-live-vb" ) ) . toBeInTheDocument ( ) ;
85+ } ) ;
86+
87+ describe ( "and the broadcast ends" , ( ) => {
88+ beforeEach ( ( ) => {
89+ act ( ( ) => {
90+ voiceBroadcastRecordingsStore . clearCurrent ( ) ;
91+ } ) ;
92+ } ) ;
93+
94+ it ( "should not render the live voice broadcast avatar addon" , ( ) => {
95+ expect ( renderResult . queryByTestId ( "user-menu-live-vb" ) ) . not . toBeInTheDocument ( ) ;
96+ } ) ;
8797 } ) ;
8898 } ) ;
8999 } ) ;
90100 } ) ;
101+
102+ describe ( "<UserMenu> logout" , ( ) => {
103+ beforeEach ( ( ) => {
104+ client = stubClient ( ) ;
105+ } ) ;
106+
107+ it ( "should logout directly if no crypto" , async ( ) => {
108+ const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
109+ renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
110+
111+ mocked ( client . getRooms ) . mockReturnValue ( [
112+ {
113+ roomId : "!room0" ,
114+ } as unknown as Room ,
115+ {
116+ roomId : "!room1" ,
117+ } as unknown as Room ,
118+ ] ) ;
119+ jest . spyOn ( client , "getCrypto" ) . mockReturnValue ( undefined ) ;
120+
121+ const spy = jest . spyOn ( defaultDispatcher , "dispatch" ) ;
122+ screen . getByRole ( "button" , { name : / U s e r m e n u / i } ) . click ( ) ;
123+ screen . getByRole ( "menuitem" , { name : / S i g n o u t / i } ) . click ( ) ;
124+ await waitFor ( ( ) => {
125+ expect ( spy ) . toHaveBeenCalledWith ( { action : "logout" } ) ;
126+ } ) ;
127+ } ) ;
128+
129+ it ( "should logout directly if no encrypted rooms" , async ( ) => {
130+ const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
131+ renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
132+
133+ mocked ( client . getRooms ) . mockReturnValue ( [
134+ {
135+ roomId : "!room0" ,
136+ } as unknown as Room ,
137+ {
138+ roomId : "!room1" ,
139+ } as unknown as Room ,
140+ ] ) ;
141+ const crypto = client . getCrypto ( ) ! ;
142+
143+ jest . spyOn ( crypto , "isEncryptionEnabledInRoom" ) . mockResolvedValue ( false ) ;
144+
145+ const spy = jest . spyOn ( defaultDispatcher , "dispatch" ) ;
146+ screen . getByRole ( "button" , { name : / U s e r m e n u / i } ) . click ( ) ;
147+ screen . getByRole ( "menuitem" , { name : / S i g n o u t / i } ) . click ( ) ;
148+ await waitFor ( ( ) => {
149+ expect ( spy ) . toHaveBeenCalledWith ( { action : "logout" } ) ;
150+ } ) ;
151+ } ) ;
152+
153+ it ( "should show dialog if some encrypted rooms" , async ( ) => {
154+ const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
155+ renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
156+
157+ mocked ( client . getRooms ) . mockReturnValue ( [
158+ {
159+ roomId : "!room0" ,
160+ } as unknown as Room ,
161+ {
162+ roomId : "!room1" ,
163+ } as unknown as Room ,
164+ ] ) ;
165+ const crypto = client . getCrypto ( ) ! ;
166+
167+ jest . spyOn ( crypto , "isEncryptionEnabledInRoom" ) . mockImplementation ( async ( roomId : string ) => {
168+ return roomId === "!room0" ;
169+ } ) ;
170+
171+ const spy = jest . spyOn ( Modal , "createDialog" ) ;
172+ screen . getByRole ( "button" , { name : / U s e r m e n u / i } ) . click ( ) ;
173+ screen . getByRole ( "menuitem" , { name : / S i g n o u t / i } ) . click ( ) ;
174+
175+ await waitFor ( ( ) => {
176+ expect ( spy ) . toHaveBeenCalledWith ( LogoutDialog ) ;
177+ } ) ;
178+ } ) ;
179+ } ) ;
91180} ) ;
0 commit comments