diff_helper_spec.rb 7.44 KB
Newer Older
Marin Jankovski's avatar
Marin Jankovski committed
1 2 3 4 5 6
require 'spec_helper'

describe DiffHelper do
  include RepoHelpers

  let(:project) { create(:project) }
Rubén Dávila's avatar
Rubén Dávila committed
7
  let(:repository) { project.repository }
8
  let(:commit) { project.commit(sample_commit.id) }
9
  let(:diffs) { commit.raw_diffs }
10
  let(:diff) { diffs.first }
11
  let(:diff_refs) { [commit.parent, commit] }
12
  let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
Marin Jankovski's avatar
Marin Jankovski committed
13

14 15 16 17
  describe 'diff_view' do
    it 'returns a valid value when cookie is set' do
      helper.request.cookies[:diff_view] = 'parallel'

18
      expect(helper.diff_view).to eq :parallel
19 20 21 22 23
    end

    it 'returns a default value when cookie is invalid' do
      helper.request.cookies[:diff_view] = 'invalid'

24
      expect(helper.diff_view).to eq :inline
25 26 27 28 29
    end

    it 'returns a default value when cookie is nil' do
      expect(helper.request.cookies).to be_empty

30
      expect(helper.diff_view).to eq :inline
31 32
    end
  end
33

34
  describe 'diff_options' do
35
    it 'returns no collapse false' do
36 37 38
      expect(diff_options).to include(no_collapse: false)
    end

39
    it 'returns no collapse true if expand_all_diffs' do
40 41 42 43
      allow(controller).to receive(:params) { { expand_all_diffs: true } }
      expect(diff_options).to include(no_collapse: true)
    end

44
    it 'returns no collapse true if action name diff_for_path' do
45 46 47
      allow(controller).to receive(:action_name) { 'diff_for_path' }
      expect(diff_options).to include(no_collapse: true)
    end
48

49
    it 'returns paths if action name diff_for_path and param old path' do
50 51 52 53 54
      allow(controller).to receive(:params) { { old_path: 'lib/wadus.rb' } }
      allow(controller).to receive(:action_name) { 'diff_for_path' }
      expect(diff_options[:paths]).to include('lib/wadus.rb')
    end

55
    it 'returns paths if action name diff_for_path and param new path' do
56 57 58 59
      allow(controller).to receive(:params) { { new_path: 'lib/wadus.rb' } }
      allow(controller).to receive(:action_name) { 'diff_for_path' }
      expect(diff_options[:paths]).to include('lib/wadus.rb')
    end
60 61
  end

Douwe Maan's avatar
Douwe Maan committed
62
  describe '#diff_line_content' do
63 64 65 66 67 68 69 70
    context 'when the line is empty' do
      it 'returns a non breaking space' do
        expect(diff_line_content(nil)).to eq(' ')
      end

      it 'returns an HTML-safe string' do
        expect(diff_line_content(nil)).to be_html_safe
      end
Marin Jankovski's avatar
Marin Jankovski committed
71 72
    end

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
    context 'when the line is not empty' do
      context 'when the line starts with +, -, or a space' do
        it 'strips the first character' do
          expect(diff_line_content('+new line')).to eq('new line')
          expect(diff_line_content('-new line')).to eq('new line')
          expect(diff_line_content(' new line')).to eq('new line')
        end

        context 'when the line is HTML-safe' do
          it 'returns an HTML-safe string' do
            expect(diff_line_content('+new line'.html_safe)).to be_html_safe
            expect(diff_line_content('-new line'.html_safe)).to be_html_safe
            expect(diff_line_content(' new line'.html_safe)).to be_html_safe
          end
        end

        context 'when the line is not HTML-safe' do
          it 'returns a non-HTML-safe string' do
            expect(diff_line_content('+new line')).not_to be_html_safe
            expect(diff_line_content('-new line')).not_to be_html_safe
            expect(diff_line_content(' new line')).not_to be_html_safe
          end
        end
      end

      context 'when the line does not start with a +, -, or a space' do
        it 'returns the string' do
          expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen')).to eq('@@ -6,12 +6,18 @@ module Popen')
        end

        context 'when the line is HTML-safe' do
          it 'returns an HTML-safe string' do
            expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen'.html_safe)).to be_html_safe
          end
        end

        context 'when the line is not HTML-safe' do
          it 'returns a non-HTML-safe string' do
            expect(diff_line_content('@@ -6,12 +6,18 @@ module Popen')).not_to be_html_safe
          end
        end
      end
