From bcf304b04ac4bd7e7863421f84a388ab4039b80f Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Thu, 18 Aug 2016 13:59:09 +0100 Subject: [PATCH] Changed tests to use JS tests --- app/assets/javascripts/labels_select.js | 4 +- app/helpers/issuables_helper.rb | 3 +- app/views/shared/issuable/_sidebar.html.haml | 2 +- spec/features/issues/issue_sidebar_spec.rb | 40 --------- spec/helpers/issuable_helper_spec.rb | 16 ++++ .../fixtures/issue_sidebar_label.html.haml | 16 ++++ .../labels_issue_sidebar_spec.js.es6 | 89 +++++++++++++++++++ 7 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 spec/helpers/issuable_helper_spec.rb create mode 100644 spec/javascripts/fixtures/issue_sidebar_label.html.haml create mode 100644 spec/javascripts/labels_issue_sidebar_spec.js.es6 diff --git a/app/assets/javascripts/labels_select.js b/app/assets/javascripts/labels_select.js index 8dbbd9dd517..565dbeacdb3 100644 --- a/app/assets/javascripts/labels_select.js +++ b/app/assets/javascripts/labels_select.js @@ -34,7 +34,9 @@ $sidebarLabelTooltip.tooltip(); - new gl.CreateLabelDropdown($dropdown.closest('.dropdown').find('.dropdown-new-label'), projectId); + if ($dropdown.closest('.dropdown').find('.dropdown-new-label').length) { + new gl.CreateLabelDropdown($dropdown.closest('.dropdown').find('.dropdown-new-label'), projectId); + } saveLabelData = function() { var data, selected; diff --git a/app/helpers/issuables_helper.rb b/app/helpers/issuables_helper.rb index c3f4c1b148d..b9baeb1d6c4 100644 --- a/app/helpers/issuables_helper.rb +++ b/app/helpers/issuables_helper.rb @@ -73,8 +73,7 @@ module IssuablesHelper end def issuable_labels_tooltip(labels, limit: 5) - first = labels[0...limit] - last = labels[(limit-1)...-1] + first, last = labels.partition.with_index{ |_, i| i < limit } label_names = first.collect(&:name) label_names << "and #{last.size} more" unless last.empty? diff --git a/app/views/shared/issuable/_sidebar.html.haml b/app/views/shared/issuable/_sidebar.html.haml index bb18ef2536e..c1b50e65af5 100644 --- a/app/views/shared/issuable/_sidebar.html.haml +++ b/app/views/shared/issuable/_sidebar.html.haml @@ -109,7 +109,7 @@ - if issuable.project.labels.any? .block.labels - .sidebar-collapsed-icon.js-sidebar-labels-tooltip{ title: issuable_labels_tooltip(issuable.labels), data: { placement: "left", container: "body" } } + .sidebar-collapsed-icon.js-sidebar-labels-tooltip{ title: issuable_labels_tooltip(issuable.labels_array), data: { placement: "left", container: "body" } } = icon('tags') %span = issuable.labels_array.size diff --git a/spec/features/issues/issue_sidebar_spec.rb b/spec/features/issues/issue_sidebar_spec.rb index b043bc3feee..4b1aec8bf71 100644 --- a/spec/features/issues/issue_sidebar_spec.rb +++ b/spec/features/issues/issue_sidebar_spec.rb @@ -1,8 +1,6 @@ require 'rails_helper' feature 'Issue Sidebar', feature: true do - include WaitForAjax - let(:project) { create(:project) } let(:issue) { create(:issue, project: project) } let!(:user) { create(:user)} @@ -75,44 +73,6 @@ feature 'Issue Sidebar', feature: true do end end - context 'update labels', js: true do - before do - project.team << [user, :developer] - visit_issue(project, issue) - end - - context 'more than 5' do - before do - create(:label, project: project, title: 'a') - create(:label, project: project, title: 'b') - create(:label, project: project, title: 'c') - create(:label, project: project, title: 'd') - create(:label, project: project, title: 'e') - create(:label, project: project, title: 'f') - end - - it 'should update the tooltip for collapsed sidebar' do - page.within('.block.labels') do - find('.edit-link').click - - page.within('.dropdown-menu-labels') do - click_link 'a' - click_link 'b' - click_link 'c' - click_link 'd' - click_link 'e' - click_link 'f' - end - - find('.edit-link').click - wait_for_ajax - - expect(find('.js-sidebar-labels-tooltip', visible: false)['data-original-title']).to eq('a, b, c, d, e, and 1 more') - end - end - end - end - def visit_issue(project, issue) visit namespace_project_issue_path(project.namespace, project, issue) end diff --git a/spec/helpers/issuable_helper_spec.rb b/spec/helpers/issuable_helper_spec.rb new file mode 100644 index 00000000000..2dd2eab0524 --- /dev/null +++ b/spec/helpers/issuable_helper_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe IssuablesHelper do + let(:label) { build_stubbed(:label) } + let(:label2) { build_stubbed(:label) } + + context 'label tooltip' do + it 'returns label text' do + expect(issuable_labels_tooltip([label])).to eq(label.title) + end + + it 'returns label text' do + expect(issuable_labels_tooltip([label, label2], limit: 1)).to eq("#{label.title}, and 1 more") + end + end +end diff --git a/spec/javascripts/fixtures/issue_sidebar_label.html.haml b/spec/javascripts/fixtures/issue_sidebar_label.html.haml new file mode 100644 index 00000000000..74421730b98 --- /dev/null +++ b/spec/javascripts/fixtures/issue_sidebar_label.html.haml @@ -0,0 +1,16 @@ +.block.labels + .sidebar-collapsed-icon.js-sidebar-labels-tooltip + .title.hide-collapsed + %a.edit-link.pull-right{ href: "#" } + Edit + .selectbox.hide-collapsed{ style: "display: none;" } + .dropdown + %button.dropdown-menu-toggle.js-label-select.js-multiselect{"data-ability-name" => "issue", "data-field-name" => "issue[label_names][]", "data-issue-update" => "/root/test/issues/2.json", "data-labels" => "/root/test/labels.json", "data-project-id" => "12", "data-show-any" => "true", "data-show-no" => "true", "data-toggle" => "dropdown", :type => "button"} + %span.dropdown-toggle-text + Label + %i.fa.fa-chevron-down + .dropdown-menu.dropdown-select.dropdown-menu-paging.dropdown-menu-labels.dropdown-menu-selectable + .dropdown-page-one + .dropdown-content + .dropdown-loading + %i.fa.fa-spinner.fa-spin diff --git a/spec/javascripts/labels_issue_sidebar_spec.js.es6 b/spec/javascripts/labels_issue_sidebar_spec.js.es6 new file mode 100644 index 00000000000..840c7b6d015 --- /dev/null +++ b/spec/javascripts/labels_issue_sidebar_spec.js.es6 @@ -0,0 +1,89 @@ +//= require lib/utils/type_utility +//= require jquery +//= require bootstrap +//= require gl_dropdown +//= require select2 +//= require jquery.nicescroll +//= require api +//= require create_label +//= require issuable_context +//= require users_select +//= require labels_select + +(() => { + let saveLabelCount = 0; + describe('Issue dropdown sidebar', () => { + fixture.preload('issue_sidebar_label.html'); + + beforeEach(() => { + fixture.load('issue_sidebar_label.html'); + new IssuableContext('{"id":1,"name":"Administrator","username":"root"}'); + new LabelsSelect(); + + spyOn(jQuery, 'ajax').and.callFake((req) => { + const d = $.Deferred(); + let LABELS_DATA = [] + + if (req.url === '/root/test/labels.json') { + for (let i = 0; i < 10; i++) { + LABELS_DATA.push({id: i, title: `test ${i}`, color: '#5CB85C'}); + } + } else if (req.url === '/root/test/issues/2.json') { + let tmp = [] + for (let i = 0; i < saveLabelCount; i++) { + tmp.push({id: i, title: `test ${i}`, color: '#5CB85C'}); + } + LABELS_DATA = {labels: tmp}; + } + + d.resolve(LABELS_DATA); + return d.promise(); + }); + }); + + it('changes collapsed tooltip when changing labels when less than 5', (done) => { + saveLabelCount = 5; + $('.edit-link').get(0).click(); + + setTimeout(() => { + expect($('.dropdown-content a').length).toBe(10); + + $('.dropdow-content a').each((i, $link) => { + if (i < 5) { + $link.get(0).click(); + } + }); + + $('.edit-link').get(0).click(); + + setTimeout(() => { + expect($('.sidebar-collapsed-icon').attr('data-original-title')).toBe('test 0, test 1, test 2, test 3, test 4'); + done(); + }, 0); + }, 0); + }); + + it('changes collapsed tooltip when changing labels when more than 5', (done) => { + saveLabelCount = 6; + $('.edit-link').get(0).click(); + + setTimeout(() => { + expect($('.dropdown-content a').length).toBe(10); + + $('.dropdow-content a').each((i, $link) => { + if (i < 5) { + $link.get(0).click(); + } + }); + + $('.edit-link').get(0).click(); + + setTimeout(() => { + expect($('.sidebar-collapsed-icon').attr('data-original-title')).toBe('test 0, test 1, test 2, test 3, test 4, and 1 more'); + done(); + }, 0); + }, 0); + }); + }); +})(); +