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

cursor_pager_spec.rb « keyset « pagination « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 783e728b34c02991b9f3c66b8dc2efe0cdccdbab (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Pagination::Keyset::CursorPager do
  let(:relation) { Group.all.order(:name, :id) }
  let(:per_page) { 3 }
  let(:params) { { cursor: nil, per_page: per_page } }
  let(:request_context) { double('request_context', params: params) }
  let(:cursor_based_request_context) { Gitlab::Pagination::Keyset::CursorBasedRequestContext.new(request_context) }

  before_all do
    create_list(:group, 7)
  end

  describe '#paginate' do
    subject(:paginated_result) { described_class.new(cursor_based_request_context).paginate(relation) }

    it 'returns the limited relation' do
      expect(paginated_result).to eq(relation.limit(per_page))
    end
  end

  describe '#finalize' do
    subject(:finalize) do
      service = described_class.new(cursor_based_request_context)
      # we need to do this because `finalize` can only be called
      # after `paginate` is called. Otherwise the `paginator` object won't be set.
      service.paginate(relation)
      service.finalize
    end

    it 'passes information about next page to request' do
      cursor_for_next_page = relation.keyset_paginate(**params).cursor_for_next_page

      expect_next_instance_of(Gitlab::Pagination::Keyset::HeaderBuilder, request_context) do |builder|
        expect(builder).to receive(:add_next_page_header).with({ cursor: cursor_for_next_page })
      end

      finalize
    end

    context 'when retrieving the last page' do
      let(:relation) { Group.where('id > ?', Group.maximum(:id) - per_page).order(:name, :id) }

      it 'does not build information about the next page' do
        expect(Gitlab::Pagination::Keyset::HeaderBuilder).not_to receive(:new)

        finalize
      end
    end

    context 'when retrieving an empty page' do
      let(:relation) { Group.where('id > ?', Group.maximum(:id) + 1).order(:name, :id) }

      it 'does not build information about the next page' do
        expect(Gitlab::Pagination::Keyset::HeaderBuilder).not_to receive(:new)

        finalize
      end
    end
  end
end