Skip to content

Commit

Permalink
do not pass 'sortOrder' to EuiContextMenuItem in share context menu (#…
Browse files Browse the repository at this point in the history
…26890)

* do not pass 'sortOrder' to EuiContextMenuItem in share context menu

* add unit test for sortOrder

* avoid using lodash

* fix merge conflicts with internationization PR
  • Loading branch information
nreese authored Dec 11, 2018
1 parent 56fcc6d commit f06ec83
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shareContextMenuExtensions should sort ascending on sort order first and then ascending on name 1`] = `
<EuiContextMenu
data-test-subj="shareContextMenu"
initialPanelId={4}
panels={
Array [
Object {
"content": <InjectIntl(UrlPanelContentUI)
getUnhashableStates={[Function]}
objectType="dashboard"
/>,
"id": 1,
"title": "Permalink",
},
Object {
"content": <div>
panel content
</div>,
"id": 2,
"title": "AAA panel",
},
Object {
"content": <div>
panel content
</div>,
"id": 3,
"title": "ZZZ panel",
},
Object {
"id": 4,
"items": Array [
Object {
"data-test-subj": "sharePanel-Permalinks",
"icon": "link",
"name": "Permalinks",
"panel": 1,
},
Object {
"data-test-subj": "sharePanel-ZZZpanel",
"name": "ZZZ panel",
"panel": 3,
},
Object {
"data-test-subj": "sharePanel-AAApanel",
"name": "AAA panel",
"panel": 2,
},
],
"title": "Share this dashboard",
},
]
}
/>
`;

exports[`should only render permalink panel when there are no other panels 1`] = `
<EuiContextMenu
data-test-subj="shareContextMenu"
Expand Down Expand Up @@ -50,14 +105,12 @@ exports[`should render context menu panel when there are more than one panel 1`]
"icon": "console",
"name": "Embed code",
"panel": 2,
"sortOrder": 0,
},
Object {
"data-test-subj": "sharePanel-Permalinks",
"icon": "link",
"name": "Permalinks",
"panel": 1,
"sortOrder": 0,
},
],
"title": "Share this dashboard",
Expand Down
47 changes: 47 additions & 0 deletions src/ui/public/share/components/share_context_menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,50 @@ test('should only render permalink panel when there are no other panels', () =>
/>);
expect(component).toMatchSnapshot();
});

describe('shareContextMenuExtensions', () => {
const shareContextMenuExtensions = [
{
getShareActions: () => {
return [
{
panel: {
title: 'AAA panel',
content: (<div>panel content</div>),
},
shareMenuItem: {
name: 'AAA panel',
sortOrder: 5,
}
}
];
}
},
{
getShareActions: () => {
return [
{
panel: {
title: 'ZZZ panel',
content: (<div>panel content</div>),
},
shareMenuItem: {
name: 'ZZZ panel',
sortOrder: 0,
}
}
];
}
}
];

test('should sort ascending on sort order first and then ascending on name', () => {
const component = shallowWithIntl(<ShareContextMenu.WrappedComponent
allowEmbed={false}
objectType="dashboard"
getUnhashableStates={() => {}}
shareContextMenuExtensions={shareContextMenuExtensions}
/>);
expect(component).toMatchSnapshot();
});
});
18 changes: 9 additions & 9 deletions src/ui/public/share/components/share_context_menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,25 @@ class ShareContextMenuUI extends Component<Props> {
}
),
items: menuItems
.map(menuItem => {
menuItem['data-test-subj'] = `sharePanel-${menuItem.name.replace(' ', '')}`;
if (!menuItem.sortOrder) {
menuItem.sortOrder = 0;
}
return menuItem;
})
// Sorts ascending on sort order first and then ascending on name
.sort((a, b) => {
if (a.sortOrder > b.sortOrder) {
const aSortOrder = a.sortOrder || 0;
const bSortOrder = b.sortOrder || 0;
if (aSortOrder > bSortOrder) {
return 1;
}
if (a.sortOrder < b.sortOrder) {
if (aSortOrder < bSortOrder) {
return -1;
}
if (a.name.toLowerCase().localeCompare(b.name.toLowerCase()) > 0) {
return 1;
}
return -1;
})
.map(menuItem => {
menuItem['data-test-subj'] = `sharePanel-${menuItem.name.replace(' ', '')}`;
delete menuItem.sortOrder;
return menuItem;
}),
};
panels.push(topLevelMenuPanel);
Expand Down

0 comments on commit f06ec83

Please sign in to comment.