Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spaces] Open 'manage roles' link for spaces assign role flyout in new tab #199506

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { EuiThemeProvider } from '@elastic/eui';
import { render, screen, waitFor, within } from '@testing-library/react';
import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import crypto from 'crypto';
import React from 'react';
Expand Down Expand Up @@ -142,6 +142,15 @@ describe('PrivilegesRolesForm', () => {
jest.clearAllMocks();
});

it("would open the 'manage roles' link in a new tab", () => {
getRolesSpy.mockResolvedValue([]);
getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures));

renderPrivilegeRolesForm();

expect(screen.getByText('Manage roles')).toHaveAttribute('target', '_blank');
});

it('does not display the privilege selection buttons or customization form when no role is selected', async () => {
getRolesSpy.mockResolvedValue([]);
getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures));
Expand Down Expand Up @@ -170,6 +179,23 @@ describe('PrivilegesRolesForm', () => {
expect(screen.getByTestId('space-assign-role-create-roles-privilege-button')).toBeDisabled();
});

it('makes a request to refetch available roles if page transitions back from a user interaction page visibility change', () => {
getRolesSpy.mockResolvedValue([]);
getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures));

renderPrivilegeRolesForm();

expect(getRolesSpy).toHaveBeenCalledTimes(1);

// trigger click on manage roles link, which is perquisite for page visibility handler to trigger role refetch
fireEvent.click(screen.getByText(/manage roles/i));

// trigger page visibility change
fireEvent(document, new Event('visibilitychange'));

expect(getRolesSpy).toHaveBeenCalledTimes(2);
});

it('renders with the assign roles button disabled when no base privileges or feature privileges are selected', async () => {
getRolesSpy.mockResolvedValue([]);
getAllKibanaPrivilegeSpy.mockResolvedValue(createRawKibanaPrivileges(kibanaFeatures));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
EuiSpacer,
EuiText,
EuiTitle,
useGeneratedHtmlId,
} from '@elastic/eui';
import type { EuiComboBoxOptionOption } from '@elastic/eui';
import type { FC } from 'react';
Expand Down Expand Up @@ -81,10 +82,13 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
const [selectedRoles, setSelectedRoles] = useState<ReturnType<typeof createRolesComboBoxOptions>>(
createRolesComboBoxOptions(defaultSelected)
);
const manageRoleLinkId = useGeneratedHtmlId();
const isEditOperation = useRef(Boolean(defaultSelected.length));

useEffect(() => {
async function fetchRequiredData(spaceId: string) {
const userInvokedPageVisibilityChange = useRef<boolean | null>(null);

const fetchRequiredData = useCallback(
async (spaceId: string) => {
setFetchingDataDeps(true);

const [systemRoles, _kibanaPrivileges] = await invokeClient((clients) =>
Expand All @@ -109,10 +113,28 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
);

setKibanaPrivileges(_kibanaPrivileges);
}
},
[invokeClient]
);

useEffect(() => {
fetchRequiredData(space.id!).finally(() => setFetchingDataDeps(false));
}, [invokeClient, space.id]);
}, [fetchRequiredData, invokeClient, space.id]);

useEffect(() => {
async function visibilityChangeHandler() {
// page just transitioned back to visible state from hidden state caused by user interaction
if (userInvokedPageVisibilityChange.current && !document.hidden) {
await fetchRequiredData(space.id!).finally(() => setFetchingDataDeps(false));
}
}

document.addEventListener('visibilitychange', visibilityChangeHandler);

return () => {
document.removeEventListener('visibilitychange', visibilityChangeHandler);
};
}, [fetchRequiredData, invokeClient, space.id]);

const selectedRolesCombinedPrivileges = useMemo(() => {
const combinedPrivilege = new Set(
Expand Down Expand Up @@ -345,12 +367,23 @@ export const PrivilegesRolesForm: FC<PrivilegesRolesFormProps> = (props) => {
<React.Fragment>
{!isEditOperation.current && (
<EuiFormRow
onClick={(event: React.MouseEvent<HTMLFieldSetElement>) => {
// leverage event propagation, check if manage role link element was clicked
if ((event.target as HTMLFieldSetElement).id === manageRoleLinkId) {
userInvokedPageVisibilityChange.current = true;
}
}}
label={i18n.translate(
'xpack.spaces.management.spaceDetails.roles.selectRolesFormRowLabel',
{ defaultMessage: 'Select roles' }
)}
labelAppend={
<EuiLink href={getUrlForApp('management', { deepLinkId: 'roles' })}>
<EuiLink
id={manageRoleLinkId}
href={getUrlForApp('management', { deepLinkId: 'roles' })}
external={false}
target="_blank"
>
{i18n.translate(
'xpack.spaces.management.spaceDetails.roles.selectRolesFormRowLabelAnchor',
{ defaultMessage: 'Manage roles' }
Expand Down