summaryrefslogtreecommitdiff
path: root/spec/lib/api/helpers/pagination_strategies_spec.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-10 15:07:47 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-10 15:07:47 +0000
commit8b1228b0d409d7751f01d9fb72ebfbbf62399486 (patch)
tree1b4126fe48d7666a90c0d7ee26230cf8379b6410 /spec/lib/api/helpers/pagination_strategies_spec.rb
parent96b0c1245c93585a8b0fe23e22306d32ff4e4905 (diff)
downloadgitlab-ce-8b1228b0d409d7751f01d9fb72ebfbbf62399486.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/api/helpers/pagination_strategies_spec.rb')
-rw-r--r--spec/lib/api/helpers/pagination_strategies_spec.rb97
1 files changed, 97 insertions, 0 deletions
diff --git a/spec/lib/api/helpers/pagination_strategies_spec.rb b/spec/lib/api/helpers/pagination_strategies_spec.rb
new file mode 100644
index 00000000000..a418c09a824
--- /dev/null
+++ b/spec/lib/api/helpers/pagination_strategies_spec.rb
@@ -0,0 +1,97 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe API::Helpers::PaginationStrategies do
+ subject { Class.new.include(described_class).new }
+
+ let(:expected_result) { double("result") }
+ let(:relation) { double("relation") }
+ let(:params) { {} }
+
+ before do
+ allow(subject).to receive(:params).and_return(params)
+ end
+
+ describe '#paginate_with_strategies' do
+ let(:paginator) { double("paginator", paginate: expected_result, finalize: nil) }
+
+ before do
+ allow(subject).to receive(:paginator).with(relation).and_return(paginator)
+ end
+
+ it 'yields paginated relation' do
+ expect { |b| subject.paginate_with_strategies(relation, &b) }.to yield_with_args(expected_result)
+ end
+
+ it 'calls #finalize with first value returned from block' do
+ return_value = double
+ expect(paginator).to receive(:finalize).with(return_value)
+
+ subject.paginate_with_strategies(relation) do |records|
+ some_options = {}
+ [return_value, some_options]
+ end
+ end
+
+ it 'returns whatever the block returns' do
+ return_value = [double, double]
+
+ result = subject.paginate_with_strategies(relation) do |records|
+ return_value
+ end
+
+ expect(result).to eq(return_value)
+ end
+ end
+
+ describe '#paginator' do
+ context 'offset pagination' do
+ let(:paginator) { double("paginator") }
+
+ before do
+ allow(subject).to receive(:keyset_pagination_enabled?).and_return(false)
+ end
+
+ it 'delegates to OffsetPagination' do
+ expect(Gitlab::Pagination::OffsetPagination).to receive(:new).with(subject).and_return(paginator)
+
+ expect(subject.paginator(relation)).to eq(paginator)
+ end
+ end
+
+ context 'for keyset pagination' do
+ let(:params) { { pagination: 'keyset' } }
+ let(:request_context) { double('request context') }
+ let(:pager) { double('pager') }
+
+ before do
+ allow(subject).to receive(:keyset_pagination_enabled?).and_return(true)
+ allow(Gitlab::Pagination::Keyset::RequestContext).to receive(:new).with(subject).and_return(request_context)
+ end
+
+ context 'when keyset pagination is available' do
+ before do
+ allow(Gitlab::Pagination::Keyset).to receive(:available?).and_return(true)
+ allow(Gitlab::Pagination::Keyset::Pager).to receive(:new).with(request_context).and_return(pager)
+ end
+
+ it 'delegates to Pager' do
+ expect(subject.paginator(relation)).to eq(pager)
+ end
+ end
+
+ context 'when keyset pagination is not available' do
+ before do
+ allow(Gitlab::Pagination::Keyset).to receive(:available?).with(request_context, relation).and_return(false)
+ end
+
+ it 'renders a 501 error' do
+ expect(subject).to receive(:error!).with(/not yet available/, 405)
+
+ subject.paginator(relation)
+ end
+ end
+ end
+ end
+end