@@ -15,8 +15,9 @@ limitations under the License.
15
15
*/
16
16
17
17
import 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" ;
20
21
21
22
import UnwrappedUserMenu from "../../../src/components/structures/UserMenu" ;
22
23
import { stubClient , wrapInSdkContext } from "../../test-utils" ;
@@ -27,65 +28,153 @@ import {
27
28
} from "../../../src/voice-broadcast" ;
28
29
import { mkVoiceBroadcastInfoStateEvent } from "../../voice-broadcast/utils/test-utils" ;
29
30
import { 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" ;
30
34
31
35
describe ( "<UserMenu>" , ( ) => {
32
36
let client : MatrixClient ;
33
37
let renderResult : RenderResult ;
34
38
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
- } ) ;
48
39
49
40
beforeEach ( ( ) => {
50
41
sdkContext = new TestSdkContext ( ) ;
51
- voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore ( ) ;
52
- sdkContext . _VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore ;
53
-
54
- voiceBroadcastRecording = new VoiceBroadcastRecording ( voiceBroadcastInfoEvent , client ) ;
55
42
} ) ;
56
43
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
+ ) ;
61
57
} ) ;
62
58
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 ) ;
65
64
} ) ;
66
65
67
- describe ( "and a live voice broadcast starts " , ( ) => {
66
+ describe ( "when rendered " , ( ) => {
68
67
beforeEach ( ( ) => {
69
- act ( ( ) => {
70
- voiceBroadcastRecordingsStore . setCurrent ( voiceBroadcastRecording ) ;
71
- } ) ;
68
+ const UserMenu = wrapInSdkContext ( UnwrappedUserMenu , sdkContext ) ;
69
+ renderResult = render ( < UserMenu isPanelCollapsed = { true } /> ) ;
72
70
} ) ;
73
71
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 ( ) ;
76
74
} ) ;
77
75
78
- describe ( "and the broadcast ends " , ( ) => {
76
+ describe ( "and a live voice broadcast starts " , ( ) => {
79
77
beforeEach ( ( ) => {
80
78
act ( ( ) => {
81
- voiceBroadcastRecordingsStore . clearCurrent ( ) ;
79
+ voiceBroadcastRecordingsStore . setCurrent ( voiceBroadcastRecording ) ;
82
80
} ) ;
83
81
} ) ;
84
82
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
+ } ) ;
87
97
} ) ;
88
98
} ) ;
89
99
} ) ;
90
100
} ) ;
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
+ } ) ;
91
180
} ) ;
0 commit comments