diff --git a/app/assets/javascripts/rapid_diffs/options_menu/adapter.js b/app/assets/javascripts/rapid_diffs/options_menu/adapter.js index e7cc7f9d2bc784139c9ec4edb6d8f47b69e38d6b..1b4cd638a96f1ec0f6b18f82f6570936da16dd82 100644 --- a/app/assets/javascripts/rapid_diffs/options_menu/adapter.js +++ b/app/assets/javascripts/rapid_diffs/options_menu/adapter.js @@ -1,14 +1,20 @@ import Vue from 'vue'; import { GlDisclosureDropdown } from '@gitlab/ui'; +function getMenuItems(container) { + return JSON.parse(container.querySelector('script').textContent); +} + export const OptionsMenuAdapter = { clicks: { toggleOptionsMenu(event) { const button = event.target.closest('.js-options-button'); + const menuContainer = button.parentElement; + const items = getMenuItems(menuContainer); if (!this.sink.optionsMenu) { this.sink.optionsMenu = new Vue({ - el: Vue.version.startsWith('2') ? button : button.parentElement, + el: Vue.version.startsWith('2') ? button : menuContainer, name: 'GlDisclosureDropdown', render: (createElement = Vue.h) => createElement(GlDisclosureDropdown, { @@ -18,6 +24,7 @@ export const OptionsMenuAdapter = { noCaret: true, category: 'tertiary', size: 'small', + items, }, }), }); diff --git a/app/components/rapid_diffs/diff_file_header_component.html.haml b/app/components/rapid_diffs/diff_file_header_component.html.haml index e0411e679fc409610b8378bb030acbdd6d6e23c7..20a131c1172e46319fd30ebf1d7a7d6f66e3b110 100644 --- a/app/components/rapid_diffs/diff_file_header_component.html.haml +++ b/app/components/rapid_diffs/diff_file_header_component.html.haml @@ -11,6 +11,10 @@ -# * toggle file comments -# * submodule compare +- view_title = _('View file @ %{commitSha}') % { commitSha: Commit.truncate_sha(@diff_file.content_sha) } +- view_href = project_blob_path(@diff_file.repository.project, helpers.tree_join(@diff_file.content_sha, @diff_file.new_path)) +- options_menu_items = [ { "text": "#{view_title}", "href": "#{view_href}" } ].to_json + .rd-diff-file-header{ data: { testid: 'rd-diff-file-header' } } .rd-diff-file-toggle< = render Pajamas::ButtonComponent.new(category: :tertiary, size: :small, icon: 'chevron-down', button_options: { class: 'rd-diff-file-toggle-button', data: { opened: '', click: 'toggleFile' }, aria: { label: _('Hide file contents') } }) @@ -51,4 +55,7 @@ %span{ "data-testid" => "js-file-deletion-line" }= @diff_file.removed_lines .rd-diff-file-options-menu .js-options-menu + -# @@ -23,6 +27,8 @@ describe('Diff File Options Menu', () => { container: () => get('file').querySelector('.js-options-menu'), serverButton: () => get('container').querySelector('.js-options-button'), vueButton: () => get('container').querySelector('[data-testid="base-dropdown-toggle"]'), + menuItems: () => + get('container').querySelectorAll('[data-testid="disclosure-dropdown-item"]'), }; return elements[element]?.(); @@ -61,4 +67,16 @@ describe('Diff File Options Menu', () => { */ expect(get('serverButton')).toBeNull(); }); + + it('renders the correct menu items in the GlDisclosureDropdown as provided by the back end', () => { + const button = get('serverButton'); + + button.click(); + + const items = Array.from(get('menuItems')); + + expect(items.length).toBe(1); + expect(items[0].textContent.trim()).toBe(item1.text); + expect(items[0].querySelector('a').getAttribute('href')).toBe(item1.path); + }); });