1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00
kaminari--kaminari/spec/models/array_spec.rb
Akira Matsuda e04f65aeaa refs #47, #68, #74 generic pagination support for Array object
this enables you to paginate through any Arrayish object in this way:
  Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)
2011-04-22 11:43:08 +09:00

105 lines
2.4 KiB
Ruby

require File.expand_path('../spec_helper', File.dirname(__FILE__))
describe Kaminari::PaginatableArray do
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 }
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 }
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
end
describe '#num_pages' do
context 'per 25 (default)' do
subject { array.page }
its(:num_pages) { should == 4 }
end
context 'per 7' do
subject { array.page(2).per(7) }
its(:num_pages) { should == 15 }
end
context 'per 65536' do
subject { array.page(50).per(65536) }
its(:num_pages) { should == 1 }
end
context 'per 0 (using default)' do
subject { array.page(50).per(0) }
its(:num_pages) { should == 4 }
end
context 'per -1 (using default)' do
subject { array.page(5).per(-1) }
its(:num_pages) { should == 4 }
end
context 'per "String value that can not be converted into Number" (using default)' do
subject { array.page(5).per('aho') }
its(:num_pages) { should == 4 }
end
end
describe '#current_page' do
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
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
end