summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/serializer/pagination_spec.rb
blob: 43d9782c3b76e794b3a525ef3ec48ff1226f2e69 (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
require 'spec_helper'

describe Gitlab::Serializer::Pagination do
  let(:request) { spy('request') }
  let(:response) { spy('response') }

  before do
    allow(request)
      .to receive(:query_parameters)
      .and_return(params)
  end

  let(:pagination) { described_class.new(request, response) }

  describe '#paginate' do
    subject { pagination.paginate(resource) }

    let(:resource) { User.all }
    let(:params) { { page: 1, per_page: 2 } }

    context 'when a multiple resources are present in relation' do
      before { create_list(:user, 3) }

      it 'correctly paginates the resource' do
        expect(subject.count).to be 2
      end

      it 'appends relevant headers' do
        expect(response).to receive(:[]=).with('X-Total', '3')
        expect(response).to receive(:[]=).with('X-Total-Pages', '2')
        expect(response).to receive(:[]=).with('X-Per-Page', '2')

        subject
      end
    end

    context 'when an invalid resource is about to be paginated' do
      let(:resource) { create(:user) }

      it 'raises error' do
        expect { subject }.to raise_error(
          described_class::InvalidResourceError)
      end
    end
  end
end