require 'spec_helper'
describe 'Kaminari::ActionViewExtension', :if => defined?(Rails) do
describe '#paginate' do
before do
50.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1)
end
subject { helper.paginate @users, :params => {:controller => 'users', :action => 'index'} }
it { should be_a String }
context 'escaping the pagination for javascript' do
it 'should escape for javascript' do
lambda { helper.escape_javascript(helper.paginate @users, :params => {:controller => 'users', :action => 'index'}) }.should_not raise_error
end
end
context 'accepts :theme option' do
before { helper.controller.append_view_path "spec/fake_app/views" }
subject { helper.paginate @users, :theme => "bootstrap", :params => {:controller => 'users', :action => 'index'} }
it { should match(/bootstrap-paginator/) }
it { should match(/bootstrap-page-link/) }
end
context 'accepts :view_prefix option' do
before { helper.controller.append_view_path "spec/fake_app/views" }
subject { helper.paginate @users, :views_prefix => "alternative/", :params => {:controller => 'users', :action => 'index'} }
it { should eq(" 1\n") }
end
end
describe '#link_to_previous_page' do
before do
60.times {|i| User.create! :name => "user#{i}"}
end
context 'having previous pages' do
before do
@users = User.page(3)
end
context 'the default behaviour' do
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
it { should match(/page=2/) }
it { should match(/rel="prev"/) }
end
context 'overriding rel=' do
subject { helper.link_to_previous_page @users, 'Previous', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
it { should match(/rel="external"/) }
end
context 'with params' do
before do
helper.params[:status] = "active"
end
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
it { should match(/status=active/) }
end
end
context 'the first page' do
before do
@users = User.page(1)
end
subject { helper.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'} }
it { should_not be }
end
context 'out of range' do
before { @users = User.page(5) }
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
it { should_not be }
end
end
describe '#link_to_next_page' do
before do
50.times {|i| User.create! :name => "user#{i}"}
end
context 'having more page' do
before do
@users = User.page(1)
end
context 'the default behaviour' do
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
it { should match(/page=2/) }
it { should match(/rel="next"/) }
end
context 'overriding rel=' do
subject { helper.link_to_next_page @users, 'More', :rel => 'external', :params => {:controller => 'users', :action => 'index'} }
it { should match(/rel="external"/) }
end
context 'with params' do
before do
helper.params[:status] = "active"
end
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
it { should match(/status=active/) }
end
end
context 'the last page' do
before do
@users = User.page(2)
end
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
it { should_not be }
end
context 'out of range' do
before { @users = User.page(5) }
subject { helper.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'} }
it { should_not be }
end
end
describe '#page_entries_info' do
context 'on a model without namespace' do
before do
@users = User.page(1).per(25)
end
context 'having no entries' do
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'No users found' }
context 'setting the entry name option to "member"' do
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
it { should == 'No members found' }
end
end
context 'having 1 entry' do
before do
User.create! :name => 'user1'
@users = User.page(1).per(25)
end
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying 1 user' }
context 'setting the entry name option to "member"' do
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying 1 member' }
end
end
context 'having more than 1 but less than a page of entries' do
before do
10.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1).per(25)
end
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying all 10 users' }
context 'setting the entry name option to "member"' do
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying all 10 members' }
end
end
context 'having more than one page of entries' do
before do
50.times {|i| User.create! :name => "user#{i}"}
end
describe 'the first page' do
before do
@users = User.page(1).per(25)
end
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying users 1 - 25 of 50 in total' }
context 'setting the entry name option to "member"' do
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying members 1 - 25 of 50 in total' }
end
end
describe 'the next page' do
before do
@users = User.page(2).per(25)
end
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying users 26 - 50 of 50 in total' }
context 'setting the entry name option to "member"' do
subject { helper.page_entries_info @users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying members 26 - 50 of 50 in total' }
end
end
end
end
context 'on a model with namespace' do
before do
@addresses = User::Address.page(1).per(25)
end
context 'having no entries' do
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'No addresses found' }
end
context 'having 1 entry' do
before do
User::Address.create!
@addresses = User::Address.page(1).per(25)
end
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying 1 address' }
context 'setting the entry name option to "place"' do
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying 1 place' }
end
end
context 'having more than 1 but less than a page of entries' do
before do
10.times {|i| User::Address.create!}
@addresses = User::Address.page(1).per(25)
end
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying all 10 addresses' }
context 'setting the entry name option to "place"' do
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying all 10 places' }
end
end
context 'having more than one page of entries' do
before do
50.times {|i| User::Address.create!}
end
describe 'the first page' do
before do
@addresses = User::Address.page(1).per(25)
end
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying addresses 1 - 25 of 50 in total' }
context 'setting the entry name option to "place"' do
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying places 1 - 25 of 50 in total' }
end
end
describe 'the next page' do
before do
@addresses = User::Address.page(2).per(25)
end
subject { helper.page_entries_info @addresses, :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying addresses 26 - 50 of 50 in total' }
context 'setting the entry name option to "place"' do
subject { helper.page_entries_info @addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'} }
it { should == 'Displaying places 26 - 50 of 50 in total' }
end
end
end
end
context 'on a PaginatableArray' do
before do
@numbers = Kaminari.paginate_array(%w{one two three}).page(1)
end
subject { helper.page_entries_info @numbers }
it { should == 'Displaying all 3 entries' }
end
end
describe '#rel_next_prev_link_tags' do
before do
31.times {|i| User.create! :name => "user#{i}"}
end
subject { helper.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'} }
context 'the first page' do
let(:users) { User.page(1).per(10) }
it { should_not match(/rel="prev"/) }
it { should match(/rel="next"/) }
it { should match(/\?page=2/) }
end
context 'the second page' do
let(:users) { User.page(2).per(10) }
it { should match(/rel="prev"/) }
it { should_not match(/\?page=1/) }
it { should match(/rel="next"/) }
it { should match(/\?page=3/) }
end
context 'the last page' do
let(:users) { User.page(4).per(10) }
it { should match(/rel="prev"/) }
it { should match(/\?page=3"/) }
it { should_not match(/rel="next"/) }
end
end
end