expand_collapse_diffs_spec.rb 10.2 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Expand and collapse diffs', :js do
4
  let(:branch) { 'expand-collapse-diffs' }
5
  let(:project) { create(:project, :repository) }
6

7
  before do
8 9 10 11 12
    # Set the limits to those when these specs were written, to avoid having to
    # update the test repo every time we change them.
    allow(Gitlab::Git::Diff).to receive(:size_limit).and_return(100.kilobytes)
    allow(Gitlab::Git::Diff).to receive(:collapse_limit).and_return(10.kilobytes)

13
    sign_in(create(:admin))
14 15

    # Ensure that undiffable.md is in .gitattributes
Sean McGivern's avatar
Sean McGivern committed
16
    project.repository.copy_gitattributes(branch)
17
    visit project_commit_path(project, project.commit(branch))
18 19 20 21 22 23 24 25 26 27
    execute_script('window.ajaxUris = []; $(document).ajaxSend(function(event, xhr, settings) { ajaxUris.push(settings.url) });')
  end

  def file_container(filename)
    find("[data-blob-diff-path*='#{filename}']")
  end

  # Use define_method instead of let (which is memoized) so that this just works across a
  # reload.
  #
28 29 30 31 32 33
  files = [
    'small_diff.md', 'large_diff.md', 'large_diff_renamed.md', 'undiffable.md',
    'too_large.md', 'too_large_image.jpg'
  ]

  files.each do |file|
34 35 36
    define_method(file.split('.').first) { file_container(file) }
  end

37 38 39 40
  it 'should show the diff content with a highlighted line when linking to line' do
    expect(large_diff).not_to have_selector('.code')
    expect(large_diff).to have_selector('.nothing-here-block')

41
    visit project_commit_path(project, project.commit(branch), anchor: "#{large_diff[:id]}_0_1")
42 43
    execute_script('window.location.reload()')

44
    wait_for_requests
45 46 47 48 49 50 51 52 53 54

    expect(large_diff).to have_selector('.code')
    expect(large_diff).not_to have_selector('.nothing-here-block')
    expect(large_diff).to have_selector('.hll')
  end

  it 'should show the diff content when linking to file' do
    expect(large_diff).not_to have_selector('.code')
    expect(large_diff).to have_selector('.nothing-here-block')

55
    visit project_commit_path(project, project.commit(branch), anchor: large_diff[:id])
56 57
    execute_script('window.location.reload()')

58
    wait_for_requests
59 60 61 62 63

    expect(large_diff).to have_selector('.code')
    expect(large_diff).not_to have_selector('.nothing-here-block')
  end

Sean McGivern's avatar
Sean McGivern committed
64
  context 'visiting a commit with collapsed diffs' do
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    it 'shows small diffs immediately' do
      expect(small_diff).to have_selector('.code')
      expect(small_diff).not_to have_selector('.nothing-here-block')
    end

    it 'shows non-renderable diffs as such immediately, regardless of their size' do
      expect(undiffable).not_to have_selector('.code')
      expect(undiffable).to have_selector('.nothing-here-block')
      expect(undiffable).to have_content('gitattributes')
    end

    it 'does not allow diffs that are larger than the maximum size to be expanded' do
      expect(too_large).not_to have_selector('.code')
      expect(too_large).to have_selector('.nothing-here-block')
      expect(too_large).to have_content('too large')
    end

    it 'shows image diffs immediately, regardless of their size' do
      expect(too_large_image).not_to have_selector('.nothing-here-block')
      expect(too_large_image).to have_selector('.image')
    end

87 88
    context 'expanding a diff for a renamed file' do
      before do
89
        large_diff_renamed.find('.click-to-expand').click
90
        wait_for_requests
91 92 93 94 95
      end

      it 'shows the old content' do
        old_line = large_diff_renamed.find('.line_content.old')

Sean McGivern's avatar
Sean McGivern committed
96
        expect(old_line).to have_content('two copies')
97 98 99 100 101
      end

      it 'shows the new content' do
        new_line = large_diff_renamed.find('.line_content.new', match: :prefer_exact)

Sean McGivern's avatar
Sean McGivern committed
102
        expect(new_line).to have_content('three copies')
103 104 105
      end
    end

106 107
    context 'expanding a large diff' do
      before do
108
        # Wait for diffs
109
        find('.js-file-title', match: :first)
110
        # Click `large_diff.md` title
111
        all('.diff-toggle-caret')[1].click
112
        wait_for_requests
113 114 115 116 117 118 119 120 121 122 123
      end

      it 'shows the diff content' do
        expect(large_diff).to have_selector('.code')
        expect(large_diff).not_to have_selector('.nothing-here-block')
      end

      context 'adding a comment to the expanded diff' do
        let(:comment_text) { 'A comment' }

        before do
Sean McGivern's avatar
Sean McGivern committed
124
          large_diff.find('.diff-line-num', match: :prefer_exact).hover
125
          large_diff.find('.add-diff-note', match: :prefer_exact).click
126 127
          large_diff.find('.note-textarea').send_keys comment_text
          large_diff.find_button('Comment').click
128
          wait_for_requests
129 130 131 132 133 134 135
        end

        it 'adds the comment' do
          expect(large_diff.find('.notes')).to have_content comment_text
        end

        context 'reloading the page' do
136 137 138
          before do
            refresh
          end
139 140 141 142 143 144 145 146

          it 'collapses the large diff by default' do
            expect(large_diff).not_to have_selector('.code')
            expect(large_diff).to have_selector('.nothing-here-block')
          end

          context 'expanding the diff' do
            before do
147
              # Wait for diffs
148
              find('.js-file-title', match: :first)
