From 6fdde5fabd9defed06a3308da7788c9a7f4632cf Mon Sep 17 00:00:00 2001 From: James Lopez Date: Tue, 19 Apr 2016 12:56:58 +0200 Subject: filter labels by including all filter titles as part of the query --- app/finders/issuable_finder.rb | 2 +- app/models/concerns/issuable.rb | 2 +- spec/features/issues/filter_by_labels_spec.rb | 170 ++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 spec/features/issues/filter_by_labels_spec.rb diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index f1df6832bf6..f8e2062d110 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -270,7 +270,7 @@ class IssuableFinder if filter_by_no_label? items = items.without_label else - items = items.with_label(label_names) + items = items.with_label(label_names.flatten) if projects items = items.where(labels: { project_id: projects }) diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index afa2ca039ae..e2a2899941f 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -37,7 +37,7 @@ module Issuable scope :closed, -> { with_state(:closed) } scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') } scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') } - scope :with_label, ->(title) { joins(:labels).where(labels: { title: title }) } + scope :with_label, ->(title) { joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}") } scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) } scope :join_project, -> { joins(:project) } diff --git a/spec/features/issues/filter_by_labels_spec.rb b/spec/features/issues/filter_by_labels_spec.rb new file mode 100644 index 00000000000..27d9ea7c62f --- /dev/null +++ b/spec/features/issues/filter_by_labels_spec.rb @@ -0,0 +1,170 @@ +# Uncomment once this is merged with multi-filter-labels +# Changes are related to using AND in label filters instead of OR + +# require 'rails_helper' +# +# feature 'Issue filtering by Labels', feature: true do +# let(:project) { create(:project, :public) } +# let!(:user) { create(:user)} +# let!(:label) { create(:label, project: project) } +# +# before do +# ['bug', 'feature', 'enhancement'].each do |title| +# create(:label, +# project: project, +# title: title) +# end +# +# issue1 = create(:issue, title: "Bugfix1", project: project) +# issue1.labels << project.labels.find_by(title: 'bug') +# +# issue2 = create(:issue, title: "Bugfix2", project: project) +# issue2.labels << project.labels.find_by(title: 'bug') +# issue2.labels << project.labels.find_by(title: 'enhancement') +# +# issue3 = create(:issue, title: "Feature1", project: project) +# issue3.labels << project.labels.find_by(title: 'feature') +# +# project.team << [user, :master] +# login_as(user) +# +# visit namespace_project_issues_path(project.namespace, project) +# end +# +# context 'filter by label bug', js: true do +# before do +# page.find('.js-label-select').click +# sleep 0.5 +# execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") +# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click +# sleep 2 +# end +# +# it 'should show issue "Bugfix1" and "Bugfix2" in issues list' do +# expect(page).to have_content "Bugfix1" +# expect(page).to have_content "Bugfix2" +# end +# +# it 'should not show "Feature1" in issues list' do +# expect(page).not_to have_content "Feature1" +# end +# +# it 'should show label "bug" in filtered-labels' do +# expect(find('.filtered-labels')).to have_content "bug" +# end +# +# it 'should not show label "feature" and "enhancement" in filtered-labels' do +# expect(find('.filtered-labels')).not_to have_content "feature" +# expect(find('.filtered-labels')).not_to have_content "enhancement" +# end +# end +# +# context 'filter by label feature', js: true do +# before do +# page.find('.js-label-select').click +# sleep 0.5 +# execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") +# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click +# sleep 2 +# end +# +# it 'should show issue "Feature1" in issues list' do +# expect(page).to have_content "Feature1" +# end +# +# it 'should not show "Bugfix1" and "Bugfix2" in issues list' do +# expect(page).not_to have_content "Bugfix2" +# expect(page).not_to have_content "Bugfix1" +# end +# +# it 'should show label "feature" in filtered-labels' do +# expect(find('.filtered-labels')).to have_content "feature" +# end +# +# it 'should not show label "bug" and "enhancement" in filtered-labels' do +# expect(find('.filtered-labels')).not_to have_content "bug" +# expect(find('.filtered-labels')).not_to have_content "enhancement" +# end +# end +# +# context 'filter by label enhancement', js: true do +# before do +# page.find('.js-label-select').click +# sleep 0.5 +# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") +# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click +# sleep 2 +# end +# +# it 'should show issue "Bugfix2" in issues list' do +# expect(page).to have_content "Bugfix2" +# end +# +# it 'should not show "Feature1" and "Bugfix1" in issues list' do +# expect(page).not_to have_content "Feature1" +# expect(page).not_to have_content "Bugfix1" +# end +# +# it 'should show label "enhancement" in filtered-labels' do +# expect(find('.filtered-labels')).to have_content "enhancement" +# end +# +# it 'should not show label "feature" and "bug" in filtered-labels' do +# expect(find('.filtered-labels')).not_to have_content "bug" +# expect(find('.filtered-labels')).not_to have_content "feature" +# end +# end +# +# context 'filter by label enhancement or feature', js: true do +# before do +# page.find('.js-label-select').click +# sleep 0.5 +# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") +# execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") +# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click +# sleep 2 +# end +# +# it 'should not show "Bugfix1" or "Feature1" in issues list' do +# expect(page).not_to have_content "Bugfix1" +# expect(page).not_to have_content "Feature1" +# end +# +# it 'should show label "enhancement" and "feature" in filtered-labels' do +# expect(find('.filtered-labels')).to have_content "enhancement" +# expect(find('.filtered-labels')).to have_content "feature" +# end +# +# it 'should not show label "bug" in filtered-labels' do +# expect(find('.filtered-labels')).not_to have_content "bug" +# end +# end +# +# context 'filter by label enhancement and bug in issues list', js: true do +# before do +# page.find('.js-label-select').click +# sleep 0.5 +# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") +# execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") +# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click +# sleep 2 +# end +# +# it 'should show issue "Bugfix2" in issues list' do +# expect(page).to have_content "Bugfix2" +# end +# +# it 'should not show "Feature1"' do +# expect(page).not_to have_content "Feature1" +# end +# +# it 'should show label "bug" and "enhancement" in filtered-labels' do +# expect(find('.filtered-labels')).to have_content "bug" +# expect(find('.filtered-labels')).to have_content "enhancement" +# end +# +# it 'should not show label "feature" in filtered-labels' do +# expect(find('.filtered-labels')).not_to have_content "feature" +# end +# end +# end -- cgit v1.2.1 From 8619208b644f2acdc30ccf36209ac56527bb4188 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Tue, 19 Apr 2016 15:36:28 +0200 Subject: fix other spec failures --- app/helpers/milestones_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/milestones_helper.rb b/app/helpers/milestones_helper.rb index 87fc2db6901..e2d01632df6 100644 --- a/app/helpers/milestones_helper.rb +++ b/app/helpers/milestones_helper.rb @@ -32,7 +32,7 @@ module MilestonesHelper end def milestone_issues_by_label_count(milestone, label, state:) - milestone.issues.with_label(label.title).send(state).size + milestone.issues.with_label([label.title]).send(state).size end def milestone_progress_bar(milestone) -- cgit v1.2.1 From af18cddddf4ecb402ac613ddcc1f313f6adb7aad Mon Sep 17 00:00:00 2001 From: James Lopez Date: Wed, 20 Apr 2016 10:56:28 +0200 Subject: udpated a few things based on MR feedback. Also added model spec --- app/finders/issuable_finder.rb | 5 +- app/helpers/milestones_helper.rb | 2 +- app/models/concerns/issuable.rb | 9 +- spec/features/issues/filter_by_labels_spec.rb | 337 +++++++++++++------------- spec/models/concerns/issuable_spec.rb | 26 +- 5 files changed, 203 insertions(+), 176 deletions(-) diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb index f8e2062d110..ca71acb65d3 100644 --- a/app/finders/issuable_finder.rb +++ b/app/finders/issuable_finder.rb @@ -270,8 +270,7 @@ class IssuableFinder if filter_by_no_label? items = items.without_label else - items = items.with_label(label_names.flatten) - + items = items.with_label(label_names) if projects items = items.where(labels: { project_id: projects }) end @@ -282,7 +281,7 @@ class IssuableFinder end def label_names - params[:label_name].split(',') + params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name] end def current_user_related? diff --git a/app/helpers/milestones_helper.rb b/app/helpers/milestones_helper.rb index e2d01632df6..87fc2db6901 100644 --- a/app/helpers/milestones_helper.rb +++ b/app/helpers/milestones_helper.rb @@ -32,7 +32,7 @@ module MilestonesHelper end def milestone_issues_by_label_count(milestone, label, state:) - milestone.issues.with_label([label.title]).send(state).size + milestone.issues.with_label(label.title).send(state).size end def milestone_progress_bar(milestone) diff --git a/app/models/concerns/issuable.rb b/app/models/concerns/issuable.rb index e2a2899941f..d5166e81474 100644 --- a/app/models/concerns/issuable.rb +++ b/app/models/concerns/issuable.rb @@ -37,7 +37,6 @@ module Issuable scope :closed, -> { with_state(:closed) } scope :order_milestone_due_desc, -> { joins(:milestone).reorder('milestones.due_date DESC, milestones.id DESC') } scope :order_milestone_due_asc, -> { joins(:milestone).reorder('milestones.due_date ASC, milestones.id ASC') } - scope :with_label, ->(title) { joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}") } scope :without_label, -> { joins("LEFT OUTER JOIN label_links ON label_links.target_type = '#{name}' AND label_links.target_id = #{table_name}.id").where(label_links: { id: nil }) } scope :join_project, -> { joins(:project) } @@ -122,6 +121,14 @@ module Issuable joins(join_clause).group(issuable_table[:id]).reorder("COUNT(notes.id) DESC") end + + def with_label(title) + if title.is_a?(Array) && title.count > 1 + joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}") + else + joins(:labels).where(labels: { title: title }) + end + end end def today? diff --git a/spec/features/issues/filter_by_labels_spec.rb b/spec/features/issues/filter_by_labels_spec.rb index 27d9ea7c62f..b189a97fbc0 100644 --- a/spec/features/issues/filter_by_labels_spec.rb +++ b/spec/features/issues/filter_by_labels_spec.rb @@ -1,170 +1,167 @@ -# Uncomment once this is merged with multi-filter-labels -# Changes are related to using AND in label filters instead of OR - -# require 'rails_helper' -# -# feature 'Issue filtering by Labels', feature: true do -# let(:project) { create(:project, :public) } -# let!(:user) { create(:user)} -# let!(:label) { create(:label, project: project) } -# -# before do -# ['bug', 'feature', 'enhancement'].each do |title| -# create(:label, -# project: project, -# title: title) -# end -# -# issue1 = create(:issue, title: "Bugfix1", project: project) -# issue1.labels << project.labels.find_by(title: 'bug') -# -# issue2 = create(:issue, title: "Bugfix2", project: project) -# issue2.labels << project.labels.find_by(title: 'bug') -# issue2.labels << project.labels.find_by(title: 'enhancement') -# -# issue3 = create(:issue, title: "Feature1", project: project) -# issue3.labels << project.labels.find_by(title: 'feature') -# -# project.team << [user, :master] -# login_as(user) -# -# visit namespace_project_issues_path(project.namespace, project) -# end -# -# context 'filter by label bug', js: true do -# before do -# page.find('.js-label-select').click -# sleep 0.5 -# execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") -# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click -# sleep 2 -# end -# -# it 'should show issue "Bugfix1" and "Bugfix2" in issues list' do -# expect(page).to have_content "Bugfix1" -# expect(page).to have_content "Bugfix2" -# end -# -# it 'should not show "Feature1" in issues list' do -# expect(page).not_to have_content "Feature1" -# end -# -# it 'should show label "bug" in filtered-labels' do -# expect(find('.filtered-labels')).to have_content "bug" -# end -# -# it 'should not show label "feature" and "enhancement" in filtered-labels' do -# expect(find('.filtered-labels')).not_to have_content "feature" -# expect(find('.filtered-labels')).not_to have_content "enhancement" -# end -# end -# -# context 'filter by label feature', js: true do -# before do -# page.find('.js-label-select').click -# sleep 0.5 -# execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") -# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click -# sleep 2 -# end -# -# it 'should show issue "Feature1" in issues list' do -# expect(page).to have_content "Feature1" -# end -# -# it 'should not show "Bugfix1" and "Bugfix2" in issues list' do -# expect(page).not_to have_content "Bugfix2" -# expect(page).not_to have_content "Bugfix1" -# end -# -# it 'should show label "feature" in filtered-labels' do -# expect(find('.filtered-labels')).to have_content "feature" -# end -# -# it 'should not show label "bug" and "enhancement" in filtered-labels' do -# expect(find('.filtered-labels')).not_to have_content "bug" -# expect(find('.filtered-labels')).not_to have_content "enhancement" -# end -# end -# -# context 'filter by label enhancement', js: true do -# before do -# page.find('.js-label-select').click -# sleep 0.5 -# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") -# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click -# sleep 2 -# end -# -# it 'should show issue "Bugfix2" in issues list' do -# expect(page).to have_content "Bugfix2" -# end -# -# it 'should not show "Feature1" and "Bugfix1" in issues list' do -# expect(page).not_to have_content "Feature1" -# expect(page).not_to have_content "Bugfix1" -# end -# -# it 'should show label "enhancement" in filtered-labels' do -# expect(find('.filtered-labels')).to have_content "enhancement" -# end -# -# it 'should not show label "feature" and "bug" in filtered-labels' do -# expect(find('.filtered-labels')).not_to have_content "bug" -# expect(find('.filtered-labels')).not_to have_content "feature" -# end -# end -# -# context 'filter by label enhancement or feature', js: true do -# before do -# page.find('.js-label-select').click -# sleep 0.5 -# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") -# execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") -# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click -# sleep 2 -# end -# -# it 'should not show "Bugfix1" or "Feature1" in issues list' do -# expect(page).not_to have_content "Bugfix1" -# expect(page).not_to have_content "Feature1" -# end -# -# it 'should show label "enhancement" and "feature" in filtered-labels' do -# expect(find('.filtered-labels')).to have_content "enhancement" -# expect(find('.filtered-labels')).to have_content "feature" -# end -# -# it 'should not show label "bug" in filtered-labels' do -# expect(find('.filtered-labels')).not_to have_content "bug" -# end -# end -# -# context 'filter by label enhancement and bug in issues list', js: true do -# before do -# page.find('.js-label-select').click -# sleep 0.5 -# execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") -# execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") -# page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click -# sleep 2 -# end -# -# it 'should show issue "Bugfix2" in issues list' do -# expect(page).to have_content "Bugfix2" -# end -# -# it 'should not show "Feature1"' do -# expect(page).not_to have_content "Feature1" -# end -# -# it 'should show label "bug" and "enhancement" in filtered-labels' do -# expect(find('.filtered-labels')).to have_content "bug" -# expect(find('.filtered-labels')).to have_content "enhancement" -# end -# -# it 'should not show label "feature" in filtered-labels' do -# expect(find('.filtered-labels')).not_to have_content "feature" -# end -# end -# end +require 'rails_helper' + +feature 'Issue filtering by Labels', feature: true do + let(:project) { create(:project, :public) } + let!(:user) { create(:user)} + let!(:label) { create(:label, project: project) } + + before do + ['bug', 'feature', 'enhancement'].each do |title| + create(:label, + project: project, + title: title) + end + + issue1 = create(:issue, title: "Bugfix1", project: project) + issue1.labels << project.labels.find_by(title: 'bug') + + issue2 = create(:issue, title: "Bugfix2", project: project) + issue2.labels << project.labels.find_by(title: 'bug') + issue2.labels << project.labels.find_by(title: 'enhancement') + + issue3 = create(:issue, title: "Feature1", project: project) + issue3.labels << project.labels.find_by(title: 'feature') + + project.team << [user, :master] + login_as(user) + + visit namespace_project_issues_path(project.namespace, project) + end + + context 'filter by label bug', js: true do + before do + page.find('.js-label-select').click + sleep 0.5 + execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") + page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click + sleep 2 + end + + it 'should show issue "Bugfix1" and "Bugfix2" in issues list' do + expect(page).to have_content "Bugfix1" + expect(page).to have_content "Bugfix2" + end + + it 'should not show "Feature1" in issues list' do + expect(page).not_to have_content "Feature1" + end + + it 'should show label "bug" in filtered-labels' do + expect(find('.filtered-labels')).to have_content "bug" + end + + it 'should not show label "feature" and "enhancement" in filtered-labels' do + expect(find('.filtered-labels')).not_to have_content "feature" + expect(find('.filtered-labels')).not_to have_content "enhancement" + end + end + + context 'filter by label feature', js: true do + before do + page.find('.js-label-select').click + sleep 0.5 + execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") + page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click + sleep 2 + end + + it 'should show issue "Feature1" in issues list' do + expect(page).to have_content "Feature1" + end + + it 'should not show "Bugfix1" and "Bugfix2" in issues list' do + expect(page).not_to have_content "Bugfix2" + expect(page).not_to have_content "Bugfix1" + end + + it 'should show label "feature" in filtered-labels' do + expect(find('.filtered-labels')).to have_content "feature" + end + + it 'should not show label "bug" and "enhancement" in filtered-labels' do + expect(find('.filtered-labels')).not_to have_content "bug" + expect(find('.filtered-labels')).not_to have_content "enhancement" + end + end + + context 'filter by label enhancement', js: true do + before do + page.find('.js-label-select').click + sleep 0.5 + execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") + page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click + sleep 2 + end + + it 'should show issue "Bugfix2" in issues list' do + expect(page).to have_content "Bugfix2" + end + + it 'should not show "Feature1" and "Bugfix1" in issues list' do + expect(page).not_to have_content "Feature1" + expect(page).not_to have_content "Bugfix1" + end + + it 'should show label "enhancement" in filtered-labels' do + expect(find('.filtered-labels')).to have_content "enhancement" + end + + it 'should not show label "feature" and "bug" in filtered-labels' do + expect(find('.filtered-labels')).not_to have_content "bug" + expect(find('.filtered-labels')).not_to have_content "feature" + end + end + + context 'filter by label enhancement or feature', js: true do + before do + page.find('.js-label-select').click + sleep 0.5 + execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") + execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") + page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click + sleep 2 + end + + it 'should not show "Bugfix1" or "Feature1" in issues list' do + expect(page).not_to have_content "Bugfix1" + expect(page).not_to have_content "Feature1" + end + + it 'should show label "enhancement" and "feature" in filtered-labels' do + expect(find('.filtered-labels')).to have_content "enhancement" + expect(find('.filtered-labels')).to have_content "feature" + end + + it 'should not show label "bug" in filtered-labels' do + expect(find('.filtered-labels')).not_to have_content "bug" + end + end + + context 'filter by label enhancement and bug in issues list', js: true do + before do + page.find('.js-label-select').click + sleep 0.5 + execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") + execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") + page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click + sleep 2 + end + + it 'should show issue "Bugfix2" in issues list' do + expect(page).to have_content "Bugfix2" + end + + it 'should not show "Feature1"' do + expect(page).not_to have_content "Feature1" + end + + it 'should show label "bug" and "enhancement" in filtered-labels' do + expect(find('.filtered-labels')).to have_content "bug" + expect(find('.filtered-labels')).to have_content "enhancement" + end + + it 'should not show label "feature" in filtered-labels' do + expect(find('.filtered-labels')).not_to have_content "feature" + end + end +end diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index b16ccc6e305..dc7a9a10893 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -212,4 +212,28 @@ describe Issue, "Issuable" do expect(issue.downvotes).to eq(1) end end -end + + describe ".with_label" do + let(:example_label) { 'test1' } + let(:example_labels) { ['test1', 'test2'] } + + it 'finds issue with 1 label' do + setup_labels([example_label]) + + expect(Issue.with_label(example_label).count).to eq(1) + end + + it 'finds issue with 2 labels' do + setup_labels(example_labels) + + expect(Issue.with_label(example_labels).to_a.count).to eq(1) + end + + def setup_labels(label_names) + labels = label_names.map do |label| + create(:label, project: issue.project, title: label) + end + issue.labels << labels + end + end +end \ No newline at end of file -- cgit v1.2.1 From c0948396d1fd8bf4cbd187f33583b2f6db5d2178 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Wed, 20 Apr 2016 11:25:33 +0200 Subject: fix rubocop warning --- spec/models/concerns/issuable_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index dc7a9a10893..6b61bbb2fab 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -220,13 +220,13 @@ describe Issue, "Issuable" do it 'finds issue with 1 label' do setup_labels([example_label]) - expect(Issue.with_label(example_label).count).to eq(1) + expect(Issue.with_label(example_label).size).to eq(1) end it 'finds issue with 2 labels' do setup_labels(example_labels) - expect(Issue.with_label(example_labels).to_a.count).to eq(1) + expect(Issue.with_label(example_labels).to_a.size).to eq(1) end def setup_labels(label_names) -- cgit v1.2.1 From 976593fa7fd657437c68ade72f2061260318e7a9 Mon Sep 17 00:00:00 2001 From: James Lopez Date: Wed, 20 Apr 2016 11:50:07 +0200 Subject: final line missing --- spec/models/concerns/issuable_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 6b61bbb2fab..63a0a47e923 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -236,4 +236,4 @@ describe Issue, "Issuable" do issue.labels << labels end end -end \ No newline at end of file +end -- cgit v1.2.1 From 6865bc16f8974e80b1ae657d7330be30c12c21ad Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 21 Apr 2016 09:12:03 +0200 Subject: refactored specs, adding more scenarios --- spec/features/issues/filter_by_labels_spec.rb | 33 ++++++++++++--------------- spec/models/concerns/issuable_spec.rb | 23 +++++++++++++++---- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/spec/features/issues/filter_by_labels_spec.rb b/spec/features/issues/filter_by_labels_spec.rb index 7944403f874..9696af4b006 100644 --- a/spec/features/issues/filter_by_labels_spec.rb +++ b/spec/features/issues/filter_by_labels_spec.rb @@ -4,23 +4,25 @@ feature 'Issue filtering by Labels', feature: true do let(:project) { create(:project, :public) } let!(:user) { create(:user)} let!(:label) { create(:label, project: project) } + let(:bug) { create(:label, project: project, title: 'bug') } + let(:feature) { create(:label, project: project, title: 'feature') } + let(:enhancement) { create(:label, project: project, title: 'enhancement') } before do - ['bug', 'feature', 'enhancement'].each do |title| - create(:label, - project: project, - title: title) - end + + bug = create(:label, project: project, title: 'bug') + feature = create(:label, project: project, title: 'feature') + enhancement = create(:label, project: project, title: 'enhancement') issue1 = create(:issue, title: "Bugfix1", project: project) - issue1.labels << project.labels.find_by(title: 'bug') + issue1.labels << bug issue2 = create(:issue, title: "Bugfix2", project: project) - issue2.labels << project.labels.find_by(title: 'bug') - issue2.labels << project.labels.find_by(title: 'enhancement') + issue2.labels << bug + issue2.labels << enhancement issue3 = create(:issue, title: "Feature1", project: project) - issue3.labels << project.labels.find_by(title: 'feature') + issue3.labels << feature project.team << [user, :master] login_as(user) @@ -122,13 +124,9 @@ feature 'Issue filtering by Labels', feature: true do sleep 2 end - it 'should show issue "Bugfix2" or "Feature1" in issues list' do - expect(page).to have_content "Bugfix2" - expect(page).to have_content "Feature1" - end - - it 'should not show "Bugfix1" in issues list' do + it 'should not show "Bugfix1" or "Feature1" in issues list' do expect(page).not_to have_content "Bugfix1" + expect(page).not_to have_content "Feature1" end it 'should show label "enhancement" and "feature" in filtered-labels' do @@ -141,7 +139,7 @@ feature 'Issue filtering by Labels', feature: true do end end - context 'filter by label enhancement or bug in issues list', js: true do + context 'filter by label enhancement and bug in issues list', js: true do before do page.find('.js-label-select').click sleep 0.5 @@ -151,9 +149,8 @@ feature 'Issue filtering by Labels', feature: true do sleep 2 end - it 'should show issue "Bugfix2" or "Bugfix1" in issues list' do + it 'should show issue "Bugfix2" in issues list' do expect(page).to have_content "Bugfix2" - expect(page).to have_content "Bugfix1" end it 'should not show "Feature1"' do diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index 63a0a47e923..cf9c1cc1fba 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -217,16 +217,26 @@ describe Issue, "Issuable" do let(:example_label) { 'test1' } let(:example_labels) { ['test1', 'test2'] } - it 'finds issue with 1 label' do + before(:each) do + setup_other_issue + end + + it 'finds the correct issue with 1 label' do setup_labels([example_label]) - expect(Issue.with_label(example_label).size).to eq(1) + expect(Issue.with_label(example_label)).to eq([issue]) + end + + it 'finds the correct issue with 2 labels' do + setup_labels(example_labels) + + expect(Issue.with_label(example_labels)).to eq([issue]) end - it 'finds issue with 2 labels' do + it 'finds the correct issue with 1 of 2 labels' do setup_labels(example_labels) - expect(Issue.with_label(example_labels).to_a.size).to eq(1) + expect(Issue.with_label(example_label)).to eq([issue]) end def setup_labels(label_names) @@ -235,5 +245,10 @@ describe Issue, "Issuable" do end issue.labels << labels end + + def setup_other_issue + issue2 = create(:issue) + issue2.labels << create(:label, project: issue2.project, title: 'other_label') + end end end -- cgit v1.2.1 From 2d2b73ae4954a96d060fdf21ce8e95ccbdebe68f Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 21 Apr 2016 10:30:48 +0200 Subject: use wait_for_ajax instead of sleeping for 2 days! --- spec/features/issues/filter_by_labels_spec.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/spec/features/issues/filter_by_labels_spec.rb b/spec/features/issues/filter_by_labels_spec.rb index 9696af4b006..29c3f9eaad4 100644 --- a/spec/features/issues/filter_by_labels_spec.rb +++ b/spec/features/issues/filter_by_labels_spec.rb @@ -1,6 +1,8 @@ require 'rails_helper' feature 'Issue filtering by Labels', feature: true do + include WaitForAjax + let(:project) { create(:project, :public) } let!(:user) { create(:user)} let!(:label) { create(:label, project: project) } @@ -33,10 +35,10 @@ feature 'Issue filtering by Labels', feature: true do context 'filter by label bug', js: true do before do page.find('.js-label-select').click - sleep 0.5 + wait_for_ajax execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click - sleep 2 + wait_for_ajax end it 'should show issue "Bugfix1" and "Bugfix2" in issues list' do @@ -61,10 +63,10 @@ feature 'Issue filtering by Labels', feature: true do context 'filter by label feature', js: true do before do page.find('.js-label-select').click - sleep 0.5 + wait_for_ajax execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click - sleep 2 + wait_for_ajax end it 'should show issue "Feature1" in issues list' do @@ -89,10 +91,10 @@ feature 'Issue filtering by Labels', feature: true do context 'filter by label enhancement', js: true do before do page.find('.js-label-select').click - sleep 0.5 + wait_for_ajax execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click - sleep 2 + wait_for_ajax end it 'should show issue "Bugfix2" in issues list' do @@ -117,11 +119,11 @@ feature 'Issue filtering by Labels', feature: true do context 'filter by label enhancement or feature', js: true do before do page.find('.js-label-select').click - sleep 0.5 + wait_for_ajax execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") execute_script("$('.dropdown-menu-labels li:contains(\"feature\") a').click()") page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click - sleep 2 + wait_for_ajax end it 'should not show "Bugfix1" or "Feature1" in issues list' do @@ -142,11 +144,11 @@ feature 'Issue filtering by Labels', feature: true do context 'filter by label enhancement and bug in issues list', js: true do before do page.find('.js-label-select').click - sleep 0.5 + wait_for_ajax execute_script("$('.dropdown-menu-labels li:contains(\"enhancement\") a').click()") execute_script("$('.dropdown-menu-labels li:contains(\"bug\") a').click()") page.first('.labels-filter .dropdown-title .dropdown-menu-close-icon').click - sleep 2 + wait_for_ajax end it 'should show issue "Bugfix2" in issues list' do -- cgit v1.2.1 From b09b175def7c66487d4571d90f23f613d868f25c Mon Sep 17 00:00:00 2001 From: James Lopez Date: Thu, 21 Apr 2016 13:20:00 +0200 Subject: refactored specs based on feedback --- spec/features/issues/filter_by_labels_spec.rb | 4 --- spec/models/concerns/issuable_spec.rb | 45 +++++++++++---------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/spec/features/issues/filter_by_labels_spec.rb b/spec/features/issues/filter_by_labels_spec.rb index 29c3f9eaad4..7f654684143 100644 --- a/spec/features/issues/filter_by_labels_spec.rb +++ b/spec/features/issues/filter_by_labels_spec.rb @@ -6,12 +6,8 @@ feature 'Issue filtering by Labels', feature: true do let(:project) { create(:project, :public) } let!(:user) { create(:user)} let!(:label) { create(:label, project: project) } - let(:bug) { create(:label, project: project, title: 'bug') } - let(:feature) { create(:label, project: project, title: 'feature') } - let(:enhancement) { create(:label, project: project, title: 'enhancement') } before do - bug = create(:label, project: project, title: 'bug') feature = create(:label, project: project, title: 'feature') enhancement = create(:label, project: project, title: 'enhancement') diff --git a/spec/models/concerns/issuable_spec.rb b/spec/models/concerns/issuable_spec.rb index cf9c1cc1fba..4a4cd093435 100644 --- a/spec/models/concerns/issuable_spec.rb +++ b/spec/models/concerns/issuable_spec.rb @@ -214,41 +214,32 @@ describe Issue, "Issuable" do end describe ".with_label" do - let(:example_label) { 'test1' } - let(:example_labels) { ['test1', 'test2'] } + let(:project) { create(:project, :public) } + let(:bug) { create(:label, project: project, title: 'bug') } + let(:feature) { create(:label, project: project, title: 'feature') } + let(:enhancement) { create(:label, project: project, title: 'enhancement') } + let(:issue1) { create(:issue, title: "Bugfix1", project: project) } + let(:issue2) { create(:issue, title: "Bugfix2", project: project) } + let(:issue3) { create(:issue, title: "Feature1", project: project) } before(:each) do - setup_other_issue + issue1.labels << bug + issue1.labels << feature + issue2.labels << bug + issue2.labels << enhancement + issue3.labels << feature end - it 'finds the correct issue with 1 label' do - setup_labels([example_label]) - - expect(Issue.with_label(example_label)).to eq([issue]) - end - - it 'finds the correct issue with 2 labels' do - setup_labels(example_labels) - - expect(Issue.with_label(example_labels)).to eq([issue]) + it 'finds the correct issue containing just enhancement label' do + expect(Issue.with_label(enhancement.title)).to match_array([issue2]) end - it 'finds the correct issue with 1 of 2 labels' do - setup_labels(example_labels) - - expect(Issue.with_label(example_label)).to eq([issue]) - end - - def setup_labels(label_names) - labels = label_names.map do |label| - create(:label, project: issue.project, title: label) - end - issue.labels << labels + it 'finds the correct issues containing the same label' do + expect(Issue.with_label(bug.title)).to match_array([issue1, issue2]) end - def setup_other_issue - issue2 = create(:issue) - issue2.labels << create(:label, project: issue2.project, title: 'other_label') + it 'finds the correct issues containing only both labels' do + expect(Issue.with_label([bug.title, enhancement.title])).to match_array([issue2]) end end end -- cgit v1.2.1