Skip to content

Commit 69d39f6

Browse files
Hide tenant when disabled in the account nav button popover (#1768) (#1792)
Signed-off-by: Derek Ho <dxho@amazon.com> (cherry picked from commit 4dc984b) Co-authored-by: Derek Ho <dxho@amazon.com>
1 parent c31e8f3 commit 69d39f6

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

public/apps/account/account-nav-button.tsx

+11-9
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ export function AccountNavButton(props: {
108108
}
109109
/>
110110
</EuiListGroup>
111-
<EuiListGroupItem
112-
color="subdued"
113-
key="tenant"
114-
label={
115-
<EuiText size="xs" id="tenantName">
116-
{resolveTenantName(props.tenant || '', username)}
117-
</EuiText>
118-
}
119-
/>
111+
{isMultiTenancyEnabled && props.config.multitenancy.enabled && (
112+
<EuiListGroupItem
113+
color="subdued"
114+
key="tenant"
115+
label={
116+
<EuiText size="xs" id="tenantName">
117+
{resolveTenantName(props.tenant || '', username)}
118+
</EuiText>
119+
}
120+
/>
121+
)}
120122
</EuiFlexItem>
121123
</EuiFlexGroup>
122124

public/apps/account/test/account-nav-button.test.tsx

+78-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
import { shallow } from 'enzyme';
1717
import React from 'react';
1818
import { AccountNavButton, reloadAfterTenantSwitch } from '../account-nav-button';
19-
import { getShouldShowTenantPopup, setShouldShowTenantPopup } from '../../../utils/storage-utils';
19+
import { getShouldShowTenantPopup } from '../../../utils/storage-utils';
2020
import { getDashboardsInfo } from '../../../utils/dashboards-info-utils';
21+
import { render, fireEvent } from '@testing-library/react';
2122

2223
jest.mock('../../../utils/storage-utils', () => ({
2324
getShouldShowTenantPopup: jest.fn(),
@@ -81,6 +82,10 @@ describe('Account navigation button', () => {
8182
jest.clearAllMocks();
8283
});
8384

85+
afterAll(() => {
86+
useStateSpy.mockRestore();
87+
});
88+
8489
it('renders', () => {
8590
(getDashboardsInfo as jest.Mock).mockImplementationOnce(() => {
8691
return mockDashboardsInfo;
@@ -149,6 +154,10 @@ describe('Account navigation button, multitenancy disabled', () => {
149154
useStateSpy.mockImplementation((init) => [init, setState]);
150155
});
151156

157+
afterAll(() => {
158+
useStateSpy.mockRestore();
159+
});
160+
152161
afterEach(() => {
153162
jest.clearAllMocks();
154163
});
@@ -168,6 +177,74 @@ describe('Account navigation button, multitenancy disabled', () => {
168177
});
169178
});
170179

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+
171248
describe('Reload window after tenant switch', () => {
172249
const originalLocation = window.location;
173250
const mockSetWindowHref = jest.fn();

0 commit comments

Comments
 (0)