From 959e6d221d6b11c64d17fc9d4734200806c293ab Mon Sep 17 00:00:00 2001
From: Ashley Dumaine <adumaine@datto.com>
Date: Wed, 30 Aug 2017 13:58:19 -0400
Subject: [PATCH] add unit tests for new collapse_outdated_diff_comments toggle

---
 .../collapse_outdated_diff_comments.rb        | 76 +++++++++++++++++++
 spec/requests/api/projects_spec.rb            | 30 ++++++++
 spec/requests/api/v3/projects_spec.rb         | 26 +++++++
 3 files changed, 132 insertions(+)
 create mode 100644 spec/features/merge_requests/collapse_outdated_diff_comments.rb

diff --git a/spec/features/merge_requests/collapse_outdated_diff_comments.rb b/spec/features/merge_requests/collapse_outdated_diff_comments.rb
new file mode 100644
index 00000000000..baca34026ea
--- /dev/null
+++ b/spec/features/merge_requests/collapse_outdated_diff_comments.rb
@@ -0,0 +1,76 @@
+require 'spec_helper'
+
+feature 'Collapse outdated diff comments', js: true do
+  let(:merge_request) { create(:merge_request, importing: true) }
+  let(:project) { merge_request.source_project }
+
+  let!(:outdated_discussion) { create(:diff_note_on_merge_request, project: project, noteable: merge_request, position: outdated_position).to_discussion }
+  let!(:active_discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
+
+  let(:outdated_position) do
+    Gitlab::Diff::Position.new(
+      old_path: "files/ruby/popen.rb",
+      new_path: "files/ruby/popen.rb",
+      old_line: nil,
+      new_line: 9,
+      diff_refs: outdated_diff_refs
+    )
+  end
+
+  let(:outdated_diff_refs) { project.commit("874797c3a73b60d2187ed6e2fcabd289ff75171e").diff_refs }
+
+  before do
+    sign_in(create(:admin))
+  end
+
+  context 'when project.collapse_outdated_diff_comments == true' do
+    before do
+      project.update_column(:collapse_outdated_diff_comments, true)
+    end
+
+    context 'with unresolved outdated discussions' do
+      it 'does not show outdated discussion' do
+        visit_merge_request(merge_request)
+        within(".discussion[data-discussion-id='#{outdated_discussion.id}']") do
+          expect(page).to have_css('.discussion-body .hide .js-toggle-content', visible: false)
+        end
+      end
+    end
+
+    context 'with unresolved active discussions' do
+      it 'shows active discussion' do
+        visit_merge_request(merge_request)
+        within(".discussion[data-discussion-id='#{active_discussion.id}']") do
+          expect(page).to have_css('.discussion-body .hide .js-toggle-content', visible: true)
+        end
+      end
+    end
+  end
+
+  context 'when project.collapse_outdated_diff_comments == false' do
+    before do
+      project.update_column(:collapse_outdated_diff_comments, false)
+    end
+
+    context 'with unresolved outdated discussions' do
+      it 'shows outdated discussion' do
+        visit_merge_request(merge_request)
+        within(".discussion[data-discussion-id='#{outdated_discussion.id}']") do
+          expect(page).to have_css('.discussion-body .hide .js-toggle-content', visible: true)
+        end
+      end
+    end
+
+    context 'with unresolved active discussions' do
+      it 'shows active discussion' do
+        visit_merge_request(merge_request)
+        within(".discussion[data-discussion-id='#{active_discussion.id}']") do
+          expect(page).to have_css('.discussion-body .hide .js-toggle-content', visible: true)
+        end
+      end
+    end
+  end
+  def visit_merge_request(merge_request)
+    visit project_merge_request_path(project, merge_request)
+  end
+end
diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
index 4490e50702b..5e3fdbd468e 100644
--- a/spec/requests/api/projects_spec.rb
+++ b/spec/requests/api/projects_spec.rb
@@ -414,6 +414,7 @@ describe API::Projects do
         jobs_enabled: false,
         merge_requests_enabled: false,
         wiki_enabled: false,
+        collapse_outdated_diff_comments: false,
         only_allow_merge_if_pipeline_succeeds: false,
         request_access_enabled: true,
         only_allow_merge_if_all_discussions_are_resolved: false,
@@ -477,6 +478,18 @@ describe API::Projects do
       expect(json_response['avatar_url']).to eq("http://localhost/uploads/-/system/project/avatar/#{project_id}/banana_sample.gif")
     end
 
+    it 'sets a project as allowing outdated diff comments to collapse regardless of discussion resolution' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: false })
+      post api('/projects', user), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_falsey
+    end
+
+    it 'sets a project as allowing outdated diff comments to collapse if collapse_outdated_diff_comments' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: true })
+      post api('/projects', user), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_truthy
+    end
+
     it 'sets a project as allowing merge even if build fails' do
       project = attributes_for(:project, { only_allow_merge_if_pipeline_succeeds: false })
       post api('/projects', user), project
@@ -642,6 +655,22 @@ describe API::Projects do
       expect(json_response['visibility']).to eq('private')
     end
 
+    it 'sets a project as allowing outdated diff comments to collapse regardless of discussion resolution' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: false })
+
+      post api("/projects/user/#{user.id}", admin), project
+
+      expect(json_response['collapse_outdated_diff_comments']).to be_falsey
+    end
+
+    it 'sets a project as allowing outdated diff comments to collapse only if collapse_outdated_diff_comments' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: true })
+
+      post api("/projects/user/#{user.id}", admin), project
+
+      expect(json_response['collapse_outdated_diff_comments']).to be_truthy
+    end
+
     it 'sets a project as allowing merge even if build fails' do
       project = attributes_for(:project, { only_allow_merge_if_pipeline_succeeds: false })
       post api("/projects/user/#{user.id}", admin), project
@@ -732,6 +761,7 @@ describe API::Projects do
         expect(json_response['wiki_enabled']).to be_present
         expect(json_response['jobs_enabled']).to be_present
         expect(json_response['snippets_enabled']).to be_present
+        expect(json_response['collapse_outdated_diff_comments']).to eq(project.collapse_outdated_diff_comments)
         expect(json_response['container_registry_enabled']).to be_present
         expect(json_response['created_at']).to be_present
         expect(json_response['last_activity_at']).to be_present
diff --git a/spec/requests/api/v3/projects_spec.rb b/spec/requests/api/v3/projects_spec.rb
index a514166274a..bbe56c972bd 100644
--- a/spec/requests/api/v3/projects_spec.rb
+++ b/spec/requests/api/v3/projects_spec.rb
@@ -394,6 +394,7 @@ describe API::V3::Projects do
         issues_enabled: false,
         merge_requests_enabled: false,
         wiki_enabled: false,
+        collapse_outdated_diff_comments: false,
         only_allow_merge_if_build_succeeds: false,
         request_access_enabled: true,
         only_allow_merge_if_all_discussions_are_resolved: false
@@ -455,6 +456,18 @@ describe API::V3::Projects do
       expect(json_response['visibility_level']).to eq(Gitlab::VisibilityLevel::PRIVATE)
     end
 
+    it 'sets a project as allowing outdated diff comments to collapse regardless of discussion resolution' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: false })
+      post v3_api('/projects', user), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_falsey
+    end
+
+    it 'sets a project as allowing outdated diff comments to collapse if collapse_outdated_diff_comments' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: true })
+      post v3_api('/projects', user), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_truthy
+    end
+
     it 'sets a project as allowing merge even if build fails' do
       project = attributes_for(:project, { only_allow_merge_if_build_succeeds: false })
       post v3_api('/projects', user), project
@@ -599,6 +612,18 @@ describe API::V3::Projects do
       expect(json_response['visibility_level']).to eq(Gitlab::VisibilityLevel::PRIVATE)
     end
 
+    it 'sets a project as allowing outdated diff comments to collapse regardless of discussion resolution' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: false })
+      post v3_api("/projects/user/#{user.id}", admin), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_falsey
+    end
+
+    it 'sets a project as allowing outdated diff comments to collapse only if collapse_outdated_diff_comments' do
+      project = attributes_for(:project, { collapse_outdated_diff_comments: true })
+      post v3_api("/projects/user/#{user.id}", admin), project
+      expect(json_response['collapse_outdated_diff_comments']).to be_truthy
+    end
+
     it 'sets a project as allowing merge even if build fails' do
       project = attributes_for(:project, { only_allow_merge_if_build_succeeds: false })
       post v3_api("/projects/user/#{user.id}", admin), project
@@ -687,6 +712,7 @@ describe API::V3::Projects do
         expect(json_response['wiki_enabled']).to be_present
         expect(json_response['builds_enabled']).to be_present
         expect(json_response['snippets_enabled']).to be_present
+        expect(json_response['collapse_outdated_diff_comments']).to eq(project.collapse_outdated_diff_comments)
         expect(json_response['container_registry_enabled']).to be_present
         expect(json_response['created_at']).to be_present
         expect(json_response['last_activity_at']).to be_present
-- 
2.30.9