Skip to content

Commit

Permalink
Changing nav menu logic to only render the default Edit Profile link …
Browse files Browse the repository at this point in the history
…if there is no custom Edit Profile link passed in by another plugin
  • Loading branch information
kc13greiner committed Jul 18, 2022
1 parent 6b12199 commit 9bfd590
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ describe('SecurityNavControl', () => {
expect(wrapper.prop<boolean>('isOpen')).toEqual(false);
});

it('should render additional user menu links registered by other plugins', async () => {
it('should render additional user menu links registered by other plugins and should render the default Edit Profile link as the first link when no custom profile link is provided', async () => {
const wrapper = shallow(
<SecurityNavControl
editProfileUrl=""
editProfileUrl="edit-profile-link"
logoutUrl=""
userMenuLinks$={
new BehaviorSubject([
Expand All @@ -195,7 +195,7 @@ describe('SecurityNavControl', () => {
"items": Array [
Object {
"data-test-subj": "profileLink",
"href": "",
"href": "edit-profile-link",
"icon": <EuiIcon
size="m"
type="user"
Expand Down Expand Up @@ -254,10 +254,10 @@ describe('SecurityNavControl', () => {
`);
});

it('should render custom profile link registered by other plugins', async () => {
it('should render custom profile link registered by other plugins and not render default Edit Profile link', async () => {
const wrapper = shallow(
<SecurityNavControl
editProfileUrl=""
editProfileUrl="edit-profile-link"
logoutUrl=""
userMenuLinks$={
new BehaviorSubject([
Expand Down Expand Up @@ -307,20 +307,6 @@ describe('SecurityNavControl', () => {
/>,
"name": "link3",
},
Object {
"data-test-subj": "profileLink",
"href": "",
"icon": <EuiIcon
size="m"
type="user"
/>,
"name": <FormattedMessage
defaultMessage="Edit profile"
id="xpack.security.navControlComponent.editProfileLinkText"
values={Object {}}
/>,
"onClick": [Function],
},
Object {
"data-test-subj": "logoutLink",
"href": "",
Expand Down Expand Up @@ -383,92 +369,4 @@ describe('SecurityNavControl', () => {
]
`);
});

it('should not render Edit profile for cloud user', async () => {
useCurrentUserMock.mockReturnValue({
loading: false,
value: mockAuthenticatedUser({
elastic_cloud_user: true,
}),
});

const wrapper = shallow(
<SecurityNavControl editProfileUrl="" logoutUrl="" userMenuLinks$={userMenuLinks$} />
);

expect(wrapper.find(EuiContextMenu).prop('panels')).toMatchInlineSnapshot(`
Array [
Object {
"id": 0,
"items": Array [
Object {
"data-test-subj": "logoutLink",
"href": "",
"icon": <EuiIcon
size="m"
type="exit"
/>,
"name": <FormattedMessage
defaultMessage="Log out"
id="xpack.security.navControlComponent.logoutLinkText"
values={Object {}}
/>,
},
],
"title": "full name",
},
]
`);
});

it('should render Edit profile for non-cloud user', () => {
useCurrentUserMock.mockReturnValue({
loading: false,
value: mockAuthenticatedUser({
elastic_cloud_user: false,
}),
});

const wrapper = shallow(
<SecurityNavControl editProfileUrl="" logoutUrl="" userMenuLinks$={userMenuLinks$} />
);

expect(wrapper.find(EuiContextMenu).prop('panels')).toMatchInlineSnapshot(`
Array [
Object {
"id": 0,
"items": Array [
Object {
"data-test-subj": "profileLink",
"href": "",
"icon": <EuiIcon
size="m"
type="user"
/>,
"name": <FormattedMessage
defaultMessage="Edit profile"
id="xpack.security.navControlComponent.editProfileLinkText"
values={Object {}}
/>,
"onClick": [Function],
},
Object {
"data-test-subj": "logoutLink",
"href": "",
"icon": <EuiIcon
size="m"
type="exit"
/>,
"name": <FormattedMessage
defaultMessage="Log out"
id="xpack.security.navControlComponent.logoutLinkText"
values={Object {}}
/>,
},
],
"title": "full name",
},
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ export const SecurityNavControl: FunctionComponent<SecurityNavControlProps> = ({
}

const isAnonymous = currentUser.value ? isUserAnonymous(currentUser.value) : false;
const isCloudUser = currentUser.value?.elastic_cloud_user;

if (!isAnonymous && !isCloudUser) {
const hasCustomProfileLinks = userMenuLinks.some(({ setAsProfile }) => setAsProfile === true);
const hasCustomProfileLinks = userMenuLinks.some(({ setAsProfile }) => setAsProfile === true);

if (!isAnonymous && !hasCustomProfileLinks) {
const profileMenuItem: EuiContextMenuPanelItemDescriptor = {
name: (
<FormattedMessage
Expand All @@ -114,11 +112,7 @@ export const SecurityNavControl: FunctionComponent<SecurityNavControlProps> = ({
};

// Set this as the first link if there is no user-defined profile link
if (!hasCustomProfileLinks) {
items.unshift(profileMenuItem);
} else {
items.push(profileMenuItem);
}
items.unshift(profileMenuItem);
}

items.push({
Expand Down

0 comments on commit 9bfd590

Please sign in to comment.