Commit b08817e3 authored by Rémy Coutable's avatar Rémy Coutable

Reduce differences in spec/support/shared_examples/mentionable_shared_examples.rb

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent cefa9e22
# frozen_string_literal: true
require 'spec_helper'
describe Commit do
it_behaves_like 'a mentionable with EE-specific mentions' do
subject { create(:project, :repository).commit }
let(:author) { create(:user, email: subject.author_email) }
let(:backref_text) { "commit #{subject.id}" }
let(:set_mentionable_text) do
->(txt) { allow(subject).to receive(:safe_message).and_return(txt) }
end
# Include the subject in the repository stub.
let(:extra_commits) { [subject] }
end
end
...@@ -27,6 +27,13 @@ describe Issue do ...@@ -27,6 +27,13 @@ describe Issue do
end end
end end
it_behaves_like 'an editable mentionable with EE-specific mentions' do
subject { create(:issue, project: create(:project, :repository)) }
let(:backref_text) { "issue #{subject.to_reference}" }
let(:set_mentionable_text) { ->(txt) { subject.description = txt } }
end
describe '#related_issues' do describe '#related_issues' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:authorized_project) { create(:project) } let(:authorized_project) { create(:project) }
......
...@@ -17,6 +17,13 @@ describe MergeRequest do ...@@ -17,6 +17,13 @@ describe MergeRequest do
it { is_expected.to have_many(:approved_by_users) } it { is_expected.to have_many(:approved_by_users) }
end end
it_behaves_like 'an editable mentionable with EE-specific mentions' do
subject { create(:merge_request, :simple) }
let(:backref_text) { "merge request #{subject.to_reference}" }
let(:set_mentionable_text) { ->(txt) { subject.description = txt } }
end
describe 'approval_rules' do describe 'approval_rules' do
context 'when project contains approval_rules' do context 'when project contains approval_rules' do
let!(:project_rule1) { project.approval_rules.create(name: 'p1') } let!(:project_rule1) { project.approval_rules.create(name: 'p1') }
......
# frozen_string_literal: true
require 'spec_helper'
describe Note do
it_behaves_like 'an editable mentionable with EE-specific mentions' do
subject { create :note, noteable: issue, project: issue.project }
let(:issue) { create(:issue, project: create(:project, :repository)) }
let(:backref_text) { issue.gfm_reference }
let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
end
end
# frozen_string_literal: true
# Specifications for behavior common to all Mentionable implementations.
# Requires a shared context containing:
# - subject { "the mentionable implementation" }
# - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
# - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
shared_context 'mentionable context with EE-specific mentions' do
let(:group) { create(:group) }
let(:mentioned_epic) { create(:epic, group: group) }
let(:ref_string) do
<<-MSG.strip_heredoc
These references are new:
Epic: #{mentioned_epic.to_reference(project)}
MSG
end
before do
stub_licensed_features(epics: true)
end
end
shared_examples 'a mentionable with EE-specific mentions' do
include_context 'mentionable context'
include_context 'mentionable context with EE-specific mentions'
it "extracts references from its reference property" do
# De-duplicate and omit itself
refs = subject.referenced_mentionables
expect(refs.size).to eq(1)
expect(refs).to include(mentioned_epic)
end
it 'creates cross-reference notes' do
mentioned_objects = [mentioned_epic]
mentioned_objects.each do |referenced|
expect(SystemNoteService).to receive(:cross_reference)
.with(referenced, subject.local_reference, author)
end
subject.create_cross_references!
end
end
shared_examples 'an editable mentionable with EE-specific mentions' do
include_context 'mentionable context'
include_context 'mentionable context with EE-specific mentions'
it_behaves_like 'a mentionable with EE-specific mentions'
let(:new_epic) { create(:epic, group: group) }
it 'creates new cross-reference notes when the mentionable text is edited' do
subject.save
subject.create_cross_references!
new_text = <<-MSG.strip_heredoc
These references already existed:
Issue: #{mentioned_epic.to_reference(project)}
---
This reference are introduced in an edit:
Epic: #{new_epic.to_reference(project)}
MSG
# These four objects were already referenced, and should not receive new
# notes
[mentioned_epic].each do |oldref|
expect(SystemNoteService).not_to receive(:cross_reference)
.with(oldref, any_args)
end
# These two issues and an epic are new and should receive reference notes
# In the case of MergeRequests remember that cannot mention commits included in the MergeRequest
[new_epic].each do |newref|
expect(SystemNoteService).to receive(:cross_reference)
.with(newref, subject.local_reference, author)
end
set_mentionable_text.call(new_text)
subject.create_new_cross_references!(author)
end
end
...@@ -5,13 +5,11 @@ ...@@ -5,13 +5,11 @@
# - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } } # - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
shared_context 'mentionable context' do shared_context 'mentionable context' do
let(:group) { create(:group) }
let(:project) { subject.project } let(:project) { subject.project }
let(:author) { subject.author } let(:author) { subject.author }
let(:mentioned_issue) { create(:issue, project: project) } let(:mentioned_issue) { create(:issue, project: project) }
let!(:mentioned_mr) { create(:merge_request, source_project: project) } let!(:mentioned_mr) { create(:merge_request, source_project: project) }
let(:mentioned_epic) { create(:epic, group: group) }
let(:mentioned_commit) { project.commit("HEAD~1") } let(:mentioned_commit) { project.commit("HEAD~1") }
let(:ext_proj) { create(:project, :public, :repository) } let(:ext_proj) { create(:project, :public, :repository) }
...@@ -29,7 +27,6 @@ shared_context 'mentionable context' do ...@@ -29,7 +27,6 @@ shared_context 'mentionable context' do
These references are new: These references are new:
Issue: #{mentioned_issue.to_reference} Issue: #{mentioned_issue.to_reference}
Merge: #{mentioned_mr.to_reference} Merge: #{mentioned_mr.to_reference}
Epic: #{mentioned_epic.to_reference(project)}
Commit: #{mentioned_commit.to_reference} Commit: #{mentioned_commit.to_reference}
This reference is a repeat and should only be mentioned once: This reference is a repeat and should only be mentioned once:
...@@ -46,8 +43,6 @@ shared_context 'mentionable context' do ...@@ -46,8 +43,6 @@ shared_context 'mentionable context' do
end end
before do before do
stub_licensed_features(epics: true)
# Wire the project's repository to return the mentioned commit, and +nil+ # Wire the project's repository to return the mentioned commit, and +nil+
# for any unrecognized commits. # for any unrecognized commits.
allow_any_instance_of(::Repository).to receive(:commit).and_call_original allow_any_instance_of(::Repository).to receive(:commit).and_call_original
...@@ -72,10 +67,9 @@ shared_examples 'a mentionable' do ...@@ -72,10 +67,9 @@ shared_examples 'a mentionable' do
it "extracts references from its reference property" do it "extracts references from its reference property" do
# De-duplicate and omit itself # De-duplicate and omit itself
refs = subject.referenced_mentionables refs = subject.referenced_mentionables
expect(refs.size).to eq(7) expect(refs.size).to eq(6)
expect(refs).to include(mentioned_issue) expect(refs).to include(mentioned_issue)
expect(refs).to include(mentioned_mr) expect(refs).to include(mentioned_mr)
expect(refs).to include(mentioned_epic)
expect(refs).to include(mentioned_commit) expect(refs).to include(mentioned_commit)
expect(refs).to include(ext_issue) expect(refs).to include(ext_issue)
expect(refs).to include(ext_mr) expect(refs).to include(ext_mr)
...@@ -83,7 +77,7 @@ shared_examples 'a mentionable' do ...@@ -83,7 +77,7 @@ shared_examples 'a mentionable' do
end end
it 'creates cross-reference notes' do it 'creates cross-reference notes' do
mentioned_objects = [mentioned_issue, mentioned_mr, mentioned_epic, mentioned_commit, mentioned_objects = [mentioned_issue, mentioned_mr, mentioned_commit,
ext_issue, ext_mr, ext_commit] ext_issue, ext_mr, ext_commit]
mentioned_objects.each do |referenced| mentioned_objects.each do |referenced|
...@@ -103,7 +97,6 @@ shared_examples 'an editable mentionable' do ...@@ -103,7 +97,6 @@ shared_examples 'an editable mentionable' do
let(:new_issues) do let(:new_issues) do
[create(:issue, project: project), create(:issue, project: ext_proj)] [create(:issue, project: project), create(:issue, project: ext_proj)]
end end
let(:new_epic) { create(:epic, group: group) }
it 'creates new cross-reference notes when the mentionable text is edited' do it 'creates new cross-reference notes when the mentionable text is edited' do
subject.save subject.save
...@@ -114,8 +107,6 @@ shared_examples 'an editable mentionable' do ...@@ -114,8 +107,6 @@ shared_examples 'an editable mentionable' do
Issue: #{mentioned_issue.to_reference} Issue: #{mentioned_issue.to_reference}
Issue: #{mentioned_epic.to_reference(project)}
Commit: #{mentioned_commit.to_reference} Commit: #{mentioned_commit.to_reference}
--- ---
...@@ -126,26 +117,23 @@ shared_examples 'an editable mentionable' do ...@@ -126,26 +117,23 @@ shared_examples 'an editable mentionable' do
--- ---
These three references are introduced in an edit: These two references are introduced in an edit:
Issue: #{new_issues[0].to_reference} Issue: #{new_issues[0].to_reference}
Cross: #{new_issues[1].to_reference(project)} Cross: #{new_issues[1].to_reference(project)}
Epic: #{new_epic.to_reference(project)}
MSG MSG
# These four objects were already referenced, and should not receive new # These three objects were already referenced, and should not receive new
# notes # notes
[mentioned_issue, mentioned_commit, mentioned_epic, ext_issue].each do |oldref| [mentioned_issue, mentioned_commit, ext_issue].each do |oldref|
expect(SystemNoteService).not_to receive(:cross_reference) expect(SystemNoteService).not_to receive(:cross_reference)
.with(oldref, any_args) .with(oldref, any_args)
end end
# These two issues and an epic are new and should receive reference notes # These two issues are new and should receive reference notes
# In the case of MergeRequests remember that cannot mention commits included in the MergeRequest # In the case of MergeRequests remember that cannot mention commits included in the MergeRequest
new_mentionables = new_issues + [new_epic] new_issues.each do |newref|
new_mentionables.each do |newref|
expect(SystemNoteService).to receive(:cross_reference) expect(SystemNoteService).to receive(:cross_reference)
.with(newref, subject.local_reference, author) .with(newref, subject.local_reference, author)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment