Commit 9e51b1b2 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '301143-fix-render-data-and-class-in-menu-item' into 'master'

Fix missing data and class in top_nav_menu_item

See merge request gitlab-org/gitlab!62172
parents 7e6ebcdf e0fb36f1
<script> <script>
import { GlButton, GlIcon } from '@gitlab/ui'; import { GlButton, GlIcon } from '@gitlab/ui';
import { kebabCase, mapKeys } from 'lodash';
const getDataKey = (key) => `data-${kebabCase(key)}`;
export default { export default {
components: { components: {
...@@ -12,6 +15,11 @@ export default { ...@@ -12,6 +15,11 @@ export default {
required: true, required: true,
}, },
}, },
computed: {
dataAttrs() {
return mapKeys(this.menuItem.data || {}, (value, key) => getDataKey(key));
},
},
}; };
</script> </script>
...@@ -20,6 +28,8 @@ export default { ...@@ -20,6 +28,8 @@ export default {
category="tertiary" category="tertiary"
:href="menuItem.href" :href="menuItem.href"
class="top-nav-menu-item gl-display-block" class="top-nav-menu-item gl-display-block"
:class="menuItem.css_class"
v-bind="dataAttrs"
v-on="$listeners" v-on="$listeners"
> >
<span class="gl-display-flex"> <span class="gl-display-flex">
......
...@@ -132,7 +132,7 @@ module Nav ...@@ -132,7 +132,7 @@ module Nav
active: active_nav_link?(controller: 'admin/sessions'), active: active_nav_link?(controller: 'admin/sessions'),
icon: 'lock-open', icon: 'lock-open',
href: destroy_admin_session_path, href: destroy_admin_session_path,
method: :post data: { method: 'post' }
) )
elsif current_user.admin? elsif current_user.admin?
builder.add_secondary_menu_item( builder.add_secondary_menu_item(
......
...@@ -8,14 +8,13 @@ module Gitlab ...@@ -8,14 +8,13 @@ module Gitlab
# this is already :/. We could also take a hash and manually check every # this is already :/. We could also take a hash and manually check every
# entry, but it's much more maintainable to do rely on native Ruby. # entry, but it's much more maintainable to do rely on native Ruby.
# rubocop: disable Metrics/ParameterLists # rubocop: disable Metrics/ParameterLists
def self.build(id:, title:, active: false, icon: '', href: '', method: nil, view: '', css_class: '', data: {}) def self.build(id:, title:, active: false, icon: '', href: '', view: '', css_class: '', data: {})
{ {
id: id, id: id,
title: title, title: title,
active: active, active: active,
icon: icon, icon: icon,
href: href, href: href,
method: method,
view: view.to_s, view: view.to_s,
css_class: css_class, css_class: css_class,
data: data data: data
......
...@@ -7,6 +7,8 @@ const TEST_MENU_ITEM = { ...@@ -7,6 +7,8 @@ const TEST_MENU_ITEM = {
icon: 'search', icon: 'search',
href: '/pretty/good/burger', href: '/pretty/good/burger',
view: 'burger-view', view: 'burger-view',
css_class: 'test-super-crazy test-class',
data: { qa_selector: 'not-a-real-selector', method: 'post', testFoo: 'test' },
}; };
describe('~/nav/components/top_nav_menu_item.vue', () => { describe('~/nav/components/top_nav_menu_item.vue', () => {
...@@ -47,6 +49,22 @@ describe('~/nav/components/top_nav_menu_item.vue', () => { ...@@ -47,6 +49,22 @@ describe('~/nav/components/top_nav_menu_item.vue', () => {
expect(button.text()).toBe(TEST_MENU_ITEM.title); expect(button.text()).toBe(TEST_MENU_ITEM.title);
}); });
it('renders button classes', () => {
const button = findButton();
expect(button.classes()).toEqual(expect.arrayContaining(TEST_MENU_ITEM.css_class.split(' ')));
});
it('renders button data attributes', () => {
const button = findButton();
expect(button.attributes()).toMatchObject({
'data-qa-selector': TEST_MENU_ITEM.data.qa_selector,
'data-method': TEST_MENU_ITEM.data.method,
'data-test-foo': TEST_MENU_ITEM.data.testFoo,
});
});
it('passes listeners to button', () => { it('passes listeners to button', () => {
expect(listener).not.toHaveBeenCalled(); expect(listener).not.toHaveBeenCalled();
......
...@@ -424,7 +424,7 @@ RSpec.describe Nav::TopNavHelper do ...@@ -424,7 +424,7 @@ RSpec.describe Nav::TopNavHelper do
title: 'Leave Admin Mode', title: 'Leave Admin Mode',
icon: 'lock-open', icon: 'lock-open',
href: '/admin/session/destroy', href: '/admin/session/destroy',
method: :post data: { method: 'post' }
) )
expect(subject[:secondary].last).to eq(expected_leave_admin_mode_item) expect(subject[:secondary].last).to eq(expected_leave_admin_mode_item)
end end
......
...@@ -11,7 +11,6 @@ RSpec.describe ::Gitlab::Nav::TopNavMenuItem do ...@@ -11,7 +11,6 @@ RSpec.describe ::Gitlab::Nav::TopNavMenuItem do
active: true, active: true,
icon: 'icon', icon: 'icon',
href: 'href', href: 'href',
method: 'method',
view: 'view', view: 'view',
css_class: 'css_class', css_class: 'css_class',
data: {} data: {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment