From 6865bc16f8974e80b1ae657d7330be30c12c21ad Mon Sep 17 00:00:00 2001
From: James Lopez <james@jameslopez.es>
Date: Thu, 21 Apr 2016 09:12:03 +0200
Subject: [PATCH] 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
-- 
GitLab