16
16
import { shallow } from 'enzyme' ;
17
17
import React from 'react' ;
18
18
import { AccountNavButton , reloadAfterTenantSwitch } from '../account-nav-button' ;
19
- import { getShouldShowTenantPopup , setShouldShowTenantPopup } from '../../../utils/storage-utils' ;
19
+ import { getShouldShowTenantPopup } from '../../../utils/storage-utils' ;
20
20
import { getDashboardsInfo } from '../../../utils/dashboards-info-utils' ;
21
+ import { render , fireEvent } from '@testing-library/react' ;
21
22
22
23
jest . mock ( '../../../utils/storage-utils' , ( ) => ( {
23
24
getShouldShowTenantPopup : jest . fn ( ) ,
@@ -81,6 +82,10 @@ describe('Account navigation button', () => {
81
82
jest . clearAllMocks ( ) ;
82
83
} ) ;
83
84
85
+ afterAll ( ( ) => {
86
+ useStateSpy . mockRestore ( ) ;
87
+ } ) ;
88
+
84
89
it ( 'renders' , ( ) => {
85
90
( getDashboardsInfo as jest . Mock ) . mockImplementationOnce ( ( ) => {
86
91
return mockDashboardsInfo ;
@@ -149,6 +154,10 @@ describe('Account navigation button, multitenancy disabled', () => {
149
154
useStateSpy . mockImplementation ( ( init ) => [ init , setState ] ) ;
150
155
} ) ;
151
156
157
+ afterAll ( ( ) => {
158
+ useStateSpy . mockRestore ( ) ;
159
+ } ) ;
160
+
152
161
afterEach ( ( ) => {
153
162
jest . clearAllMocks ( ) ;
154
163
} ) ;
@@ -168,6 +177,74 @@ describe('Account navigation button, multitenancy disabled', () => {
168
177
} ) ;
169
178
} ) ;
170
179
180
+ describe ( 'Shows tenant info when multitenancy enabled, and hides it if disabled' , ( ) => {
181
+ test ( 'Renders "switch-tenants" and "tenant-name" when multi-tenancy is enabled' , ( ) => {
182
+ const props = {
183
+ coreStart : { } ,
184
+ isInternalUser : true ,
185
+ username : 'example_user' ,
186
+ tenant : 'example_tenant' ,
187
+ config : {
188
+ multitenancy : { enabled : true } ,
189
+ auth : {
190
+ type : 'dummy' ,
191
+ } ,
192
+ } ,
193
+ } ;
194
+
195
+ // Render the component
196
+ const { container, queryByText } = render ( < AccountNavButton { ...props } /> ) ;
197
+
198
+ // Find the popover component by data-test-subj
199
+ const popoverElement = container . querySelector ( '[data-test-subj="account-popover"]' ) ;
200
+
201
+ // Assert that the popover component is rendered
202
+ expect ( popoverElement ) . toBeDefined ( ) ;
203
+
204
+ // Now, simulate a click event on the button to open the popover
205
+ fireEvent . click ( popoverElement ! ) ;
206
+
207
+ const tenantName = queryByText ( 'example_tenant' ) ;
208
+ expect ( tenantName ) . not . toBeNull ( ) ;
209
+
210
+ const tenantSwitch = queryByText ( 'Switch tenants' ) ;
211
+ expect ( tenantSwitch ) . not . toBeNull ( ) ;
212
+ } ) ;
213
+
214
+ test ( 'Does not render "switch-tenants" and "tenant-name" when multi-tenancy is disabled' , ( ) => {
215
+ const props = {
216
+ coreStart : { } ,
217
+ isInternalUser : true ,
218
+ username : 'example_user' ,
219
+ tenant : 'example_tenant' ,
220
+ config : {
221
+ multitenancy : { enabled : false } ,
222
+ auth : {
223
+ type : 'dummy' ,
224
+ } ,
225
+ } ,
226
+ } ;
227
+
228
+ // Render the component
229
+ const { container, queryByText } = render ( < AccountNavButton { ...props } /> ) ;
230
+
231
+ // Find the popover component by data-test-subj
232
+ const popoverElement = container . querySelector ( '[data-test-subj="account-popover"]' ) ;
233
+
234
+ // Assert that the popover component is rendered
235
+ expect ( popoverElement ) . toBeDefined ( ) ;
236
+
237
+ // Now, simulate a click event on the button to open the popover
238
+ fireEvent . click ( popoverElement ! ) ;
239
+
240
+ const tenantName = queryByText ( 'example_tenant' ) ;
241
+ expect ( tenantName ) . toBeNull ( ) ;
242
+
243
+ const tenantSwitch = queryByText ( 'Switch tenants' ) ;
244
+ expect ( tenantSwitch ) . toBeNull ( ) ;
245
+ } ) ;
246
+ } ) ;
247
+
171
248
describe ( 'Reload window after tenant switch' , ( ) => {
172
249
const originalLocation = window . location ;
173
250
const mockSetWindowHref = jest . fn ( ) ;
0 commit comments