summaryrefslogtreecommitdiff
path: root/spec/services/projects/blame_service_spec.rb
blob: 54c4315d2429cac3de00d5c25fe1f778763c4e0d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::BlameService, :aggregate_failures do
  subject(:service) { described_class.new(blob, commit, params) }

  let_it_be(:project) { create(:project, :repository) }
  let_it_be(:commit) { project.repository.commit }
  let_it_be(:blob) { project.repository.blob_at('HEAD', 'README.md') }

  let(:params) { { page: page } }
  let(:page) { nil }

  before do
    stub_const("#{described_class.name}::PER_PAGE", 2)
  end

  describe '#blame' do
    subject { service.blame }

    it 'returns a correct Gitlab::Blame object' do
      is_expected.to be_kind_of(Gitlab::Blame)

      expect(subject.blob).to eq(blob)
      expect(subject.commit).to eq(commit)
      expect(subject.range).to eq(1..2)
    end

    describe 'Pagination range calculation' do
      subject { service.blame.range }

      context 'with page = 1' do
        let(:page) { 1 }

        it { is_expected.to eq(1..2) }
      end

      context 'with page = 2' do
        let(:page) { 2 }

        it { is_expected.to eq(3..4) }
      end

      context 'with page = 3 (overlimit)' do
        let(:page) { 3 }

        it { is_expected.to eq(1..2) }
      end

      context 'with page = 0 (incorrect)' do
        let(:page) { 0 }

        it { is_expected.to eq(1..2) }
      end

      context 'when feature flag disabled' do
        before do
          stub_feature_flags(blame_page_pagination: false)
        end

        it { is_expected.to be_nil }
      end
    end
  end

  describe '#pagination' do
    subject { service.pagination }

    it 'returns a pagination object' do
      is_expected.to be_kind_of(Kaminari::PaginatableArray)

      expect(subject.current_page).to eq(1)
      expect(subject.total_pages).to eq(2)
      expect(subject.total_count).to eq(4)
    end

    context 'when feature flag disabled' do
      before do
        stub_feature_flags(blame_page_pagination: false)
      end

      it { is_expected.to be_nil }
    end

    context 'when per_page is above the global max per page limit' do
      before do
        stub_const("#{described_class.name}::PER_PAGE", 1000)
        allow(blob).to receive_message_chain(:data, :lines, :count) { 500 }
      end

      it 'returns a correct pagination object' do
        is_expected.to be_kind_of(Kaminari::PaginatableArray)

        expect(subject.current_page).to eq(1)
        expect(subject.total_pages).to eq(1)
        expect(subject.total_count).to eq(500)
      end
    end

    describe 'Pagination attributes' do
      using RSpec::Parameterized::TableSyntax

      where(:page, :current_page, :total_pages) do
        1 | 1 | 2
        2 | 2 | 2
        3 | 1 | 2 # Overlimit
        0 | 1 | 2 # Incorrect
      end

      with_them do
        it 'returns the correct pagination attributes' do
          expect(subject.current_page).to eq(current_page)
          expect(subject.total_pages).to eq(total_pages)
        end
      end
    end
  end
end