2011-12-10 11:15:18 -05:00
|
|
|
require 'spec_helper'
|
2011-04-21 13:57:12 -04:00
|
|
|
|
|
|
|
describe Kaminari::PaginatableArray do
|
2011-09-21 01:42:50 -04:00
|
|
|
it { should have(0).items }
|
|
|
|
|
2011-08-25 19:23:51 -04:00
|
|
|
context 'specifying limit and offset when initializing' do
|
|
|
|
subject { Kaminari::PaginatableArray.new((1..100).to_a, :limit => 10, :offset => 20) }
|
|
|
|
its(:current_page) { should == 3 }
|
|
|
|
end
|
|
|
|
|
2011-04-21 13:57:12 -04:00
|
|
|
let(:array) { Kaminari::PaginatableArray.new((1..100).to_a) }
|
|
|
|
describe '#page' do
|
|
|
|
shared_examples_for 'the first page of array' do
|
|
|
|
it { should have(25).users }
|
2011-08-17 07:58:20 -04:00
|
|
|
its(:current_page) { should == 1 }
|
2011-04-21 13:57:12 -04:00
|
|
|
its(:first) { should == 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for 'blank array page' do
|
|
|
|
it { should have(0).items }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 1' do
|
|
|
|
subject { array.page 1 }
|
|
|
|
it_should_behave_like 'the first page of array'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { array.page 2 }
|
|
|
|
it { should have(25).users }
|
2011-08-17 07:58:20 -04:00
|
|
|
its(:current_page) { should == 2 }
|
2011-04-21 13:57:12 -04:00
|
|
|
its(:first) { should == 26 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page without an argument' do
|
|
|
|
subject { array.page }
|
|
|
|
it_should_behave_like 'the first page of array'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page < 1' do
|
|
|
|
subject { array.page 0 }
|
|
|
|
it_should_behave_like 'the first page of array'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page > max page' do
|
|
|
|
subject { array.page 5 }
|
|
|
|
it_should_behave_like 'blank array page'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#per' do
|
|
|
|
context 'page 1 per 5' do
|
|
|
|
subject { array.page(1).per(5) }
|
|
|
|
it { should have(5).users }
|
|
|
|
its(:first) { should == 1 }
|
|
|
|
end
|
2014-08-24 20:29:21 -04:00
|
|
|
|
|
|
|
context "page 1 per 0" do
|
|
|
|
subject { array.page(1).per(0) }
|
|
|
|
it { should have(0).users }
|
|
|
|
end
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
2012-05-25 02:31:53 -04:00
|
|
|
describe '#total_pages' do
|
2011-04-21 13:57:12 -04:00
|
|
|
context 'per 25 (default)' do
|
|
|
|
subject { array.page }
|
2012-05-25 02:31:53 -04:00
|
|
|
its(:total_pages) { should == 4 }
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per 7' do
|
|
|
|
subject { array.page(2).per(7) }
|
2012-05-25 02:31:53 -04:00
|
|
|
its(:total_pages) { should == 15 }
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per 65536' do
|
|
|
|
subject { array.page(50).per(65536) }
|
2012-05-25 02:31:53 -04:00
|
|
|
its(:total_pages) { should == 1 }
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
2014-08-24 20:29:21 -04:00
|
|
|
context 'per 0' do
|
2011-04-21 13:57:12 -04:00
|
|
|
subject { array.page(50).per(0) }
|
2014-08-24 20:29:21 -04:00
|
|
|
it "raises Kaminari::ZeroPerPageOperation" do
|
|
|
|
expect { subject.total_pages }.to raise_error(Kaminari::ZeroPerPageOperation)
|
|
|
|
end
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per -1 (using default)' do
|
|
|
|
subject { array.page(5).per(-1) }
|
2012-05-25 02:31:53 -04:00
|
|
|
its(:total_pages) { should == 4 }
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'per "String value that can not be converted into Number" (using default)' do
|
|
|
|
subject { array.page(5).per('aho') }
|
2012-05-25 02:31:53 -04:00
|
|
|
its(:total_pages) { should == 4 }
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
2013-07-02 09:20:53 -04:00
|
|
|
|
|
|
|
context 'per 25, padding 25' do
|
|
|
|
subject { array.page(1).padding(25) }
|
|
|
|
its(:total_pages) { should == 3 }
|
|
|
|
end
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#current_page' do
|
2014-08-24 20:29:21 -04:00
|
|
|
context 'any page, per 0' do
|
|
|
|
subject { array.page.per(0) }
|
|
|
|
it "raises Kaminari::ZeroPerPageOperation" do
|
|
|
|
expect { subject.current_page }.to raise_error(Kaminari::ZeroPerPageOperation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-21 13:57:12 -04:00
|
|
|
context 'page 1' do
|
|
|
|
subject { array.page }
|
|
|
|
its(:current_page) { should == 1 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { array.page(2).per 3 }
|
|
|
|
its(:current_page) { should == 2 }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-04 05:16:48 -05:00
|
|
|
describe '#next_page' do
|
|
|
|
context 'page 1' do
|
|
|
|
subject { array.page }
|
|
|
|
its(:next_page) { should == 2 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 5' do
|
|
|
|
subject { array.page 5 }
|
|
|
|
its(:next_page) { should be_nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#prev_page' do
|
|
|
|
context 'page 1' do
|
|
|
|
subject { array.page }
|
|
|
|
its(:prev_page) { should be_nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 5' do
|
|
|
|
subject { array.page 5 }
|
|
|
|
its(:prev_page) { should == 4 }
|
|
|
|
end
|
2014-09-13 23:41:30 -04:00
|
|
|
|
|
|
|
context 'page 6' do
|
|
|
|
subject { array.page 6 }
|
|
|
|
its(:prev_page) { should be_nil }
|
|
|
|
end
|
2012-12-04 05:16:48 -05:00
|
|
|
end
|
|
|
|
|
2011-04-21 13:57:12 -04:00
|
|
|
describe '#count' do
|
|
|
|
context 'page 1' do
|
|
|
|
subject { array.page }
|
|
|
|
its(:count) { should == 25 }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'page 2' do
|
|
|
|
subject { array.page 2 }
|
|
|
|
its(:count) { should == 25 }
|
|
|
|
end
|
|
|
|
end
|
2011-08-24 23:13:46 -04:00
|
|
|
|
|
|
|
context 'when setting total count explicitly' do
|
2014-06-24 21:19:21 -04:00
|
|
|
context "array 1..10, page 5, per 10, total_count 9999" do
|
2014-05-06 23:04:16 -04:00
|
|
|
subject { Kaminari::PaginatableArray.new((1..10).to_a, :total_count => 9999).page(5).per(10) }
|
|
|
|
|
2014-06-24 21:19:21 -04:00
|
|
|
it { should have(10).items }
|
|
|
|
its(:first) { should == 1 }
|
|
|
|
its(:current_page) { should == 5 }
|
2014-05-06 23:04:16 -04:00
|
|
|
its(:total_count) { should == 9999 }
|
|
|
|
end
|
|
|
|
|
2014-06-24 21:19:21 -04:00
|
|
|
context "array 1..15, page 1, per 10, total_count 15" do
|
2014-05-06 23:34:23 -04:00
|
|
|
subject { Kaminari.paginate_array((1..15).to_a, :total_count => 15).page(1).per(10) }
|
2014-05-06 23:04:16 -04:00
|
|
|
|
|
|
|
it { should have(10).items }
|
|
|
|
its(:first) { should == 1 }
|
2014-06-24 21:19:21 -04:00
|
|
|
its(:current_page) { should == 1 }
|
2014-05-06 23:04:16 -04:00
|
|
|
its(:total_count) { should == 15 }
|
|
|
|
end
|
|
|
|
|
2014-06-24 21:19:21 -04:00
|
|
|
context "array 1..25, page 2, per 10, total_count 15" do
|
2014-05-06 23:34:23 -04:00
|
|
|
subject { Kaminari.paginate_array((1..25).to_a, :total_count => 15).page(2).per(10) }
|
2014-05-06 23:04:16 -04:00
|
|
|
|
|
|
|
it { should have(5).items }
|
|
|
|
its(:first) { should == 11 }
|
2014-06-24 21:19:21 -04:00
|
|
|
its(:current_page) { should == 2 }
|
2014-05-06 23:04:16 -04:00
|
|
|
its(:total_count) { should == 15 }
|
|
|
|
end
|
2011-08-24 23:13:46 -04:00
|
|
|
end
|
2011-04-21 13:57:12 -04:00
|
|
|
end
|