summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/diff/rendered/notebook/diff_file_spec.rb
blob: 15edbc224600006b130abfe1fccbf1cb0dd8ed9e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Diff::Rendered::Notebook::DiffFile do
  include RepoHelpers

  let(:project) { create(:project, :repository) }
  let(:commit) { project.commit("5d6ed1503801ca9dc28e95eeb85a7cf863527aee") }
  let(:diffs) { commit.raw_diffs.to_a }
  let(:diff) { diffs.first }
  let(:source) { Gitlab::Diff::File.new(diff, diff_refs: commit.diff_refs, repository: project.repository) }
  let(:nb_file) { described_class.new(source) }

  describe '#old_blob and #new_blob' do
    context 'when file is changed' do
      it 'transforms the old blob' do
        expect(nb_file.old_blob.data).to include('%%')
      end

      it 'transforms the new blob' do
        expect(nb_file.new_blob.data).to include('%%')
      end
    end

    context 'when file is added' do
      let(:diff) { diffs[1] }

      it 'old_blob is empty' do
        expect(nb_file.old_blob).to be_nil
      end

      it 'new_blob is transformed' do
        expect(nb_file.new_blob.data).to include('%%')
      end
    end

    context 'when file is removed' do
      let(:diff) { diffs[2] }

      it 'old_blob is transformed' do
        expect(nb_file.old_blob.data).to include('%%')
      end

      it 'new_blob is empty' do
        expect(nb_file.new_blob).to be_nil
      end
    end
  end

  describe '#diff' do
    context 'for valid notebooks' do
      it 'returns the transformed diff' do
        expect(nb_file.diff.diff).to include('%%')
      end
    end

    context 'for invalid notebooks' do
      let(:commit) { project.commit("6d85bb693dddaee631ec0c2f697c52c62b93f6d3") }
      let(:diff) { diffs[1] }

      it 'returns nil' do
        expect(nb_file.diff).to be_nil
      end
    end
  end

  describe '#has_renderable?' do
    context 'notebook diff is empty' do
      let(:commit) { project.commit("a867a602d2220e5891b310c07d174fbe12122830") }

      it 'is false' do
        expect(nb_file.has_renderable?).to be_falsey
      end
    end

    context 'notebook is valid' do
      it 'is true' do
        expect(nb_file.has_renderable?).to be_truthy
      end
    end
  end

  describe '#highlighted_diff_lines?' do
    context 'when line transformed line is not part of the diff' do
      it 'line is not discussable' do
        expect(nb_file.highlighted_diff_lines[0].discussable?).to be_falsey
      end
    end

    context 'when line transformed line part of the diff' do
      it 'line is not discussable' do
        expect(nb_file.highlighted_diff_lines[12].discussable?).to be_truthy
      end
    end

    context 'assigns the correct position' do
      it 'computes de first line where the remove would appear' do
        expect(nb_file.highlighted_diff_lines[0].old_pos).to eq(3)
        expect(nb_file.highlighted_diff_lines[0].new_pos).to eq(3)

        expect(nb_file.highlighted_diff_lines[12].new_pos).to eq(15)
        expect(nb_file.highlighted_diff_lines[12].old_pos).to eq(18)
      end
    end
  end
end