File: draft_note_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (122 lines) | stat: -rw-r--r-- 3,711 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DraftNote, feature_category: :code_review_workflow do
  include RepoHelpers

  let_it_be(:project)       { create(:project, :repository) }
  let_it_be(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }

  describe 'validations' do
    it_behaves_like 'a valid diff positionable note' do
      subject { build(:draft_note, merge_request: merge_request, commit_id: commit_id, position: position) }
    end
  end

  describe 'delegations' do
    it { is_expected.to delegate_method(:file_path).to(:diff_file).allow_nil }
    it { is_expected.to delegate_method(:file_hash).to(:diff_file).allow_nil }
    it { is_expected.to delegate_method(:file_identifier_hash).to(:diff_file).allow_nil }
  end

  describe 'enums' do
    let(:note_types) do
      { Note: 0, DiffNote: 1, DiscussionNote: 2 }
    end

    it { is_expected.to define_enum_for(:note_type).with_values(**note_types) }
  end

  describe '#line_code' do
    describe 'stored line_code' do
      let(:draft_note) { build(:draft_note, merge_request: merge_request, line_code: '1234567890') }

      it 'returns stored line_code' do
        expect(draft_note.line_code).to eq('1234567890')
      end
    end

    describe 'none stored line_code' do
      let(:draft_note) { build(:draft_note, merge_request: merge_request) }

      before do
        allow(draft_note).to receive(:find_line_code).and_return('none stored line_code')
      end

      it 'returns found line_code' do
        expect(draft_note.line_code).to eq('none stored line_code')
      end
    end
  end

  describe '#diff_file' do
    let(:draft_note) { build(:draft_note, merge_request: merge_request) }

    context 'when diff_file exists' do
      it "returns an unfolded diff_file" do
        diff_file = instance_double(Gitlab::Diff::File)
        expect(draft_note.original_position).to receive(:diff_file).with(project.repository).and_return(diff_file)
        expect(diff_file).to receive(:unfold_diff_lines).with(draft_note.original_position)

        expect(draft_note.diff_file).to be diff_file
      end
    end

    context 'when diff_file does not exist' do
      it 'returns nil' do
        expect(draft_note.original_position).to receive(:diff_file).with(project.repository).and_return(nil)

        expect(draft_note.diff_file).to be_nil
      end
    end
  end

  describe '#type' do
    let(:draft_note) { build(:draft_note, merge_request: merge_request) }

    context 'when note_type is present' do
      before do
        draft_note.note_type = 'DiffNote'
      end

      it 'returns the note_type' do
        expect(draft_note.type).to eq('DiffNote')
      end
    end

    context 'when note_type is not present' do
      context 'when on_diff? is true' do
        before do
          allow(draft_note).to receive(:on_diff?).and_return(true)
        end

        it 'returns "DiffNote"' do
          expect(draft_note.type).to eq('DiffNote')
        end
      end

      context 'when on_diff? is false and discussion_id is present' do
        before do
          allow(draft_note).to receive(:on_diff?).and_return(false)
          draft_note.discussion_id = 'some_id'
        end

        it 'returns "DiscussionNote"' do
          expect(draft_note.type).to eq('DiscussionNote')
        end
      end

      context 'when on_diff? is false and discussion_id is not present' do
        before do
          allow(draft_note).to receive(:on_diff?).and_return(false)
          draft_note.discussion_id = nil
        end

        it 'returns "Note"' do
          expect(draft_note.type).to eq('Note')
        end
      end
    end
  end
end