149
              # Click `large_diff.md` title
150
              all('.diff-toggle-caret')[1].click
151
              wait_for_requests
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            end

            it 'shows the diff content' do
              expect(large_diff).to have_selector('.code')
              expect(large_diff).not_to have_selector('.nothing-here-block')
            end

            it 'shows the diff comment' do
              expect(large_diff.find('.notes')).to have_content comment_text
            end
          end
        end
      end
    end

    context 'collapsing an expanded diff' do
168 169
      before do
        # Wait for diffs
170
        find('.js-file-title', match: :first)
171
        # Click `small_diff.md` title
172
        all('.diff-toggle-caret')[3].click
173
      end
174 175 176 177 178 179 180

      it 'hides the diff content' do
        expect(small_diff).not_to have_selector('.code')
        expect(small_diff).to have_selector('.nothing-here-block')
      end

      context 're-expanding the same diff' do
181 182
        before do
          # Wait for diffs
183
          find('.js-file-title', match: :first)
184
          # Click `small_diff.md` title
185
          all('.diff-toggle-caret')[3].click
186
        end
187 188 189 190 191 192 193

        it 'shows the diff content' do
          expect(small_diff).to have_selector('.code')
          expect(small_diff).not_to have_selector('.nothing-here-block')
        end

        it 'does not make a new HTTP request' do
Sean McGivern's avatar
Sean McGivern committed
194
          expect(evaluate_script('ajaxUris')).not_to include(a_string_matching('small_diff.md'))
195 196 197
        end
      end
    end
198 199 200 201 202 203 204 205 206

    context 'expanding a diff when symlink was converted to a regular file' do
      let(:branch) { 'symlink-expand-diff' }

      it 'shows the content of the regular file' do
        expect(page).to have_content('This diff is collapsed')
        expect(page).to have_no_content('No longer a symlink')

        find('.click-to-expand').click
207
        wait_for_requests
208 209 210 211

        expect(page).to have_content('No longer a symlink')
      end
    end
212
  end
213

214 215 216 217 218 219 220 221 222 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
  context 'visiting a commit without collapsed diffs' do
    let(:branch) { 'feature' }

    it 'does not show Expand all button' do
      expect(page).not_to have_link('Expand all')
    end
  end

  context 'visiting a commit with more than safe files' do
    let(:branch) { 'expand-collapse-files' }

    # safe-files -> 100 | safe-lines -> 5000 | commit-files -> 105
    it 'does collapsing from the safe number of files to the end on small files' do
      expect(page).to have_link('Expand all')

      expect(page).to have_selector('.diff-content', count: 105)
      expect(page).to have_selector('.diff-collapsed', count: 5)

      %w(file-95.txt file-96.txt file-97.txt file-98.txt file-99.txt).each do |filename|
        expect(find("[data-blob-diff-path*='#{filename}']")).to have_selector('.diff-collapsed')
      end
    end
  end

  context 'visiting a commit with more than safe lines' do
    let(:branch) { 'expand-collapse-lines' }

    # safe-files -> 100 | safe-lines -> 5000 | commit_files -> 8 (each 1250 lines)
    it 'does collapsing from the safe number of lines to the end' do
      expect(page).to have_link('Expand all')

      expect(page).to have_selector('.diff-content', count: 6)
      expect(page).to have_selector('.diff-collapsed', count: 2)

      %w(file-4.txt file-5.txt).each do |filename|
        expect(find("[data-blob-diff-path*='#{filename}']")).to have_selector('.diff-collapsed')
      end
    end
  end

254 255 256
  context 'expanding all diffs' do
    before do
      click_link('Expand all')
257 258 259

      # Wait for elements to appear to ensure full page reload
      expect(page).to have_content('This diff was suppressed by a .gitattributes entry')
Douwe Maan's avatar
Douwe Maan committed
260
      expect(page).to have_content('This source diff could not be displayed because it is too large.')
261 262 263
      expect(page).to have_content('too_large_image.jpg')
      find('.note-textarea')

264
      wait_for_requests
265 266 267 268 269 270 271 272 273 274 275 276
      execute_script('window.ajaxUris = []; $(document).ajaxSend(function(event, xhr, settings) { ajaxUris.push(settings.url) });')
    end

    it 'reloads the page with all diffs expanded' do
      expect(small_diff).to have_selector('.code')
      expect(small_diff).not_to have_selector('.nothing-here-block')

      expect(large_diff).to have_selector('.code')
      expect(large_diff).not_to have_selector('.nothing-here-block')
    end

    context 'collapsing an expanded diff' do
277 278
      before do
        # Wait for diffs
279
        find('.js-file-title', match: :first)
280
        # Click `small_diff.md` title
281
        all('.diff-toggle-caret')[3].click
282
      end
283 284 285 286 287 288 289

      it 'hides the diff content' do
        expect(small_diff).not_to have_selector('.code')
        expect(small_diff).to have_selector('.nothing-here-block')
      end

      context 're-expanding the same diff' do
290 291
        before do
          # Wait for diffs
292
          find('.js-file-title', match: :first)
293
          # Click `small_diff.md` title
294
          all('.diff-toggle-caret')[3].click
295
        end
296 297 298 299 300 301 302

        it 'shows the diff content' do
          expect(small_diff).to have_selector('.code')
          expect(small_diff).not_to have_selector('.nothing-here-block')
        end

        it 'does not make a new HTTP request' do
Sean McGivern's avatar
Sean McGivern committed
303
          expect(evaluate_script('ajaxUris')).not_to include(a_string_matching('small_diff.md'))
304 305 306 307
        end
      end
    end
  end
308
end