Commit 0e9c4a90 authored by Nick Thomas's avatar Nick Thomas

DRY up the added update service specs, add two email helpers

parent b62954db
...@@ -320,37 +320,10 @@ describe Issues::UpdateService, services: true do ...@@ -320,37 +320,10 @@ describe Issues::UpdateService, services: true do
end end
end end
context 'updated user mentions' do context 'updating mentions' do
let(:user4) { create(:user) } let(:mentionable) { issue }
before do include_examples 'updating mentions', Issues::UpdateService
project.team << [user4, :developer]
end
context "in title" do
before do
perform_enqueued_jobs { update_issue(title: user4.to_reference) }
end
it 'emails only the newly-mentioned user' do
should_not_email(user)
should_not_email(user2)
should_not_email(user3)
should_email(user4)
end
end
context 'in description' do
before do
perform_enqueued_jobs { update_issue(description: user4.to_reference) }
end
it 'emails only the newly-mentioned user' do
should_not_email(user)
should_not_email(user2)
should_not_email(user3)
should_email(user4)
end
end
end end
end end
end end
...@@ -226,37 +226,9 @@ describe MergeRequests::UpdateService, services: true do ...@@ -226,37 +226,9 @@ describe MergeRequests::UpdateService, services: true do
end end
end end
context 'updated user mentions' do context 'updating mentions' do
let(:user4) { create(:user) } let(:mentionable) { merge_request }
before do include_examples 'updating mentions', MergeRequests::UpdateService
project.team << [user4, :developer]
end
context 'in title' do
before do
perform_enqueued_jobs { update_merge_request(title: user4.to_reference) }
end
it 'emails only the newly-mentioned user' do
should_not_email(user)
should_not_email(user2)
should_not_email(user3)
should_email(user4)
end
end
context 'in description' do
before do
perform_enqueued_jobs { update_merge_request(description: user4.to_reference) }
end
it 'emails only the newly-mentioned user' do
should_not_email(user)
should_not_email(user2)
should_not_email(user3)
should_email(user4)
end
end
end end
context 'when MergeRequest has tasks' do context 'when MergeRequest has tasks' do
......
...@@ -3,6 +3,16 @@ module EmailHelpers ...@@ -3,6 +3,16 @@ module EmailHelpers
ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1 ActionMailer::Base.deliveries.map(&:to).flatten.count(user.email) == 1
end end
def reset_delivered_emails!
ActionMailer::Base.deliveries.clear
end
def should_only_email(*users)
users.each {|user| should_email(user) }
recipients = ActionMailer::Base.deliveries.flat_map(&:to)
expect(recipients.count).to eq(users.count)
end
def should_email(user) def should_email(user)
expect(sent_to_user?(user)).to be_truthy expect(sent_to_user?(user)).to be_truthy
end end
......
RSpec.shared_examples 'updating mentions' do |service_class|
let(:mentioned_user) { create(:user) }
let(:service_class) { service_class }
before { project.team << [mentioned_user, :developer] }
def update_mentionable(opts)
reset_delivered_emails!
perform_enqueued_jobs do
service_class.new(project, user, opts).execute(mentionable)
end
mentionable.reload
end
context 'in title' do
before { update_mentionable(title: mentioned_user.to_reference) }
it 'emails only the newly-mentioned user' do
should_only_email(mentioned_user)
end
end
context 'in description' do
before { update_mentionable(description: mentioned_user.to_reference) }
it 'emails only the newly-mentioned user' do
should_only_email(mentioned_user)
end
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