BigW Consortium Gitlab

diff_note_spec.rb 7.74 KB
Newer Older
Douwe Maan committed
1 2
require 'spec_helper'

3
describe DiffNote do
Douwe Maan committed
4 5
  include RepoHelpers

6
  let!(:merge_request) { create(:merge_request) }
7
  let(:project) { merge_request.project }
Douwe Maan committed
8 9 10 11
  let(:commit) { project.commit(sample_commit.id) }

  let(:path) { "files/ruby/popen.rb" }

12
  let!(:position) do
Douwe Maan committed
13 14 15 16 17 18 19 20 21
    Gitlab::Diff::Position.new(
      old_path: path,
      new_path: path,
      old_line: nil,
      new_line: 14,
      diff_refs: merge_request.diff_refs
    )
  end

22
  let!(:new_position) do
Douwe Maan committed
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
    Gitlab::Diff::Position.new(
      old_path: path,
      new_path: path,
      old_line: 16,
      new_line: 22,
      diff_refs: merge_request.diff_refs
    )
  end

  subject { create(:diff_note_on_merge_request, project: project, position: position, noteable: merge_request) }

  describe "#position=" do
    context "when provided a string" do
      it "sets the position" do
        subject.position = new_position.to_json

        expect(subject.position).to eq(new_position)
      end
    end

    context "when provided a hash" do
      it "sets the position" do
        subject.position = new_position.to_h

        expect(subject.position).to eq(new_position)
      end
    end

    context "when provided a position object" do
      it "sets the position" do
        subject.position = new_position

        expect(subject.position).to eq(new_position)
      end
    end
  end

60
  describe "#original_position=" do
Douwe Maan committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    context "when provided a string" do
      it "sets the original position" do
        subject.original_position = new_position.to_json

        expect(subject.original_position).to eq(new_position)
      end
    end

    context "when provided a hash" do
      it "sets the original position" do
        subject.original_position = new_position.to_h

        expect(subject.original_position).to eq(new_position)
      end
    end

    context "when provided a position object" do
      it "sets the original position" do
        subject.original_position = new_position

        expect(subject.original_position).to eq(new_position)
      end
    end
84 85
  end

Douwe Maan committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  describe "#diff_file" do
    it "returns the correct diff file" do
      diff_file = subject.diff_file

      expect(diff_file.old_path).to eq(position.old_path)
      expect(diff_file.new_path).to eq(position.new_path)
      expect(diff_file.diff_refs).to eq(position.diff_refs)
    end
  end

  describe "#diff_line" do
    it "returns the correct diff line" do
      diff_line = subject.diff_line

      expect(diff_line.added?).to be true
101
      expect(diff_line.new_line).to eq(position.formatter.new_line)
Douwe Maan committed
102 103 104 105 106 107
      expect(diff_line.text).to eq("+    vars = {")
    end
  end

  describe "#line_code" do
    it "returns the correct line code" do
108
      line_code = Gitlab::Git.diff_line_code(position.file_path, position.formatter.new_line, 15)
Douwe Maan committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

      expect(subject.line_code).to eq(line_code)
    end
  end

  describe "#for_line?" do
    context "when provided the correct diff line" do
      it "returns true" do
        expect(subject.for_line?(subject.diff_line)).to be true
      end
    end

    context "when provided a different diff line" do
      it "returns false" do
        some_line = subject.diff_file.diff_lines.first

        expect(subject.for_line?(some_line)).to be false
      end
    end
  end

  describe "#active?" do
    context "when noteable is a commit" do
Douwe Maan committed
132
      subject { build(:diff_note_on_commit, project: project, position: position) }
Douwe Maan committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

      it "returns true" do
        expect(subject.active?).to be true
      end
    end

    context "when noteable is a merge request" do
      context "when the merge request's diff refs match that of the diff note" do
        it "returns true" do
          expect(subject.active?).to be true
        end
      end

      context "when the merge request's diff refs don't match that of the diff note" do
        before do
