2017-02-06 14:07:13 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Serializer::Pagination do
|
|
|
|
let(:request) { spy('request') }
|
|
|
|
let(:response) { spy('response') }
|
2017-02-07 12:15:58 +00:00
|
|
|
let(:headers) { spy('headers') }
|
2017-02-06 14:07:13 +00:00
|
|
|
|
|
|
|
before do
|
2017-02-07 12:15:58 +00:00
|
|
|
allow(request).to receive(:query_parameters)
|
2017-02-06 14:07:13 +00:00
|
|
|
.and_return(params)
|
2017-02-07 12:15:58 +00:00
|
|
|
|
|
|
|
allow(response).to receive(:headers)
|
|
|
|
.and_return(headers)
|
2017-02-06 14:07:13 +00:00
|
|
|
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
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
create_list(:user, 3)
|
|
|
|
end
|
2017-02-06 14:07:13 +00:00
|
|
|
|
|
|
|
it 'correctly paginates the resource' do
|
|
|
|
expect(subject.count).to be 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends relevant headers' do
|
2017-02-07 12:15:58 +00:00
|
|
|
expect(headers).to receive(:[]=).with('X-Total', '3')
|
|
|
|
expect(headers).to receive(:[]=).with('X-Total-Pages', '2')
|
|
|
|
expect(headers).to receive(:[]=).with('X-Per-Page', '2')
|
2017-02-06 14:07:13 +00:00
|
|
|
|
|
|
|
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
|