summaryrefslogtreecommitdiff
path: root/spec/services/suggestions/create_service_spec.rb
blob: f1142c88a69d3e22de3d1e45ba33b3eceda64fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

require 'spec_helper'

describe Suggestions::CreateService do
  let(:project_with_repo) { create(:project, :repository) }
  let(:merge_request) do
    create(:merge_request, source_project: project_with_repo,
                           target_project: project_with_repo)
  end

  let(:position) do
    Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb",
                               new_path: "files/ruby/popen.rb",
                               old_line: nil,
                               new_line: 14,
                               diff_refs: merge_request.diff_refs)
  end

  let(:markdown) do
    <<-MARKDOWN.strip_heredoc
        ```suggestion
          foo
          bar
        ```

        ```
          nothing
        ```

        ```suggestion
          xpto
          baz
        ```

        ```thing
          this is not a suggestion, it's a thing
        ```
    MARKDOWN
  end

  subject { described_class.new(note) }

  describe '#execute' do
    context 'should not try to parse suggestions' do
      context 'when not a diff note for merge requests' do
        let(:note) do
          create(:diff_note_on_commit, project: project_with_repo,
                                       note: markdown)
        end

        it 'does not try to parse suggestions' do
          expect(Banzai::SuggestionsParser).not_to receive(:parse)

          subject.execute
        end
      end

      context 'when diff note is not for text' do
        let(:note) do
          create(:diff_note_on_merge_request, project: project_with_repo,
                                              noteable: merge_request,
                                              position: position,
                                              note: markdown)
        end

        it 'does not try to parse suggestions' do
          allow(note).to receive(:on_text?) { false }

          expect(Banzai::SuggestionsParser).not_to receive(:parse)

          subject.execute
        end
      end
    end

    context 'should create suggestions' do
      let(:note) do
        create(:diff_note_on_merge_request, project: project_with_repo,
                                            noteable: merge_request,
                                            position: position,
                                            note: markdown)
      end

      context 'single line suggestions' do
        it 'persists suggestion records' do
          expect { subject.execute }
            .to change { note.suggestions.count }
            .from(0)
            .to(2)
        end

        it 'persists original from_content lines and suggested lines' do
          subject.execute

          suggestions = note.suggestions.order(:relative_order)

          suggestion_1 = suggestions.first
          suggestion_2 = suggestions.last

          expect(suggestion_1).to have_attributes(from_content: "    vars = {\n",
                                                  to_content: "  foo\n  bar\n")

          expect(suggestion_2).to have_attributes(from_content: "    vars = {\n",
                                                  to_content: "  xpto\n  baz\n")
        end
      end
    end
  end
end