2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Serializer::Pagination do
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:request) { double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:response) { spy('response') }
|
|
|
|
let(:headers) { spy('headers') }
|
|
|
|
|
|
|
|
before do
|
2019-07-07 11:18:12 +05:30
|
|
|
allow(response).to receive(:headers).and_return(headers)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:pagination) { described_class.new(request, response) }
|
|
|
|
|
|
|
|
describe '#paginate' do
|
|
|
|
subject { pagination.paginate(resource) }
|
|
|
|
|
|
|
|
let(:resource) { User.all }
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:query) { { page: 1, per_page: 2 } }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'when a multiple resources are present in relation' do
|
2017-09-10 17:25:29 +05:30
|
|
|
before do
|
|
|
|
create_list(:user, 3)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'correctly paginates the resource' do
|
|
|
|
expect(subject.count).to be 2
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'appends relevant headers' do
|
|
|
|
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')
|
|
|
|
|
|
|
|
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
|