2011-12-10 11:15:18 -05:00
|
|
|
require 'spec_helper'
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
if defined? Sinatra
|
|
|
|
ERB_TEMPLATE_FOR_PAGINATE = <<EOT
|
|
|
|
<div>
|
|
|
|
<ul>
|
|
|
|
<% @users.each do |user| %>
|
|
|
|
<li class="user_info"><%= user.id %></li>
|
|
|
|
<% end %>
|
|
|
|
</ul>
|
|
|
|
<%= paginate @users, @options %>
|
|
|
|
</div>
|
2011-11-21 23:29:45 -05:00
|
|
|
EOT
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
|
|
|
|
<div>
|
|
|
|
<ul>
|
|
|
|
<% @users.each do |user| %>
|
|
|
|
<li class="user_info"><%= user.id %></li>
|
|
|
|
<% end %>
|
|
|
|
</ul>
|
|
|
|
<%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}.merge(@options || {})) %>
|
|
|
|
</div>
|
|
|
|
EOT
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
describe 'Kaminari::Helpers::SinatraHelper' do
|
2011-11-21 23:29:45 -05:00
|
|
|
before do
|
2012-05-22 17:46:01 -04:00
|
|
|
50.times {|i| User.create! :name => "user#{i}"}
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#paginate' do
|
|
|
|
before do
|
|
|
|
mock_app do
|
|
|
|
register Kaminari::Helpers::SinatraHelpers
|
|
|
|
get '/users' do
|
|
|
|
@page = params[:page] || 1
|
|
|
|
@users = User.page(@page)
|
|
|
|
@options = {}
|
|
|
|
erb ERB_TEMPLATE_FOR_PAGINATE
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
context 'normal paginations with Sinatra' do
|
|
|
|
before { get '/users' }
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should have a navigation tag' do
|
|
|
|
last_document.search('nav.pagination').should_not be_empty
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should have pagination links' do
|
|
|
|
last_document.search('.page a').should have_at_least(1).items
|
|
|
|
last_document.search('.next a').should have_at_least(1).items
|
|
|
|
last_document.search('.last a').should have_at_least(1).items
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should point to current page' do
|
|
|
|
last_document.search('.current').text.should match(/1/)
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
get '/users?page=2'
|
|
|
|
last_document.search('.current').text.should match(/2/)
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should load 25 users' do
|
|
|
|
last_document.search('li.user_info').should have(25).items
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should preserve params' do
|
|
|
|
get '/users?foo=bar'
|
|
|
|
last_document.search('.page a').should(be_all do |elm|
|
|
|
|
elm.attribute('href').value =~ /foo=bar/
|
|
|
|
end)
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
context 'optional paginations with Sinatra' do
|
|
|
|
it 'should have 5 windows with 1 gap' do
|
|
|
|
mock_app do
|
|
|
|
register Kaminari::Helpers::SinatraHelpers
|
|
|
|
get '/users' do
|
|
|
|
@page = params[:page] || 1
|
|
|
|
@users = User.page(@page).per(5)
|
|
|
|
@options = {}
|
|
|
|
erb ERB_TEMPLATE_FOR_PAGINATE
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
get '/users'
|
|
|
|
last_document.search('.page').should have(6).items
|
|
|
|
last_document.search('.gap').should have(1).item
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should controll the inner window size' do
|
|
|
|
mock_app do
|
|
|
|
register Kaminari::Helpers::SinatraHelpers
|
|
|
|
get '/users' do
|
|
|
|
@page = params[:page] || 1
|
|
|
|
@users = User.page(@page).per(3)
|
|
|
|
@options = {:window => 10}
|
|
|
|
erb ERB_TEMPLATE_FOR_PAGINATE
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
2012-05-22 17:46:01 -04:00
|
|
|
|
|
|
|
get '/users'
|
|
|
|
last_document.search('.page').should have(12).items
|
|
|
|
last_document.search('.gap').should have(1).item
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should specify a page param name' do
|
|
|
|
mock_app do
|
|
|
|
register Kaminari::Helpers::SinatraHelpers
|
|
|
|
get '/users' do
|
|
|
|
@page = params[:page] || 1
|
|
|
|
@users = User.page(@page).per(3)
|
|
|
|
@options = {:param_name => :user_page}
|
|
|
|
erb ERB_TEMPLATE_FOR_PAGINATE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/users'
|
|
|
|
last_document.search('.page a').should(be_all do |elm|
|
|
|
|
elm.attribute('href').value =~ /user_page=\d+/
|
|
|
|
end)
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
2012-05-22 17:46:01 -04:00
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
describe '#link_to_next_page' do
|
|
|
|
before do
|
2011-11-21 23:29:45 -05:00
|
|
|
mock_app do
|
|
|
|
register Kaminari::Helpers::SinatraHelpers
|
|
|
|
get '/users' do
|
|
|
|
@page = params[:page] || 1
|
2012-05-22 17:46:01 -04:00
|
|
|
@users = User.page(@page)
|
|
|
|
erb ERB_TEMPLATE_FOR_NEXT_PAGE
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
get '/users_placeholder' do
|
|
|
|
@page = params[:page] || 1
|
|
|
|
@options = {:placeholder => %{<span id='no_next_page'>No Next Page</span>}}
|
|
|
|
@users = User.page(@page)
|
|
|
|
erb ERB_TEMPLATE_FOR_NEXT_PAGE
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
context 'having more page' do
|
|
|
|
it 'should have a more page link' do
|
|
|
|
get '/users'
|
|
|
|
last_document.search('a#next_page_link').should be_present
|
|
|
|
last_document.search('a#next_page_link').text.should match(/Next!/)
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
context 'the last page' do
|
|
|
|
it 'should not have a more page link' do
|
|
|
|
get '/users?page=2'
|
|
|
|
last_document.search('a#next_page_link').should be_empty
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
|
2012-05-22 17:46:01 -04:00
|
|
|
it 'should have a no more page notation using placeholder' do
|
|
|
|
get '/users_placeholder?page=2'
|
|
|
|
last_document.search('a#next_page_link').should be_empty
|
|
|
|
last_document.search('span#no_next_page').should be_present
|
|
|
|
last_document.search('span#no_next_page').text.should match(/No Next Page/)
|
|
|
|
end
|
2011-11-21 23:29:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|