Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_file_entity_spec.rb « serializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 48099cb1fdf9f8eb80e63cc8cdbdf6aea238f980 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe DiffFileEntity do
  include RepoHelpers

  let_it_be(:project) { create(:project, :repository) }

  let(:repository) { project.repository }
  let(:commit) { project.commit(sample_commit.id) }
  let(:diff_refs) { commit.diff_refs }
  let(:diff) { commit.raw_diffs.first }
  let(:diff_file) { Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
  let(:options) { {} }
  let(:entity) { described_class.new(diff_file, options.reverse_merge(request: {})) }

  subject { entity.as_json }

  context 'when there is no merge request' do
    it_behaves_like 'diff file entity'
  end

  context 'when there is a merge request' do
    let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project) }

    let(:user) { create(:user) }
    let(:code_navigation_path) { Gitlab::CodeNavigationPath.new(project, project.commit.sha) }
    let(:request) { EntityRequest.new(project: project, current_user: user) }
    let(:entity) { described_class.new(diff_file, options.merge(request: request, merge_request: merge_request, code_navigation_path: code_navigation_path)) }
    let(:exposed_urls) { %i(edit_path view_path context_lines_path) }

    it_behaves_like 'diff file entity'

    it 'exposes additional attributes' do
      expect(subject).to include(*exposed_urls)
      expect(subject).to include(:replaced_view_path)
      expect(subject).to include(:code_navigation_path)
    end

    it 'points all urls to merge request target project' do
      response = subject

      exposed_urls.each do |attribute|
        expect(response[attribute]).to include(merge_request.target_project.to_param)
      end
    end

    it 'exposes load_collapsed_diff_url if the file viewer is collapsed' do
      allow(diff_file.viewer).to receive(:collapsed?) { true }

      expect(subject).to include(:load_collapsed_diff_url)
    end

    context 'when diff_view is unknown' do
      let(:options) { { diff_view: :unknown } }

      it 'hides highlighted_diff_lines and parallel_diff_lines' do
        is_expected.not_to include(:highlighted_diff_lines, :parallel_diff_lines)
      end
    end
  end

  describe '#parallel_diff_lines' do
    let(:options) { { diff_view: :parallel } }

    it 'exposes parallel diff lines correctly' do
      response = subject

      lines = response[:parallel_diff_lines]

      # make sure at least one line is present for each side
      expect(lines.map { |line| line[:right] }.compact).to be_present
      expect(lines.map { |line| line[:left] }.compact).to be_present
      # make sure all lines are in correct format
      lines.each do |parallel_line|
        expect(parallel_line[:left].as_json).to match_schema('entities/diff_line') if parallel_line[:left]
        expect(parallel_line[:right].as_json).to match_schema('entities/diff_line') if parallel_line[:right]
      end
    end
  end

  describe '#is_fully_expanded' do
    context 'file with a conflict' do
      let(:options) { { conflicts: { diff_file.new_path => double(diff_lines_for_serializer: [], conflict_type: :both_modified) } } }

      it 'returns false' do
        expect(diff_file).not_to receive(:fully_expanded?)
        expect(subject[:is_fully_expanded]).to eq(false)
      end
    end
  end

  describe '#highlighted_diff_lines' do
    context 'file without a conflict' do
      let(:options) { { conflicts: {} } }

      it 'calls diff_lines_for_serializer on diff_file' do
        # #diff_lines_for_serializer gets called in #fully_expanded? as well so we expect twice
        expect(diff_file).to receive(:diff_lines_for_serializer).twice.and_return([])
        expect(subject[:highlighted_diff_lines]).to eq([])
      end
    end

    context 'file with a conflict' do
      let(:conflict_file) { instance_double(Gitlab::Conflict::File, conflict_type: :both_modified) }
      let(:options) { { conflicts: { diff_file.new_path => conflict_file } } }

      it 'calls diff_lines_for_serializer on matching conflict file' do
        expect(conflict_file).to receive(:diff_lines_for_serializer).and_return([])
        expect(subject[:highlighted_diff_lines]).to eq([])
      end

      context 'when Gitlab::Git::Conflict::Parser::UnmergeableFile gets raised' do
        before do
          allow(conflict_file).to receive(:diff_lines_for_serializer).and_raise(Gitlab::Git::Conflict::Parser::UnmergeableFile)
        end

        it 'falls back to diff_file diff_lines_for_serializer' do
          expect(diff_file).to receive(:diff_lines_for_serializer).and_return([])
          expect(subject[:highlighted_diff_lines]).to eq([])
        end
      end
    end
  end

  it_behaves_like 'diff file with conflict_type'
end