diff options
Diffstat (limited to 'spec/services/suggestions/apply_service_spec.rb')
-rw-r--r-- | spec/services/suggestions/apply_service_spec.rb | 104 |
1 files changed, 76 insertions, 28 deletions
diff --git a/spec/services/suggestions/apply_service_spec.rb b/spec/services/suggestions/apply_service_spec.rb index 3e7594bd30f..77d0e892725 100644 --- a/spec/services/suggestions/apply_service_spec.rb +++ b/spec/services/suggestions/apply_service_spec.rb @@ -32,8 +32,8 @@ RSpec.describe Suggestions::ApplyService do create(:suggestion, :content_from_repo, suggestion_args) end - def apply(suggestions) - result = apply_service.new(user, *suggestions).execute + def apply(suggestions, custom_message = nil) + result = apply_service.new(user, *suggestions, message: custom_message).execute suggestions.map { |suggestion| suggestion.reload } @@ -74,6 +74,14 @@ RSpec.describe Suggestions::ApplyService do expect(commit.author_name).to eq(user.name) end + it 'tracks apply suggestion event' do + expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter) + .to receive(:track_apply_suggestion_action) + .with(user: user) + + apply(suggestions) + end + context 'when a custom suggestion commit message' do before do project.update!(suggestion_commit_message: message) @@ -103,6 +111,16 @@ RSpec.describe Suggestions::ApplyService do end end end + + context 'with a user suggested commit message' do + let(:message) { "i'm a custom commit message!" } + + it "uses the user's commit message" do + apply(suggestions, message) + + expect(project.repository.commit.message).to(eq(message)) + end + end end subject(:apply_service) { described_class } @@ -570,56 +588,84 @@ RSpec.describe Suggestions::ApplyService do project.add_maintainer(user) end + shared_examples_for 'service not tracking apply suggestion event' do + it 'does not track apply suggestion event' do + expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter) + .not_to receive(:track_apply_suggestion_action) + + result + end + end + context 'diff file was not found' do - it 'returns error message' do - expect(suggestion.note).to receive(:latest_diff_file) { nil } + let(:result) { apply_service.new(user, suggestion).execute } - result = apply_service.new(user, suggestion).execute + before do + expect(suggestion.note).to receive(:latest_diff_file) { nil } + end + it 'returns error message' do expect(result).to eq(message: 'A file was not found.', status: :error) end + + it_behaves_like 'service not tracking apply suggestion event' end context 'when not all suggestions belong to the same branch' do - it 'renders error message' do - merge_request2 = create(:merge_request, - :conflict, - source_project: project, - target_project: project) - - position2 = Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb", - new_path: "files/ruby/popen.rb", - old_line: nil, - new_line: 15, - diff_refs: merge_request2 - .diff_refs) + let(:merge_request2) do + create( + :merge_request, + :conflict, + source_project: project, + target_project: project + ) + end - diff_note2 = create(:diff_note_on_merge_request, - noteable: merge_request2, - position: position2, - project: project) + let(:position2) do + Gitlab::Diff::Position.new( + old_path: "files/ruby/popen.rb", + new_path: "files/ruby/popen.rb", + old_line: nil, + new_line: 15, + diff_refs: merge_request2.diff_refs + ) + end - other_branch_suggestion = create(:suggestion, note: diff_note2) + let(:diff_note2) do + create( + :diff_note_on_merge_request, + noteable: merge_request2, + position: position2, + project: project + ) + end - result = apply_service.new(user, suggestion, other_branch_suggestion).execute + let(:other_branch_suggestion) { create(:suggestion, note: diff_note2) } + let(:result) { apply_service.new(user, suggestion, other_branch_suggestion).execute } + it 'renders error message' do expect(result).to eq(message: 'Suggestions must all be on the same branch.', status: :error) end + + it_behaves_like 'service not tracking apply suggestion event' end context 'suggestion is not appliable' do let(:inapplicable_reason) { "Can't apply this suggestion." } + let(:result) { apply_service.new(user, suggestion).execute } - it 'returns error message' do + before do expect(suggestion).to receive(:appliable?).and_return(false) expect(suggestion).to receive(:inapplicable_reason).and_return(inapplicable_reason) + end - result = apply_service.new(user, suggestion).execute - + it 'returns error message' do expect(result).to eq(message: inapplicable_reason, status: :error) end + + it_behaves_like 'service not tracking apply suggestion event' end context 'lines of suggestions overlap' do @@ -632,12 +678,14 @@ RSpec.describe Suggestions::ApplyService do create_suggestion(to_content: "I Overlap!") end - it 'returns error message' do - result = apply_service.new(user, suggestion, overlapping_suggestion).execute + let(:result) { apply_service.new(user, suggestion, overlapping_suggestion).execute } + it 'returns error message' do expect(result).to eq(message: 'Suggestions are not applicable as their lines cannot overlap.', status: :error) end + + it_behaves_like 'service not tracking apply suggestion event' end end end |