148
          allow(subject.noteable).to receive(:diff_refs).and_return(commit.diff_refs)
Douwe Maan committed
149 150 151 152 153 154 155 156 157
        end

        it "returns false" do
          expect(subject.active?).to be false
        end
      end
    end
  end

158 159 160 161
  describe "creation" do
    describe "updating of position" do
      context "when noteable is a commit" do
        let(:diff_note) { create(:diff_note_on_commit, project: project, position: position) }
Douwe Maan committed
162 163

        it "doesn't update the position" do
164
          diff_note
Douwe Maan committed
165

166 167
          expect(diff_note.original_position).to eq(position)
          expect(diff_note.position).to eq(position)
Douwe Maan committed
168 169 170
        end
      end

171 172 173 174 175 176 177 178 179 180
      context "when noteable is a merge request" do
        let(:diff_note) { create(:diff_note_on_merge_request, project: project, position: position, noteable: merge_request) }

        context "when the note is active" do
          it "doesn't update the position" do
            diff_note

            expect(diff_note.original_position).to eq(position)
            expect(diff_note.position).to eq(position)
          end
Douwe Maan committed
181 182
        end

183 184
        context "when the note is outdated" do
          before do
185
            allow(merge_request).to receive(:diff_refs).and_return(commit.diff_refs)
186 187
          end

188
          it "updates the position" do
189
            diff_note
190 191 192

            expect(diff_note.original_position).to eq(position)
            expect(diff_note.position).not_to eq(position)
193
          end
Douwe Maan committed
194 195 196 197
        end
      end
    end
  end
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  describe "#discussion_id" do
    let(:note) { create(:diff_note_on_merge_request) }

    context "when it is newly created" do
      it "has a discussion id" do
        expect(note.discussion_id).not_to be_nil
        expect(note.discussion_id).to match(/\A\h{40}\z/)
      end
    end

    context "when it didn't store a discussion id before" do
      before do
        note.update_column(:discussion_id, nil)
      end

      it "has a discussion id" do
        # The discussion_id is set in `after_initialize`, so `reload` won't work
        reloaded_note = Note.find(note.id)

        expect(reloaded_note.discussion_id).not_to be_nil
        expect(reloaded_note.discussion_id).to match(/\A\h{40}\z/)
      end
    end
  end
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

  describe '#created_at_diff?' do
    let(:diff_refs) { project.commit(sample_commit.id).diff_refs }
    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: diff_refs
      )
    end

    context "when noteable is a commit" do
      subject { build(:diff_note_on_commit, project: project, position: position) }

      it "returns true" do
        expect(subject.created_at_diff?(diff_refs)).to be true
      end
    end

    context "when noteable is a merge request" do
      context "when the diff refs match the original one of the diff note" do
        it "returns true" do
          expect(subject.created_at_diff?(diff_refs)).to be true
        end
      end

      context "when the diff refs don't match the original one of the diff note" do
        it "returns false" do
          expect(subject.created_at_diff?(merge_request.diff_refs)).to be false
        end
      end
    end
  end
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

  describe "image diff notes" do
    let(:path) { "files/images/any_image.png" }

    let!(:position) do
      Gitlab::Diff::Position.new(
        old_path: path,
        new_path: path,
        width: 10,
        height: 10,
        x: 1,
        y: 1,
        diff_refs: merge_request.diff_refs,
        position_type: "image"
      )
    end

    describe "validations" do
      subject { build(:diff_note_on_merge_request, project: project, position: position, noteable: merge_request) }

      it { is_expected.not_to validate_presence_of(:line_code) }

      it "does not validate diff line" do
        diff_line = subject.diff_line

        expect(diff_line).to be nil
        expect(subject).to be_valid
      end
    end

    it "returns true for on_image?" do
      expect(subject.on_image?).to be_truthy
    end
  end
Douwe Maan committed
292
end