summaryrefslogtreecommitdiff
path: root/spec/lib
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-06 15:07:13 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-02-06 15:07:13 +0100
commita7420b77bd9b7038af3702d3665faab317048d3a (patch)
treeeb96b3ec5c303aa6ea2573122586b397b1207fa9 /spec/lib
parent81eb5c7fd79897d63e733e99b8fc4424dc8ea38a (diff)
downloadgitlab-ce-a7420b77bd9b7038af3702d3665faab317048d3a.tar.gz
Add specs for standalone pagination for serializers
Diffstat (limited to 'spec/lib')
-rw-r--r--spec/lib/gitlab/serializer/pagination_spec.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/spec/lib/gitlab/serializer/pagination_spec.rb b/spec/lib/gitlab/serializer/pagination_spec.rb
new file mode 100644
index 00000000000..43d9782c3b7
--- /dev/null
+++ b/spec/lib/gitlab/serializer/pagination_spec.rb
@@ -0,0 +1,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