diff --git a/app/assets/javascripts/merge_request_tabs.js b/app/assets/javascripts/merge_request_tabs.js index f685af7d97e..1a214cdd00e 100644 --- a/app/assets/javascripts/merge_request_tabs.js +++ b/app/assets/javascripts/merge_request_tabs.js @@ -369,18 +369,21 @@ }; MergeRequestTabs.prototype.initAffix = function () { + var $tabs = $('.js-tabs-affix'); + // Screen space on small screens is usually very sparse // So we dont affix the tabs on these - if (Breakpoints.get().getBreakpointSize() === 'xs') return; + if (Breakpoints.get().getBreakpointSize() === 'xs' || !$tabs.length) return; - var $tabs = $('.js-tabs-affix'), - tabsWidth = $tabs.outerWidth(), - $diffTabs = $('#diff-notes-app'), - offsetTop = $tabs.offset().top - ($('.navbar-fixed-top').height() + $('.layout-nav').height()); + var tabsWidth = $tabs.outerWidth(), + $diffTabs = $('#diff-notes-app'), + offsetTop = $tabs.offset().top - ($('.navbar-fixed-top').height() + $('.layout-nav').height()); $tabs.off('affix.bs.affix affix-top.bs.affix') .affix({ - offset: offsetTop + offset: { + top: offsetTop + } }).on('affix.bs.affix', function () { $tabs.css({ left: $tabs.offset().left, diff --git a/spec/features/merge_requests/sticky_tabs_spec.rb b/spec/features/merge_requests/sticky_tabs_spec.rb index 6f8c3dc55f4..e78f3f134d5 100644 --- a/spec/features/merge_requests/sticky_tabs_spec.rb +++ b/spec/features/merge_requests/sticky_tabs_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' feature 'Merge request tabs', js: true, feature: true do + include WaitForAjax + let(:user) { create(:user) } let(:project) { create(:project, :public) } let(:merge_request) { create(:merge_request_with_diffs, source_project: project, author: user, title: "Bug NS-04") } @@ -9,18 +11,20 @@ feature 'Merge request tabs', js: true, feature: true do project.team << [user, :master] login_as user visit diffs_namespace_project_merge_request_path(project.namespace, project, merge_request) + + wait_for_ajax end it 'affixes to top of page when scrolling' do - page.execute_script "window.scrollBy(0,10000)" + page.execute_script "window.scroll(0,10000)" expect(page).to have_selector('.js-tabs-affix.affix') end it 'removes affix when scrolling to top' do - page.execute_script "window.scrollBy(0,10000)" + page.execute_script "window.scroll(0,10000)" expect(page).to have_selector('.js-tabs-affix.affix') - page.execute_script "window.scrollBy(0,-10000)" + page.execute_script "window.scroll(0,-10000)" expect(page).to have_selector('.js-tabs-affix.affix-top') end end