115
    end
Douwe Maan's avatar
Douwe Maan committed
116 117 118 119 120 121 122 123
  end

  describe "#mark_inline_diffs" do
    let(:old_line) { %{abc 'def'} }
    let(:new_line) { %{abc "def"} }

    it "returns strings with marked inline diffs" do
      marked_old_line, marked_new_line = mark_inline_diffs(old_line, new_line)
124

125
      expect(marked_old_line).to eq("abc <span class='idiff left right deletion'>&#39;def&#39;</span>")
Douwe Maan's avatar
Douwe Maan committed
126
      expect(marked_old_line).to be_html_safe
127
      expect(marked_new_line).to eq("abc <span class='idiff left right addition'>&quot;def&quot;</span>")
Douwe Maan's avatar
Douwe Maan committed
128
      expect(marked_new_line).to be_html_safe
Marin Jankovski's avatar
Marin Jankovski committed
129 130
    end
  end
131 132 133 134 135 136

  describe "#diff_match_line" do
    let(:old_pos) { 40 }
    let(:new_pos) { 50 }
    let(:text) { 'some_text' }

Valery Sizov's avatar
Valery Sizov committed
137
    it "generates foldable top match line for inline view with empty text by default" do
138 139 140 141 142 143 144 145
      output = diff_match_line old_pos, new_pos

      expect(output).to be_html_safe
      expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
      expect(output).to have_css "td:nth-child(2):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: ''
    end

Valery Sizov's avatar
Valery Sizov committed
146
    it "allows to define text and bottom option" do
147 148 149 150 151 152 153 154
      output = diff_match_line old_pos, new_pos, text: text, bottom: true

      expect(output).to be_html_safe
      expect(output).to have_css "td:nth-child(1).diff-line-num.unfold.js-unfold.js-unfold-bottom.old_line[data-linenumber='#{old_pos}']", text: '...'
      expect(output).to have_css "td:nth-child(2).diff-line-num.unfold.js-unfold.js-unfold-bottom.new_line[data-linenumber='#{new_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(3):not(.parallel).line_content.match', text: text
    end

Valery Sizov's avatar
Valery Sizov committed
155
    it "generates match line for parallel view" do
156 157 158 159 160 161 162 163 164
      output = diff_match_line old_pos, new_pos, text: text, view: :parallel

      expect(output).to be_html_safe
      expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
      expect(output).to have_css "td:nth-child(3):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(4).line_content.match.parallel', text: text
    end

Valery Sizov's avatar
Valery Sizov committed
165
    it "allows to generate only left match line for parallel view" do
166 167 168 169 170 171 172 173
      output = diff_match_line old_pos, nil, text: text, view: :parallel

      expect(output).to be_html_safe
      expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.old_line[data-linenumber='#{old_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
      expect(output).not_to have_css 'td:nth-child(3)'
    end

Valery Sizov's avatar
Valery Sizov committed
174
    it "allows to generate only right match line for parallel view" do
175 176 177 178 179 180 181 182
      output = diff_match_line nil, new_pos, text: text, view: :parallel

      expect(output).to be_html_safe
      expect(output).to have_css "td:nth-child(1):not(.js-unfold-bottom).diff-line-num.unfold.js-unfold.new_line[data-linenumber='#{new_pos}']", text: '...'
      expect(output).to have_css 'td:nth-child(2).line_content.match.parallel', text: text
      expect(output).not_to have_css 'td:nth-child(3)'
    end
  end
Marin Jankovski's avatar
Marin Jankovski committed
183
end