From 2e3f0b08fbc0387d3f7985bf61c16e8ec9821750 Mon Sep 17 00:00:00 2001
From: Felipe Artur <felipefac@gmail.com>
Date: Fri, 9 Jun 2017 12:54:20 -0300
Subject: [PATCH] Remove old specs and add new ones

---
 app/controllers/concerns/milestone_actions.rb |  2 +-
 app/models/concerns/milestoneish.rb           |  8 +++--
 .../features/dashboard/milestone_tabs_spec.rb |  2 +-
 spec/features/milestones/milestones_spec.rb   |  6 ----
 spec/models/concerns/milestoneish_spec.rb     | 35 +++++++++++++++++++
 spec/models/group_milestone_spec.rb           |  1 +
 spec/models/milestone_spec.rb                 | 29 ---------------
 7 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/app/controllers/concerns/milestone_actions.rb b/app/controllers/concerns/milestone_actions.rb
index 4bda26fdfd..1ff785ac2c 100644
--- a/app/controllers/concerns/milestone_actions.rb
+++ b/app/controllers/concerns/milestone_actions.rb
@@ -6,7 +6,7 @@ module MilestoneActions
       format.html { redirect_to milestone_redirect_path }
       format.json do
         render json: tabs_json("shared/milestones/_merge_requests_tab", {
-          merge_requests: @milestone.merge_requests.sort('priority'),
+          merge_requests: @milestone.sorted_merge_requests,
           show_project_name: true
         })
       end
diff --git a/app/models/concerns/milestoneish.rb b/app/models/concerns/milestoneish.rb
index a324721031..0f4d4d841b 100644
--- a/app/models/concerns/milestoneish.rb
+++ b/app/models/concerns/milestoneish.rb
@@ -62,12 +62,16 @@ module Milestoneish
     due_date && due_date.past?
   end
 
+  def sorted_merge_requests
+    merge_requests.sort('label_priority')
+  end
+
   private
 
   def count_issues_by_state(user)
     memoize_per_user(user, :count_issues_by_state) do
       # Need to group and count using ruby array to not break
-      # label ordering
+      # label ordering. Also it saves a SQL query.
       issues_visible_to_user(user).to_a.group_by(&:state)
     end
   end
@@ -81,6 +85,6 @@ module Milestoneish
   # override in a class that includes this module to get a faster query
   # from IssuesFinder
   def issues_finder_params
-    { sort: 'priority' }
+    { sort: 'label_priority' }
   end
 end
diff --git a/spec/features/dashboard/milestone_tabs_spec.rb b/spec/features/dashboard/milestone_tabs_spec.rb
index 0c7b992c50..2c48939bf9 100644
--- a/spec/features/dashboard/milestone_tabs_spec.rb
+++ b/spec/features/dashboard/milestone_tabs_spec.rb
@@ -23,7 +23,7 @@ describe 'Dashboard milestone tabs', :js, :feature do
   it 'loads merge requests async' do
     click_link 'Merge Requests'
 
-    expect(page).to have_selector('.merge_requests-sortable-list')
+    expect(page).to have_selector('.milestone-merge_requests-list')
   end
 
   it 'loads participants async' do
diff --git a/spec/features/milestones/milestones_spec.rb b/spec/features/milestones/milestones_spec.rb
index 1537b0f472..9e117c8ed9 100644
--- a/spec/features/milestones/milestones_spec.rb
+++ b/spec/features/milestones/milestones_spec.rb
@@ -39,7 +39,6 @@
 #       expect(issue_target).to have_selector('.issuable-row')
 #     end
 #   end
->>>>>>> Remove Drag and drop and sorting from milestone view
 
 #   context 'merge requests' do
 #     let(:merge_request)        { page.find_by_id('merge_requests-list-unassigned').find('li') }
@@ -73,13 +72,8 @@
 #     end
 #   end
 
-<<<<<<< 5f42009f8dcc29d559ee415e92c88858e361f063
-  def create_and_drag_issue(params = {})
-    @issue = create(:issue, params.merge(title: 'Foo', project: project, milestone: milestone))
-=======
 #   def create_and_drag_issue(params = {})
 #     create(:issue, params.merge(title: 'Foo', project: project, milestone: milestone))
->>>>>>> Remove Drag and drop and sorting from milestone view
 
 #     visit namespace_project_milestone_path(project.namespace, project, milestone)
 #     scroll_into_view('.milestone-content')
diff --git a/spec/models/concerns/milestoneish_spec.rb b/spec/models/concerns/milestoneish_spec.rb
index 675b730c55..7616123b48 100644
--- a/spec/models/concerns/milestoneish_spec.rb
+++ b/spec/models/concerns/milestoneish_spec.rb
@@ -19,12 +19,47 @@ describe Milestone, 'Milestoneish' do
   let!(:closed_security_issue_3) { create(:issue, :confidential, :closed, project: project, author: author, milestone: milestone) }
   let!(:closed_security_issue_4) { create(:issue, :confidential, :closed, project: project, assignees: [assignee], milestone: milestone) }
   let!(:merge_request) { create(:merge_request, source_project: project, target_project: project, milestone: milestone) }
+  let(:label_1) { create(:label, title: 'label_1', project: project, priority: 1) }
+  let(:label_2) { create(:label, title: 'label_2', project: project, priority: 2) }
+  let(:label_3) { create(:label, title: 'label_3', project: project) }
 
   before do
     project.team << [member, :developer]
     project.team << [guest, :guest]
   end
 
+  describe '#issues_visible_to_user' do
+    it 'sorts issues by label priority' do
+      issue.labels << label_1
+      security_issue_1.labels << label_2
+      closed_issue_1.labels << label_3
+
+      issues = milestone.issues_visible_to_user(member)
+
+      expect(issues.first).to eq(issue)
+      expect(issues.second).to eq(security_issue_1)
+      expect(issues.third).not_to eq(closed_issue_1)
+    end
+  end
+
+  describe '#sorted_merge_requests' do
+    let(:merge_request_1) { create(:merge_request, :simple, source_project: project, source_branch: 'branch_1', milestone: milestone) }
+    let(:merge_request_2) { create(:merge_request, :simple, source_project: project, source_branch: 'branch_2', milestone: milestone) }
+    let(:merge_request_3) { create(:merge_request, :simple, source_project: project, source_branch: 'branch_3', milestone: milestone) }
+
+    it 'sorts merge requests by label priority' do
+      merge_request_2.labels << label_1
+      merge_request_1.labels << label_2
+      merge_request_3.labels << label_3
+
+      merge_requests = milestone.sorted_merge_requests
+
+      expect(merge_requests.first).to eq(merge_request_2)
+      expect(merge_requests.second).to eq(merge_request_1)
+      expect(merge_requests.third).to eq(merge_request_3)
+    end
+  end
+
   describe '#closed_items_count' do
     it 'does not count confidential issues for non project members' do
       expect(milestone.closed_items_count(non_member)).to eq 2
diff --git a/spec/models/group_milestone_spec.rb b/spec/models/group_milestone_spec.rb
index 916afb7aaf..b81d985b15 100644
--- a/spec/models/group_milestone_spec.rb
+++ b/spec/models/group_milestone_spec.rb
@@ -3,6 +3,7 @@ require 'spec_helper'
 describe GroupMilestone, models: true do
   let(:group) { create(:group) }
   let(:project) { create(:empty_project, group: group) }
+  let(:project_2) { create(:empty_project, group: group) }
   let(:project_milestone) do
     create(:milestone, title: "Milestone v1.2", project: project)
   end
diff --git a/spec/models/milestone_spec.rb b/spec/models/milestone_spec.rb
index aa1ce89ffd..20b96c08a8 100644
--- a/spec/models/milestone_spec.rb
+++ b/spec/models/milestone_spec.rb
@@ -144,35 +144,6 @@ describe Milestone, models: true do
     end
   end
 
-  describe '#sort_issues' do
-    let(:milestone) { create(:milestone) }
-
-    let(:issue1) { create(:issue, milestone: milestone, position: 1) }
-    let(:issue2) { create(:issue, milestone: milestone, position: 2) }
-    let(:issue3) { create(:issue, milestone: milestone, position: 3) }
-    let(:issue4) { create(:issue, position: 42) }
-
-    it 'sorts the given issues' do
-      milestone.sort_issues([issue3.id, issue2.id, issue1.id])
-
-      issue1.reload
-      issue2.reload
-      issue3.reload
-
-      expect(issue1.position).to eq(3)
-      expect(issue2.position).to eq(2)
-      expect(issue3.position).to eq(1)
-    end
-
-    it 'ignores issues not part of the milestone' do
-      milestone.sort_issues([issue3.id, issue2.id, issue1.id, issue4.id])
-
-      issue4.reload
-
-      expect(issue4.position).to eq(42)
-    end
-  end
-
   describe '.search' do
     let(:milestone) { create(:milestone, title: 'foo', description: 'bar') }
 
-- 
2.30.9