1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00

RSpec => Test/Unit

This commit is contained in:
Akira Matsuda 2016-11-19 16:51:21 +09:00
parent 850a7d8047
commit f33e2566a6
46 changed files with 1572 additions and 1771 deletions

2
.rspec
View file

@ -1,2 +0,0 @@
--color
--format=d

View file

@ -17,7 +17,7 @@ gemfile:
sudo: false
script: "bundle exec rake spec"
script: 'bundle exec rake test'
cache: bundler

View file

@ -3,16 +3,18 @@
require "bundler/gem_tasks"
require 'rspec/core'
require 'rspec/core/rake_task'
require 'rake/testtask'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = [FileList['spec/**/*_spec.rb'], "#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'spec')}/**/*_spec.rb"]
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "{test,#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test')}}/**/*_test.rb"
t.warning = true
t.verbose = true
end
task :default => "spec:all"
task :default => "test:all"
namespace :spec do
namespace :test do
mappers = %w(
active_record_edge
active_record_50
@ -24,7 +26,7 @@ namespace :spec do
desc "Run Tests against #{gemfile}"
task gemfile do
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t spec"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake -t test"
end
end
@ -32,7 +34,7 @@ namespace :spec do
task :all do
mappers.each do |gemfile|
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle --quiet"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake spec"
sh "BUNDLE_GEMFILE='gemfiles/#{gemfile}.gemfile' bundle exec rake test"
end
end
end

View file

@ -2,7 +2,6 @@ source 'https://rubygems.org'
gem 'railties', '~> 4.1.0'
gem 'activerecord', '~> 4.1.0', :require => 'active_record'
gem 'rspec-rails', '~> 2.14.1'
platforms :ruby do
if RUBY_VERSION > "2.1.0"

View file

@ -2,7 +2,6 @@ source 'https://rubygems.org'
gem 'railties', '~> 4.2.0'
gem 'activerecord', '~> 4.2.0', :require => 'active_record'
gem 'rspec-rails', '~> 2.14.1'
platforms :ruby do
if RUBY_VERSION > "2.1.0"

View file

@ -17,6 +17,4 @@ platforms :rbx do
gem 'rubinius-developer_tools'
end
gem 'rspec-rails', '~> 2.14.1'
gemspec :path => '../'

View file

@ -26,6 +26,4 @@ platforms :rbx do
gem 'rubinius-developer_tools'
end
gem 'rspec-rails', '~> 2.14.1'
gemspec :path => '../'

View file

@ -1,110 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
describe Kaminari::Configuration do
subject { Kaminari.config }
describe 'default_per_page' do
context 'by default' do
its(:default_per_page) { should == 25 }
end
context 'configured via config block' do
before do
Kaminari.configure {|c| c.default_per_page = 17}
end
its(:default_per_page) { should == 17 }
after do
Kaminari.configure {|c| c.default_per_page = 25}
end
end
end
describe 'max_per_page' do
context 'by default' do
its(:max_per_page) { should == nil }
end
context 'configure via config block' do
before do
Kaminari.configure {|c| c.max_per_page = 100}
end
its(:max_per_page) { should == 100 }
after do
Kaminari.configure {|c| c.max_per_page = nil}
end
end
end
describe 'window' do
context 'by default' do
its(:window) { should == 4 }
end
end
describe 'outer_window' do
context 'by default' do
its(:outer_window) { should == 0 }
end
end
describe 'left' do
context 'by default' do
its(:left) { should == 0 }
end
end
describe 'right' do
context 'by default' do
its(:right) { should == 0 }
end
end
describe 'param_name' do
context 'by default' do
its(:param_name) { should == :page }
end
context 'configured via config block' do
before do
Kaminari.configure {|c| c.param_name = -> { :test } }
end
its(:param_name) { should == :test }
after do
Kaminari.configure {|c| c.param_name = :page }
end
end
end
describe 'max_pages' do
context 'by default' do
its(:max_pages) { should == nil }
end
context 'configure via config block' do
before do
Kaminari.configure {|c| c.max_pages = 5}
end
its(:max_pages) { should == 5 }
after do
Kaminari.configure {|c| c.max_pages = nil}
end
end
end
describe 'params_on_first_page' do
context 'by default' do
its(:params_on_first_page) { should be_false }
end
context 'configure via config block' do
before do
Kaminari.configure {|c| c.params_on_first_page = true }
end
after do
Kaminari.configure {|c| c.params_on_first_page = false }
end
its(:params_on_first_page) { should be_true }
end
end
end

View file

@ -1,19 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
if defined?(::Rails::Railtie)
require 'rails/generators'
require 'generators/kaminari/views_generator'
describe Kaminari::Generators::GitHubApiHelper, :generator_spec => true do
describe '.get_files_in_master' do
subject { Kaminari::Generators::GitHubApiHelper.get_files_in_master }
it { should include %w(README.md 7f712676aac6bcd912981a9189c110303a1ee266) }
end
describe '.get_content_for' do
subject { Kaminari::Generators::GitHubApiHelper.get_content_for('README.md') }
it { should start_with '# ' }
end
end
end

View file

@ -1,427 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
describe 'Kaminari::ActionViewExtension', :if => defined?(::Rails::Railtie) && defined?(::ActionView) do
before do
helper.output_buffer = ::ActionView::OutputBuffer.new
end
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
-> { 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 File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'spec/fake_app/views') }
after { helper.controller.view_paths.pop }
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 :views_prefix option' do
before { helper.controller.append_view_path File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'spec/fake_app/views') }
after { helper.controller.view_paths.pop }
subject { helper.paginate @users, :views_prefix => "alternative/", :params => {:controller => 'users', :action => 'index'} }
it { should eq(" <b>1</b>\n") }
end
context 'accepts :paginator_class option' do
let(:custom_paginator) do
Class.new(Kaminari::Helpers::Paginator) do
def to_s
"CUSTOM PAGINATION"
end
end
end
subject { helper.paginate @users, :paginator_class => custom_paginator, :params => {:controller => 'users', :action => 'index'} }
it { should eq("CUSTOM PAGINATION") }
end
context "total_pages: 3" do
subject { helper.paginate @users, :total_pages => 3, :params => {:controller => 'users', :action => 'index'} }
it { should match(/<a href="\/users\?page=3">Last/) }
end
context "page: 20 (out of range)" do
before { @users = User.page(20) }
subject { helper.paginate @users, :params => {:controller => 'users', :action => 'index'} }
it { should_not match(/Last/) }
it { should_not match(/Next/) }
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 <b>1</b> 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 <b>1</b> 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 <b>all 10</b> 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 <b>all 10</b> 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total' }
end
end
describe 'the last page' do
before do
User.max_pages_per 4
@users = User.page(4).per(10)
end
after { User.max_pages_per nil }
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying users <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total' }
end
end
end
context 'I18n' do
before do
50.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1).per(25)
I18n.backend.store_translations(:en, User.i18n_scope => { :models => { :user => { :one => "person", :other => "people" } } })
end
after do
I18n.backend.reload!
end
context 'page_entries_info translates entry' do
subject { helper.page_entries_info @users, :params => {:controller => 'users', :action => 'index'} }
it { should == 'Displaying people <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total' }
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 <b>1</b> 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 <b>1</b> place' }
end
end
context 'having more than 1 but less than a page of entries' do
before do
10.times { 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 <b>all 10</b> 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 <b>all 10</b> places' }
end
end
context 'having more than one page of entries' do
before do
50.times { 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>1&nbsp;-&nbsp;25</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> 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 <b>26&nbsp;-&nbsp;50</b> of <b>50</b> 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 <b>all 3</b> 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
describe '#path_to_next_page' do
before do
2.times {|i| User.create! :name => "user#{i}"}
end
subject { helper.path_to_next_page users, :params => {:controller => 'users', :action => 'index'} }
context 'the first page' do
let(:users) { User.page(1).per(1) }
it { should eql '/users?page=2' }
end
context 'the last page' do
let(:users) { User.page(2).per(1) }
it { should be nil }
end
end
describe '#path_to_prev_page' do
before do
3.times {|i| User.create! :name => "user#{i}"}
end
subject { helper.path_to_prev_page users, :params => {:controller => 'users', :action => 'index'} }
context 'the first page' do
let(:users) { User.page(1).per(1) }
it { should be nil }
end
context 'the second page' do
let(:users) { User.page(2).per(1) }
it { should eql '/users' }
end
context 'the last page' do
let(:users) { User.page(3).per(1) }
it { should eql '/users?page=2' }
end
end
end

View file

@ -1,301 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
describe 'Kaminari::Helpers' do
describe 'Tag' do
describe '#page_url_for', :if => defined?(::Rails::Railtie) do
before do
helper.params[:controller] = 'users'
helper.params[:action] = 'index'
end
context "for first page" do
subject { Kaminari::Helpers::Tag.new(helper).page_url_for(1) }
it { should == "/users" }
context 'config.params_on_first_page == false' do
before { Kaminari.config.params_on_first_page = true }
after { Kaminari.config.params_on_first_page = false }
it { should == "/users?page=1" }
end
end
context "with a friendly route setting" do
before do
helper.params[:controller] = 'addresses'
helper.params[:action] = 'index'
helper.params[:page] = 3
end
context "for first page" do
subject { Kaminari::Helpers::Tag.new(helper).page_url_for(1) }
it { should == "/addresses" }
context 'config.params_on_first_page == false' do
before { Kaminari.config.params_on_first_page = true }
after { Kaminari.config.params_on_first_page = false }
it { should match("/addresses/page/1") }
end
end
context "for other page" do
subject { Kaminari::Helpers::Tag.new(helper).page_url_for(5) }
it { should == "/addresses/page/5" }
end
end
context "with param_name = 'user[page]' option" do
before do
helper.params[:user] = {:page => "3", :scope => "active"}
end
context "for first page" do
subject { Kaminari::Helpers::Tag.new(helper, :param_name => "user[page]").page_url_for(1) }
it { should_not match(/user%5Bpage%5D=\d+/) } # not match user[page]=\d+
it { should match(/user%5Bscope%5D=active/) } # match user[scope]=active
end
context "for other page" do
subject { Kaminari::Helpers::Tag.new(helper, :param_name => "user[page]").page_url_for(2) }
it { should match(/user%5Bpage%5D=2/) } # match user[page]=2
it { should match(/user%5Bscope%5D=active/) } # match user[scope]=active
end
end
context "with param_name = 'foo.page' option" do
before do
helper.params['foo.page'] = 2
end
context "for first page" do
subject { Kaminari::Helpers::Tag.new(helper, :param_name => "foo.page").page_url_for(1) }
it { should_not match(/foo\.page=\d+/) }
end
context "for other page" do
subject { Kaminari::Helpers::Tag.new(helper, :param_name => "foo.page").page_url_for(2) }
it { should match(/foo\.page=\d+/) }
end
end
end
end
describe 'Paginator' do
describe 'Paginator::PageProxy' do
describe '#current?' do
context 'current_page == page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 26}, 26, nil) }
its(:current?) { should be_true }
end
context 'current_page != page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 13}, 26, nil) }
its(:current?) { should_not be_true }
end
end
describe '#first?' do
context 'page == 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 26}, 1, nil) }
its(:first?) { should be_true }
end
context 'page != 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 13}, 2, nil) }
its(:first?) { should_not be_true }
end
end
describe '#last?' do
context 'current_page == page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 39}, 39, nil) }
its(:last?) { should be_true }
end
context 'current_page != page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 39}, 38, nil) }
its(:last?) { should_not be_true }
end
end
describe '#next?' do
context 'page == current_page + 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 53, nil) }
its(:next?) { should be_true }
end
context 'page != current_page + 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 77, nil) }
its(:next?) { should_not be_true }
end
end
describe '#prev?' do
context 'page == current_page - 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 76, nil) }
its(:prev?) { should be_true }
end
context 'page != current_page + 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 80, nil) }
its(:prev?) { should_not be_true }
end
end
describe '#rel' do
context 'page == current_page - 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 76, nil) }
its(:rel) { should eq 'prev' }
end
context 'page == current_page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 78}, 78, nil) }
its(:rel) { should be_nil }
end
context 'page == current_page + 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 53, nil) }
its(:rel) { should eq 'next' }
end
end
describe '#left_outer?' do
context 'current_page == left' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 3, nil) }
its(:left_outer?) { should be_true }
end
context 'current_page == left + 1' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 4, nil) }
its(:left_outer?) { should_not be_true }
end
context 'current_page == left + 2' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 5, nil) }
its(:left_outer?) { should_not be_true }
end
end
describe '#right_outer?' do
context 'total_pages - page > right' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 6, nil) }
its(:right_outer?) { should_not be_true }
end
context 'total_pages - page == right' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 7, nil) }
its(:right_outer?) { should_not be_true }
end
context 'total_pages - page < right' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 8, nil) }
its(:right_outer?) { should be_true }
end
end
describe '#inside_window?' do
context 'page > current_page' do
context 'page - current_page > window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 5}, 10, nil) }
its(:inside_window?) { should_not be_true }
end
context 'page - current_page == window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 6}, 10, nil) }
its(:inside_window?) { should be_true }
end
context 'page - current_page < window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 7}, 10, nil) }
its(:inside_window?) { should be_true }
end
end
context 'current_page > page' do
context 'current_page - page > window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 4}, 10, nil) }
its(:inside_window?) { should_not be_true }
end
context 'current_page - page == window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 5}, 10, nil) }
its(:inside_window?) { should be_true }
end
context 'current_page - page < window' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 6}, 10, nil) }
its(:inside_window?) { should be_true }
end
end
end
describe '#was_truncated?' do
before do
stub(@template = Object.new) do
options { {} }
params { {} }
end
end
context 'last.is_a? Gap' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({}, 10, Kaminari::Helpers::Gap.new(@template)) }
its(:was_truncated?) { should be_true }
end
context 'last.is not a Gap' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({}, 10, Kaminari::Helpers::Page.new(@template)) }
its(:was_truncated?) { should_not be_true }
end
end
describe "#single_gap?" do
let(:window_options) do
{
:left => 1,
:window => 1,
:right => 1,
:total_pages => 9
}
end
def gap_for(page)
Kaminari::Helpers::Paginator::PageProxy.new(window_options, page, nil)
end
context "in case of '1 ... 4 5 6 ... 9'" do
before { window_options[:current_page] = 5 }
its("gap for 2") { gap_for(2).should_not be_a_single_gap }
its("gap for 3") { gap_for(3).should_not be_a_single_gap }
its("gap for 7") { gap_for(7).should_not be_a_single_gap }
its("gap for 8") { gap_for(8).should_not be_a_single_gap }
end
context "in case of '1 ... 3 4 5 ... 9'" do
before { window_options[:current_page] = 4 }
its("gap for 2") { gap_for(2).should be_a_single_gap }
its("gap for 6") { gap_for(6).should_not be_a_single_gap }
its("gap for 8") { gap_for(8).should_not be_a_single_gap }
end
context "in case of '1 ... 3 4 5 ... 7'" do
before do
window_options[:current_page] = 4
window_options[:total_pages] = 7
end
its("gap for 2") { gap_for(2).should be_a_single_gap }
its("gap for 6") { gap_for(6).should be_a_single_gap }
end
context "in case of '1 ... 5 6 7 ... 9'" do
before { window_options[:current_page] = 6 }
its("gap for 2") { gap_for(2).should_not be_a_single_gap }
its("gap for 4") { gap_for(4).should_not be_a_single_gap }
its("gap for 8") { gap_for(8).should be_a_single_gap }
end
end
describe "#out_of_range?" do
context 'within range' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 4, nil).out_of_range? }
it { should == false }
end
context 'on last page' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 5, nil).out_of_range? }
it { should == false }
end
context 'out of range' do
subject { Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 6, nil).out_of_range? }
it { should == true }
end
end
end
end
end

View file

@ -1,58 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
if defined? ActiveRecord
describe Kaminari::ActiveRecordRelationMethods do
describe '#total_count' do
before do
@author = User.create! :name => 'author'
@author2 = User.create! :name => 'author2'
@author3 = User.create! :name => 'author3'
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
@readers = 4.times.map { User.create! :name => 'reader' }
@books.each {|book| book.readers << @readers }
end
context "when the scope is cloned" do
it "should reset total_count memoization" do
User.page.tap(&:total_count).where(:name => 'author').total_count.should == 1
end
end
context "when the scope includes an order which references a generated column" do
it "should successfully count the results" do
@author.readers.by_read_count.page(1).total_count.should == @readers.size
end
end
context "when the scope use conditions on includes" do
it "should keep includes and successfully count the results" do
# Only @author and @author2 have books titled with the title00x pattern
User.includes(:books_authored).references(:books).where("books.title LIKE 'title00%'").page(1).total_count.should == 2
end
end
context 'when the Relation has custom select clause' do
specify do
-> { User.select('*, 1 as one').page(1).total_count }.should_not raise_exception
end
end
context "when total_count receives options" do
it "should ignore the options for rails 4.1+" do
User.page(1).total_count(:name, :distinct => true).should == 7
end
end
context "when the scope returns an ActiveSupport::OrderedHash" do
it "should not throw exception by passing options to count" do
lambda {
@author.readers.by_read_count.page(1).total_count(:name, :distinct => true)
}.should_not raise_exception
end
end
end
end
end

View file

@ -1,10 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
if defined? ActiveRecord
describe Kaminari::ActiveRecordModelExtension do
subject { Class.new(ActiveRecord::Base) }
it { should respond_to :page }
it { should respond_to :fake_gem_defined_method }
end
end

View file

@ -1,395 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
if defined? ActiveRecord
describe Kaminari::ActiveRecordModelExtension do
before do
Kaminari.configure do |config|
config.page_method_name = :per_page_kaminari
end
end
subject { Class.new ActiveRecord::Base }
it { should respond_to(:per_page_kaminari) }
it { should_not respond_to(:page) }
after do
Kaminari.configure do |config|
config.page_method_name = :page
end
end
end
describe Kaminari::ActiveRecordExtension do
shared_examples_for 'the first page' do
it { should have(25).users }
its('first.name') { should == 'user001' }
end
shared_examples_for 'blank page' do
it { should have(0).users }
end
before :all do
[User, GemDefinedModel, Device].each do |m|
1.upto(100) {|i| m.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
end
end
after :all do
[User, GemDefinedModel, Device].each(&:delete_all)
end
[User, Admin, GemDefinedModel, Device].each do |model_class|
context "for #{model_class}" do
describe '#page' do
context 'page 1' do
subject { model_class.page 1 }
it_should_behave_like 'the first page'
end
context 'page 2' do
subject { model_class.page 2 }
it { should have(25).users }
its('first.name') { should == 'user026' }
end
context 'page without an argument' do
subject { model_class.page }
it_should_behave_like 'the first page'
end
context 'page < 1' do
subject { model_class.page 0 }
it_should_behave_like 'the first page'
end
context 'page > max page' do
subject { model_class.page 5 }
it_should_behave_like 'blank page'
end
describe 'ensure #order_values is preserved' do
subject { model_class.order('id').page 1 }
its('order_values.uniq') { should == ['id'] }
end
end
describe '#per' do
context 'page 1 per 5' do
subject { model_class.page(1).per(5) }
it { should have(5).users }
its('first.name') { should == 'user001' }
context 'with max_per_page < 5' do
before { model_class.max_paginates_per 4 }
it { should have(4).users }
after { model_class.max_paginates_per nil }
end
end
context "page 1 per nil (using default)" do
subject { model_class.page(1).per(nil) }
it { should have(model_class.default_per_page).users }
context 'with max_per_page < default_per_page' do
around do |example|
begin
model_class.max_paginates_per(10)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
it { should have(10).users }
end
end
context "page 1 per 0" do
subject { model_class.page(1).per(0) }
it { should have(0).users }
end
# I know it's a bit strange to have this here, but I couldn't find any better place for this case
context 'when max_per_page is given via model class, and `per` is not actually called' do
subject { model_class.page(1) }
context 'with max_per_page > default_per_page' do
around do |example|
begin
model_class.max_paginates_per(200)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
it { should have(25).users }
end
context 'with max_per_page < default_per_page' do
around do |example|
begin
model_class.max_paginates_per(5)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
it { should have(5).users }
end
end
end
describe '#max_paginates_per' do
around do |example|
begin
model_class.max_paginates_per(10)
example.run
ensure
model_class.max_paginates_per(nil)
end
end
context 'calling max_paginates_per() after per()' do
context 'when #max_paginates_per is greater than #per' do
subject { model_class.page(1).per(15).max_paginates_per(20) }
it { should have(15).users }
end
context 'when #per is greater than #max_paginates_per' do
subject { model_class.page(1).per(30).max_paginates_per(20) }
it { should have(20).users }
end
context 'when nil is given to #per and #max_paginates_per is specified' do
subject { model_class.page(1).per(nil).max_paginates_per(20) }
it { should have(20).users }
end
end
context 'calling max_paginates_per() before per()' do
context 'when #max_paginates_per is greater than #per' do
subject { model_class.page(1).max_paginates_per(20).per(15) }
it { should have(15).users }
end
context 'when #per is greater than #max_paginates_per' do
subject { model_class.page(1).max_paginates_per(20).per(30) }
it { should have(20).users }
end
context 'when nil is given to #per and #max_paginates_per is specified' do
subject { model_class.page(1).max_paginates_per(20).per(nil) }
it { should have(20).users }
end
end
context 'calling max_paginates_per() without per()' do
context 'when #max_paginates_per is greater than the default per_page' do
subject { model_class.page(1).max_paginates_per(20) }
it { should have(20).users }
end
context 'when #max_paginates_per is less than the default per_page' do
subject { model_class.page(1).max_paginates_per(30) }
it { should have(25).users }
end
end
end
describe '#padding' do
context 'page 1 per 5 padding 1' do
subject { model_class.page(1).per(5).padding(1) }
it { should have(5).users }
its('first.name') { should == 'user002' }
end
context 'page 19 per 5 padding 5' do
subject { model_class.page(19).per(5).padding(5) }
its(:current_page) { should == 19 }
its(:total_pages) { should == 19 }
end
end
describe '#total_pages' do
context 'per 25 (default)' do
subject { model_class.page }
its(:total_pages) { should == 4 }
end
context 'per 7' do
subject { model_class.page(2).per(7) }
its(:total_pages) { should == 15 }
end
context 'per 65536' do
subject { model_class.page(50).per(65536) }
its(:total_pages) { should == 1 }
end
context 'per 0' do
subject { model_class.page(50).per(0) }
it "raises Kaminari::ZeroPerPageOperation" do
expect { subject.total_pages }.to raise_error(Kaminari::ZeroPerPageOperation)
end
end
context 'per -1 (using default)' do
subject { model_class.page(5).per(-1) }
its(:total_pages) { should == 4 }
end
context 'per "String value that can not be converted into Number" (using default)' do
subject { model_class.page(5).per('aho') }
its(:total_pages) { should == 4 }
end
context 'with max_pages < total pages count from database' do
before { model_class.max_pages_per 3 }
subject { model_class.page }
its(:total_pages) { should == 3 }
after { model_class.max_pages_per nil }
end
context 'with max_pages > total pages count from database' do
before { model_class.max_pages_per 11 }
subject { model_class.page }
its(:total_pages) { should == 4 }
after { model_class.max_pages_per nil }
end
context 'with max_pages is nil' do
before { model_class.max_pages_per nil }
subject { model_class.page }
its(:total_pages) { should == 4 }
end
context "with per(nil) using default" do
subject { model_class.page.per(nil) }
its(:total_pages) { should == 4 }
end
end
describe '#current_page' do
context 'any page, per 0' do
subject { model_class.page.per(0) }
it "raises Kaminari::ZeroPerPageOperation" do
expect { subject.current_page }.to raise_error(Kaminari::ZeroPerPageOperation)
end
end
context 'page 1' do
subject { model_class.page }
its(:current_page) { should == 1 }
end
context 'page 2' do
subject { model_class.page(2).per 3 }
its(:current_page) { should == 2 }
end
end
describe '#next_page' do
context 'page 1' do
subject { model_class.page }
its(:next_page) { should == 2 }
end
context 'page 5' do
subject { model_class.page(5) }
its(:next_page) { should be_nil }
end
end
describe '#prev_page' do
context 'page 1' do
subject { model_class.page }
its(:prev_page) { should be_nil }
end
context 'page 3' do
subject { model_class.page(3) }
its(:prev_page) { should == 2 }
end
context 'page 5' do
subject { model_class.page(5) }
its(:prev_page) { should be_nil }
end
end
describe '#first_page?' do
context 'on first page' do
subject { model_class.page(1).per(10) }
its(:first_page?) { should == true }
end
context 'not on first page' do
subject { model_class.page(5).per(10) }
its(:first_page?) { should == false }
end
end
describe '#last_page?' do
context 'on last page' do
subject { model_class.page(10).per(10) }
its(:last_page?) { should == true }
end
context 'within range' do
subject { model_class.page(1).per(10) }
its(:last_page?) { should == false }
end
context 'out of range' do
subject { model_class.page(11).per(10) }
its(:last_page?) { should == false }
end
end
describe '#out_of_range?' do
context 'on last page' do
subject { model_class.page(10).per(10) }
its(:out_of_range?) { should == false }
end
context 'within range' do
subject { model_class.page(1).per(10) }
its(:out_of_range?) { should == false }
end
context 'out of range' do
subject { model_class.page(11).per(10) }
its(:out_of_range?) { should == true }
end
end
describe '#count' do
context 'page 1' do
subject { model_class.page }
its(:count) { should == 25 }
end
context 'page 2' do
subject { model_class.page 2 }
its(:count) { should == 25 }
end
end
context 'chained with .group' do
subject { model_class.group('age').page(2).per 5 }
# 0..10
its(:total_count) { should == 11 }
its(:total_pages) { should == 3 }
end
context 'activerecord descendants' do
subject { ActiveRecord::Base.descendants }
its(:length) { should_not == 0 }
end
end
end
end
end

View file

@ -1,192 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
describe Kaminari::PaginatableArray do
it { should have(0).items }
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
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(:current_page) { should == 1 }
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(:current_page) { should == 2 }
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
context "page 1 per 0" do
subject { array.page(1).per(0) }
it { should have(0).users }
end
end
describe '#total_pages' do
context 'per 25 (default)' do
subject { array.page }
its(:total_pages) { should == 4 }
end
context 'per 7' do
subject { array.page(2).per(7) }
its(:total_pages) { should == 15 }
end
context 'per 65536' do
subject { array.page(50).per(65536) }
its(:total_pages) { should == 1 }
end
context 'per 0' do
subject { array.page(50).per(0) }
it "raises Kaminari::ZeroPerPageOperation" do
expect { subject.total_pages }.to raise_error(Kaminari::ZeroPerPageOperation)
end
end
context 'per -1 (using default)' do
subject { array.page(5).per(-1) }
its(:total_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(:total_pages) { should == 4 }
end
context 'per 25, padding 25' do
subject { array.page(1).padding(25) }
its(:total_pages) { should == 3 }
end
end
describe '#current_page' do
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
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 '#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 3' do
subject { array.page 3 }
its(:prev_page) { should == 2 }
end
context 'page 5' do
subject { array.page 5 }
its(:prev_page) { should be_nil }
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
context 'when setting total count explicitly' do
context "array 1..10, page 5, per 10, total_count 9999" do
subject { Kaminari::PaginatableArray.new((1..10).to_a, :total_count => 9999).page(5).per(10) }
it { should have(10).items }
its(:first) { should == 1 }
its(:current_page) { should == 5 }
its(:total_count) { should == 9999 }
end
context "array 1..15, page 1, per 10, total_count 15" do
subject { Kaminari.paginate_array((1..15).to_a, :total_count => 15).page(1).per(10) }
it { should have(10).items }
its(:first) { should == 1 }
its(:current_page) { should == 1 }
its(:total_count) { should == 15 }
end
context "array 1..25, page 2, per 10, total_count 15" do
subject { Kaminari.paginate_array((1..25).to_a, :total_count => 15).page(2).per(10) }
it { should have(5).items }
its(:first) { should == 11 }
its(:current_page) { should == 2 }
its(:total_count) { should == 15 }
end
end
end

View file

@ -1,126 +0,0 @@
# frozen_string_literal: true
require 'spec_helper'
describe "configuration methods" do
let(:model){ User }
describe "#default_per_page" do
if defined? ActiveRecord
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :paginates_per }
end
end
subject { model.page(1) }
context "by default" do
its(:limit_value){ should == 25 }
end
context "when configuring both on global and model-level" do
before do
Kaminari.configure {|c| c.default_per_page = 50 }
model.paginates_per 100
end
its(:limit_value){ should == 100 }
end
context "when configuring multiple times" do
before do
Kaminari.configure {|c| c.default_per_page = 10 }
Kaminari.configure {|c| c.default_per_page = 20 }
end
its(:limit_value){ should == 20 }
end
after do
Kaminari.configure {|c| c.default_per_page = 25 }
model.paginates_per nil
end
end
describe "#max_per_page" do
if defined? ActiveRecord
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :max_paginates_per }
end
end
subject { model.page(1).per(1000) }
context "by default" do
its(:limit_value){ should == 1000 }
end
context "when configuring both on global and model-level" do
before do
Kaminari.configure {|c| c.max_per_page = 50 }
model.max_paginates_per 100
end
its(:limit_value){ should == 100 }
end
context "when configuring multiple times" do
before do
Kaminari.configure {|c| c.max_per_page = 10 }
Kaminari.configure {|c| c.max_per_page = 20 }
end
its(:limit_value){ should == 20 }
end
after do
Kaminari.configure {|c| c.max_per_page = nil }
model.max_paginates_per nil
end
end
describe "#max_pages" do
if defined? ActiveRecord
describe 'AR::Base' do
subject { ActiveRecord::Base }
it { should_not respond_to :max_pages_per }
end
end
before do
100.times do |count|
model.create!(:name => "User#{count}")
end
end
subject { model.page(1).per(5) }
context "by default" do
its(:total_pages){ should == 20 }
end
context "when configuring both on global and model-level" do
before do
Kaminari.configure {|c| c.max_pages = 10 }
model.max_pages_per 15
end
its(:total_pages){ should == 15 }
end
context "when configuring multiple times" do
before do
Kaminari.configure {|c| c.max_pages = 10 }
Kaminari.configure {|c| c.max_pages = 15 }
end
its(:total_pages){ should == 15 }
end
after do
Kaminari.configure {|c| c.max_pages = nil }
model.max_pages_per nil
end
end
end

View file

@ -1,16 +0,0 @@
# encoding: UTF-8
# frozen_string_literal: true
require 'spec_helper'
feature 'Rendering with format: option' do
background do
User.create! :name => "user1"
end
scenario "Make sure that kaminari doesn't affect the format" do
visit '/users/index_text.text'
page.status_code.should == 200
page.should have_content 'partial1'
page.should have_content 'partial2'
end
end

View file

@ -1,14 +0,0 @@
# frozen_string_literal: true
DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
RSpec.configure do |config|
config.before :suite do
DatabaseCleaner.clean_with :truncation if defined? ActiveRecord
end
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
end

View file

@ -1,34 +0,0 @@
# frozen_string_literal: true
RSpec::Matchers.define :contain_tag do |klass|
match do |collection|
if @num.blank?
collection.any? {|tag| tag.is_a? klass}
else
(@count = collection.count {|tag| tag.is_a? klass}) == @num
end
end
def count(num)
@num = num
self
end
description do
"contain #{@num || 'any'} instance(s) of #{klass.name}"
end
failure_message_for_should do |_collection|
"expected #{@num || 'any'} instance(s) of #{klass.name} but was #{@count}"
end
end
RSpec::Matchers.define :skip do |num|
match do |criteria|
criteria.instance_variable_get('@options')[:skip] == num
end
end
RSpec::Matchers.define :offset do |num|
match do |collection|
collection.offset_value == num
end
end

View file

@ -0,0 +1,98 @@
# frozen_string_literal: true
require 'test_helper'
class ConfigurationTest < ::Test::Unit::TestCase
sub_test_case 'default_per_page' do
test 'by default' do
assert_equal 25, Kaminari.config.default_per_page
end
test 'configured via config block' do
begin
Kaminari.configure {|c| c.default_per_page = 17}
assert_equal 17, Kaminari.config.default_per_page
ensure
Kaminari.configure {|c| c.default_per_page = 25}
end
end
end
sub_test_case 'max_per_page' do
test 'by default' do
assert_nil Kaminari.config.max_per_page
end
test 'configure via config block' do
begin
Kaminari.configure {|c| c.max_per_page = 100}
assert_equal 100, Kaminari.config.max_per_page
ensure
Kaminari.configure {|c| c.max_per_page = nil}
end
end
end
sub_test_case 'window' do
test 'by default' do
assert_equal 4, Kaminari.config.window
end
end
sub_test_case 'outer_window' do
test 'by default' do
assert_equal 0, Kaminari.config.outer_window
end
end
sub_test_case 'left' do
test 'by default' do
assert_equal 0, Kaminari.config.left
end
end
sub_test_case 'right' do
test 'by default' do
assert_equal 0, Kaminari.config.right
end
end
sub_test_case 'param_name' do
test 'by default' do
assert_equal :page, Kaminari.config.param_name
end
test 'configured via config block' do
begin
Kaminari.configure {|c| c.param_name = -> { :test } }
assert_equal :test, Kaminari.config.param_name
ensure
Kaminari.configure {|c| c.param_name = :page }
end
end
end
sub_test_case 'max_pages' do
test 'by default' do
assert_nil Kaminari.config.max_pages
end
test 'configure via config block' do
begin
Kaminari.configure {|c| c.max_pages = 5}
assert_equal 5, Kaminari.config.max_pages
ensure
Kaminari.configure {|c| c.max_pages = nil}
end
end
end
sub_test_case 'params_on_first_page' do
test 'by default' do
assert_equal false, Kaminari.config.params_on_first_page
end
test 'configure via config block' do
begin
Kaminari.configure {|c| c.params_on_first_page = true }
assert_equal true, Kaminari.config.params_on_first_page
ensure
Kaminari.configure {|c| c.params_on_first_page = false }
end
end
end
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'test_helper'
if defined?(::Rails::Railtie) && ENV['GENERATOR_SPEC']
require 'rails/generators'
require 'generators/kaminari/views_generator'
class GitHubApiHelperTest < ::Test::Unit::TestCase
test '.get_files_in_master' do
assert_include Kaminari::Generators::GitHubApiHelper.get_files_in_master, %w(README.md 7f712676aac6bcd912981a9189c110303a1ee266)
end
test '.get_content_for' do
assert Kaminari::Generators::GitHubApiHelper.get_content_for('README.md').start_with?('# ')
end
end
end

View file

@ -0,0 +1,416 @@
# frozen_string_literal: true
require 'test_helper'
if defined?(::Rails::Railtie) && defined?(::ActionView)
class ActionViewExtensionTest < ActionView::TestCase
setup do
self.output_buffer = ::ActionView::OutputBuffer.new
end
teardown do
User.delete_all
end
sub_test_case '#paginate' do
setup do
50.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1)
end
test 'returns a String' do
assert_kind_of String, view.paginate(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'escaping the pagination for javascript' do
assert_nothing_raised do
escape_javascript(view.paginate @users, :params => {:controller => 'users', :action => 'index'})
end
end
test 'accepts :theme option' do
begin
controller.append_view_path File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test/fake_app/views')
html = view.paginate @users, :theme => 'bootstrap', :params => {:controller => 'users', :action => 'index'}
assert_match(/bootstrap-paginator/, html)
assert_match(/bootstrap-page-link/, html)
ensure
controller.view_paths.pop
end
end
test 'accepts :views_prefix option' do
begin
controller.append_view_path File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test/fake_app/views')
assert_equal " <b>1</b>\n", view.paginate(@users, :views_prefix => 'alternative/', :params => {:controller => 'users', :action => 'index'})
ensure
controller.view_paths.pop
end
end
test 'accepts :paginator_class option' do
custom_paginator = Class.new(Kaminari::Helpers::Paginator) do
def to_s
"CUSTOM PAGINATION"
end
end
assert_equal 'CUSTOM PAGINATION', view.paginate(@users, :paginator_class => custom_paginator, :params => {:controller => 'users', :action => 'index'})
end
test 'total_pages: 3' do
assert_match(/<a href="\/users\?page=3">Last/, view.paginate(@users, :total_pages => 3, :params => {:controller => 'users', :action => 'index'}))
end
test "page: 20 (out of range)" do
users = User.page(20)
html = view.paginate users, :params => {:controller => 'users', :action => 'index'}
assert_not_match(/Last/, html)
assert_not_match(/Next/, html)
end
end
sub_test_case '#link_to_previous_page' do
setup do
60.times {|i| User.create! :name => "user#{i}"}
end
sub_test_case 'having previous pages' do
setup do
@users = User.page(3)
end
test 'the default behaviour' do
html = view.link_to_previous_page @users, 'Previous', :params => {:controller => 'users', :action => 'index'}
assert_match(/page=2/, html)
assert_match(/rel="prev"/, html)
end
test 'overriding rel=' do
assert_match(/rel="external"/, view.link_to_previous_page(@users, 'Previous', :rel => 'external', :params => {:controller => 'users', :action => 'index'}))
end
test 'with params' do
params[:status] = 'active'
assert_match(/status=active/, view.link_to_previous_page(@users, 'Previous', :params => {:controller => 'users', :action => 'index'}))
end
end
test 'the first page' do
users = User.page(1)
assert_nil view.link_to_previous_page(users, 'Previous', :params => {:controller => 'users', :action => 'index'})
end
test 'out of range' do
users = User.page(5)
assert_nil view.link_to_next_page(users, 'More', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case '#link_to_next_page' do
setup do
50.times {|i| User.create! :name => "user#{i}"}
end
sub_test_case 'having more page' do
setup do
@users = User.page(1)
end
test 'the default behaviour' do
html = view.link_to_next_page @users, 'More', :params => {:controller => 'users', :action => 'index'}
assert_match(/page=2/, html)
assert_match(/rel="next"/, html)
end
test 'overriding rel=' do
assert_match(/rel="external"/, view.link_to_next_page(@users, 'More', :rel => 'external', :params => {:controller => 'users', :action => 'index'}))
end
test 'with params' do
params[:status] = 'active'
assert_match(/status=active/, view.link_to_next_page(@users, 'More', :params => {:controller => 'users', :action => 'index'}))
end
end
test 'the last page' do
users = User.page(2)
assert_nil view.link_to_next_page(users, 'More', :params => {:controller => 'users', :action => 'index'})
end
test 'out of range' do
users = User.page(5)
assert_nil view.link_to_next_page(users, 'More', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case '#page_entries_info' do
sub_test_case 'on a model without namespace' do
setup do
@users = User.page(1).per(25)
end
sub_test_case 'having no entries' do
test 'with default entry name' do
assert_equal 'No users found', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'setting the entry name option to "member"' do
assert_equal 'No members found', view.page_entries_info(@users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'having 1 entry' do
setup do
User.create! :name => 'user1'
@users = User.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying <b>1</b> user', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'setting the entry name option to "member"' do
assert_equal 'Displaying <b>1</b> member', view.page_entries_info(@users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'having more than 1 but less than a page of entries' do
setup do
10.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying <b>all 10</b> users', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'setting the entry name option to "member"' do
assert_equal 'Displaying <b>all 10</b> members', view.page_entries_info(@users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'having more than one page of entries' do
setup do
50.times {|i| User.create! :name => "user#{i}"}
end
sub_test_case 'the first page' do
setup do
@users = User.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying users <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'setting the entry name option to "member"' do
assert_equal 'Displaying members <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total', view.page_entries_info(@users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'the next page' do
setup do
@users = User.page(2).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying users <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
test 'setting the entry name option to "member"' do
assert_equal 'Displaying members <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(@users, :entry_name => 'member', :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'the last page' do
setup do
User.max_pages_per 4
@users = User.page(4).per(10)
end
teardown do
User.max_pages_per nil
end
test 'with default entry name' do
assert_equal 'Displaying users <b>31&nbsp;-&nbsp;40</b> of <b>50</b> in total', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
end
end
end
sub_test_case 'I18n' do
setup do
50.times {|i| User.create! :name => "user#{i}"}
@users = User.page(1).per(25)
I18n.backend.store_translations(:en, User.i18n_scope => { :models => { :user => { :one => "person", :other => "people" } } })
end
teardown do
I18n.backend.reload!
end
test 'page_entries_info translates entry' do
assert_equal 'Displaying people <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total', view.page_entries_info(@users, :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case 'on a model with namespace' do
setup do
@addresses = User::Address.page(1).per(25)
end
test 'having no entries' do
assert_equal 'No addresses found', view.page_entries_info(@addresses, :params => {:controller => 'addresses', :action => 'index'})
end
sub_test_case 'having 1 entry' do
setup do
User::Address.create!
@addresses = User::Address.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying <b>1</b> address', view.page_entries_info(@addresses, :params => {:controller => 'addresses', :action => 'index'})
end
test 'setting the entry name option to "place"' do
assert_equal 'Displaying <b>1</b> place', view.page_entries_info(@addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'})
end
end
sub_test_case 'having more than 1 but less than a page of entries' do
setup do
10.times { User::Address.create! }
@addresses = User::Address.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying <b>all 10</b> addresses', view.page_entries_info(@addresses, :params => {:controller => 'addresses', :action => 'index'})
end
test 'setting the entry name option to "place"' do
assert_equal 'Displaying <b>all 10</b> places', view.page_entries_info(@addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'})
end
end
sub_test_case 'having more than one page of entries' do
setup do
50.times { User::Address.create! }
end
sub_test_case 'the first page' do
setup do
@addresses = User::Address.page(1).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying addresses <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total', view.page_entries_info(@addresses, :params => {:controller => 'addresses', :action => 'index'})
end
test 'setting the entry name option to "place"' do
assert_equal 'Displaying places <b>1&nbsp;-&nbsp;25</b> of <b>50</b> in total', view.page_entries_info(@addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'})
end
end
sub_test_case 'the next page' do
setup do
@addresses = User::Address.page(2).per(25)
end
test 'with default entry name' do
assert_equal 'Displaying addresses <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(@addresses, :params => {:controller => 'addresses', :action => 'index'})
end
test 'setting the entry name option to "place"' do
assert_equal 'Displaying places <b>26&nbsp;-&nbsp;50</b> of <b>50</b> in total', view.page_entries_info(@addresses, :entry_name => 'place', :params => {:controller => 'addresses', :action => 'index'})
end
end
end
end
test 'on a PaginatableArray' do
numbers = Kaminari.paginate_array(%w{one two three}).page(1)
assert_equal 'Displaying <b>all 3</b> entries', view.page_entries_info(numbers)
end
end
sub_test_case '#rel_next_prev_link_tags' do
setup do
31.times {|i| User.create! :name => "user#{i}"}
end
test 'the first page' do
users = User.page(1).per(10)
html = view.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'}
assert_not_match(/rel="prev"/, html)
assert_match(/rel="next"/, html)
assert_match(/\?page=2/, html)
end
test 'the second page' do
users = User.page(2).per(10)
html = view.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'}
assert_match(/rel="prev"/, html)
assert_not_match(/\?page=1/, html)
assert_match(/rel="next"/, html)
assert_match(/\?page=3/, html)
end
test 'the last page' do
users = User.page(4).per(10)
html = view.rel_next_prev_link_tags users, :params => {:controller => 'users', :action => 'index'}
assert_match(/rel="prev"/, html)
assert_match(/\?page=3"/, html)
assert_not_match(/rel="next"/, html)
end
end
sub_test_case '#path_to_next_page' do
setup do
2.times {|i| User.create! :name => "user#{i}"}
end
test 'the first page' do
users = User.page(1).per(1)
assert_equal '/users?page=2', view.path_to_next_page(users, :params => {:controller => 'users', :action => 'index'})
end
test 'the last page' do
users = User.page(2).per(1)
assert_nil view.path_to_next_page(users, :params => {:controller => 'users', :action => 'index'})
end
end
sub_test_case '#path_to_prev_page' do
setup do
3.times {|i| User.create! :name => "user#{i}"}
end
test 'the first page' do
users = User.page(1).per(1)
assert_nil view.path_to_prev_page(users, :params => {:controller => 'users', :action => 'index'})
end
test 'the second page' do
users = User.page(2).per(1)
assert_equal '/users', view.path_to_prev_page(users, :params => {:controller => 'users', :action => 'index'})
end
test 'the last page' do
users = User.page(3).per(1)
assert_equal'/users?page=2', view.path_to_prev_page(users, :params => {:controller => 'users', :action => 'index'})
end
end
end
end

View file

@ -1,8 +1,10 @@
# frozen_string_literal: true
require 'spec_helper'
require 'test_helper'
describe 'Kaminari::Helpers::Paginator' do
let :template do
class PaginatorHelperTest < ActiveSupport::TestCase
include Kaminari::Helpers
def template
stub(r = Object.new) do
render.with_any_args
params { {} }
@ -14,43 +16,32 @@ describe 'Kaminari::Helpers::Paginator' do
r
end
describe "view helper methods delegated to template" do
before do
@paginator = Kaminari::Helpers::Paginator.new(template, :params => {})
end
subject { @paginator.link_to("link", "#") }
it { should == "<a href='#'>link</a>" }
test 'view helper methods delegated to template' do
paginator = Paginator.new(template, :params => {})
assert_equal "<a href='#'>link</a>", paginator.link_to('link', '#')
end
describe '#params' do
before do
@paginator = Kaminari::Helpers::Paginator.new(template, :params => {:controller => 'foo', :action => 'bar'})
sub_test_case '#params' do
setup do
@paginator = Paginator.new(template, :params => {:controller => 'foo', :action => 'bar'})
end
subject { @paginator.page_tag(template).instance_variable_get('@params') }
it { should == {'controller' => 'foo', 'action' => 'bar'} }
context "when params has form params" do
before do
stub(template).params do
{
:authenticity_token => "token",
:commit => "submit",
:utf8 => "true",
:_method => "patch"
}
end
test 'when params has no form params' do
assert_equal({'controller' => 'foo', 'action' => 'bar'}, @paginator.page_tag(template).instance_variable_get('@params'))
end
test 'when params has form params' do
stub(template).params do
{:authenticity_token => 'token', :commit => 'submit', :utf8 => 'true', :_method => 'patch'}
end
it { should == {'controller' => 'foo', 'action' => 'bar'} }
assert_equal({'controller' => 'foo', 'action' => 'bar'}, @paginator.page_tag(template).instance_variable_get('@params'))
end
end
describe '#param_name' do
before do
@paginator = Kaminari::Helpers::Paginator.new(template, :param_name => :pagina)
end
subject { @paginator.page_tag(template).instance_variable_get('@param_name') }
it { should == :pagina }
test '#param_name' do
paginator = Paginator.new(template, :param_name => :pagina)
assert_equal :pagina, paginator.page_tag(template).instance_variable_get('@param_name')
end
#TODO test somehow...

View file

@ -0,0 +1,238 @@
# frozen_string_literal: true
require 'test_helper'
if defined?(::Rails::Railtie) && defined?(ActionView)
class TagTest < ActionView::TestCase
sub_test_case '#page_url_for' do
setup do
self.params[:controller] = 'users'
self.params[:action] = 'index'
end
sub_test_case 'for first page' do
test 'by default' do
assert_equal '/users', Kaminari::Helpers::Tag.new(self).page_url_for(1)
end
test 'config.params_on_first_page == false' do
begin
Kaminari.config.params_on_first_page = true
assert_equal '/users?page=1', Kaminari::Helpers::Tag.new(self).page_url_for(1)
ensure
Kaminari.config.params_on_first_page = false
end
end
end
sub_test_case 'with a friendly route setting' do
setup do
self.params[:controller] = 'addresses'
self.params[:action] = 'index'
self.params[:page] = 3
end
sub_test_case 'for first page' do
test 'by default' do
assert_equal('/addresses', Kaminari::Helpers::Tag.new(self).page_url_for(1))
end
test 'config.params_on_first_page == false' do
begin
Kaminari.config.params_on_first_page = true
assert_equal('/addresses/page/1', Kaminari::Helpers::Tag.new(self).page_url_for(1))
ensure
Kaminari.config.params_on_first_page = false
end
end
end
test 'for other page' do
assert_equal('/addresses/page/5', Kaminari::Helpers::Tag.new(self).page_url_for(5))
end
end
sub_test_case "with param_name = 'user[page]' option" do
setup do
self.params[:user] = {:page => '3', :scope => 'active'}
end
test 'for first page' do
assert_not_match(/user%5Bpage%5D=\d+/, Kaminari::Helpers::Tag.new(self, :param_name => 'user[page]').page_url_for(1)) # not match user[page]=\d+
assert_match(/user%5Bscope%5D=active/, Kaminari::Helpers::Tag.new(self, :param_name => 'user[page]').page_url_for(1)) # match user[scope]=active
end
test 'for other page' do
assert_match(/user%5Bpage%5D=2/, Kaminari::Helpers::Tag.new(self, :param_name => 'user[page]').page_url_for(2)) # match user[page]=2
assert_match(/user%5Bscope%5D=active/, Kaminari::Helpers::Tag.new(self, :param_name => 'user[page]').page_url_for(2)) # match user[scope]=active
end
end
sub_test_case "with param_name = 'foo.page' option" do
setup do
self.params['foo.page'] = 2
end
test 'for first page' do
assert_not_match(/foo\.page=\d+/, Kaminari::Helpers::Tag.new(self, :param_name => 'foo.page').page_url_for(1))
end
test 'for other page' do
assert_match(/foo\.page=\d+/, Kaminari::Helpers::Tag.new(self, :param_name => 'foo.page').page_url_for(2))
end
end
end
end
class PaginatorTest < ActionView::TestCase
test '#current?' do
# current_page == page
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 26}, 26, nil).current?
# current_page != page
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 13}, 26, nil).current?
end
test '#first?' do
# page == 1
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 26}, 1, nil).first?
# page != 1
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 13}, 2, nil).first?
end
test '#last?' do
# current_page == page
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 39}, 39, nil).last?
# current_page != page
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 39}, 38, nil).last?
end
test '#next?' do
# page == current_page + 1
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 53, nil).next?
# page != current_page + 1
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 77, nil).next?
end
test '#prev?' do
# page == current_page - 1
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 76, nil).prev?
# page != current_page + 1
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 80, nil).prev?
end
test '#rel' do
# page == current_page - 1
assert_equal 'prev', Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 77}, 76, nil).rel
# page == current_page
assert_nil Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 78}, 78, nil).rel
# page == current_page + 1
assert_equal 'next', Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 52}, 53, nil).rel
end
test '#left_outer?' do
# current_page == left
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 3, nil).left_outer?
# current_page == left + 1
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 4, nil).left_outer?
# current_page == left + 2
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:left => 3}, 5, nil).left_outer?
end
test '#right_outer?' do
# total_pages - page > right
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 6, nil).right_outer?
# total_pages - page == right
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 7, nil).right_outer?
# total_pages - page < right
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 10, :right => 3}, 8, nil).right_outer?
end
sub_test_case '#inside_window?' do
test 'page > current_page' do
# page - current_page > window
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 5}, 10, nil).inside_window?
# page - current_page == window
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 6}, 10, nil).inside_window?
# page - current_page < window
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 4, :window => 7}, 10, nil).inside_window?
end
test 'current_page > page' do
# current_page - page > window
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 4}, 10, nil).inside_window?
# current_page - page == window
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 5}, 10, nil).inside_window?
# current_page - page < window
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:current_page => 15, :window => 6}, 10, nil).inside_window?
end
end
sub_test_case '#was_truncated?' do
setup do
stub(@template = Object.new) do
options { {} }
params { {} }
end
end
test 'last.is_a? Gap' do
assert_true Kaminari::Helpers::Paginator::PageProxy.new({}, 10, Kaminari::Helpers::Gap.new(@template)).was_truncated?
end
test 'last.is not a Gap' do
assert_false Kaminari::Helpers::Paginator::PageProxy.new({}, 10, Kaminari::Helpers::Page.new(@template)).was_truncated?
end
end
sub_test_case '#single_gap?' do
setup do
@window_options = {:left => 1, :window => 1, :right => 1, :total_pages => 9}
end
def gap_for(page)
Kaminari::Helpers::Paginator::PageProxy.new(@window_options, page, nil)
end
test "in case of '1 ... 4 5 6 ... 9'" do
@window_options[:current_page] = 5
assert_false gap_for(2).single_gap?
assert_false gap_for(3).single_gap?
assert_false gap_for(7).single_gap?
assert_false gap_for(8).single_gap?
end
test "in case of '1 ... 3 4 5 ... 9'" do
@window_options[:current_page] = 4
assert_true gap_for(2).single_gap?
assert_false gap_for(6).single_gap?
assert_false gap_for(8).single_gap?
end
test "in case of '1 ... 3 4 5 ... 7'" do
@window_options[:current_page] = 4
@window_options[:total_pages] = 7
assert_true gap_for(2).single_gap?
assert_true gap_for(6).single_gap?
end
test "in case of '1 ... 5 6 7 ... 9'" do
@window_options[:current_page] = 6
assert_false gap_for(2).single_gap?
assert_false gap_for(4).single_gap?
assert_true gap_for(8).single_gap?
end
end
test '#out_of_range?' do
# within range
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 4, nil).out_of_range?
# on last page
assert_false Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 5, nil).out_of_range?
# out of range
assert_true Kaminari::Helpers::Paginator::PageProxy.new({:total_pages => 5}, 6, nil).out_of_range?
end
end
end

View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
require 'test_helper'
if defined? ActiveRecord
class ActiveRecordRelationMethodsTest < ActiveSupport::TestCase
sub_test_case '#total_count' do
setup do
@author = User.create! :name => 'author'
@author2 = User.create! :name => 'author2'
@author3 = User.create! :name => 'author3'
@books = 2.times.map {|i| @author.books_authored.create!(:title => "title%03d" % i) }
@books2 = 3.times.map {|i| @author2.books_authored.create!(:title => "title%03d" % i) }
@books3 = 4.times.map {|i| @author3.books_authored.create!(:title => "subject%03d" % i) }
@readers = 4.times.map { User.create! :name => 'reader' }
@books.each {|book| book.readers << @readers }
end
teardown do
Book.delete_all
User.delete_all
end
test 'it should reset total_count memoization when the scope is cloned' do
assert_equal 1, User.page.tap(&:total_count).where(:name => 'author').total_count
end
test 'it should successfully count the results when the scope includes an order which references a generated column' do
assert_equal @readers.size, @author.readers.by_read_count.page(1).total_count
end
test 'it should keep includes and successfully count the results when the scope use conditions on includes' do
# Only @author and @author2 have books titled with the title00x pattern
assert_equal 2, User.includes(:books_authored).references(:books).where("books.title LIKE 'title00%'").page(1).total_count
end
test 'when the Relation has custom select clause' do
assert_nothing_raised do
User.select('*, 1 as one').page(1).total_count
end
end
test 'it should ignore the options for rails 4.1+ when total_count receives options' do
assert_equal 7, User.page(1).total_count(:name, :distinct => true)
end
test 'it should not throw exception by passing options to count when the scope returns an ActiveSupport::OrderedHash' do
assert_nothing_raised do
@author.readers.by_read_count.page(1).total_count(:name, :distinct => true)
end
end
end
end
end

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
require 'test_helper'
if defined? ActiveRecord
class ActiveRecordModelExtensionTest < ActiveSupport::TestCase
test 'An AR model responds to Kaminari defined methods' do
assert_respond_to Class.new(ActiveRecord::Base), :page
end
test "Kaminari doesn't prevent other AR extension gems to define a method" do
assert_respond_to Class.new(ActiveRecord::Base), :fake_gem_defined_method
end
end
end

View file

@ -0,0 +1,360 @@
# frozen_string_literal: true
require 'test_helper'
if defined? ActiveRecord
class ActiveRecordModelExtensionTest < ActiveSupport::TestCase
test 'Changing page_method_name' do
begin
Kaminari.configure {|config| config.page_method_name = :per_page_kaminari }
model = Class.new ActiveRecord::Base
assert_respond_to model, :per_page_kaminari
assert_not_respond_to model, :page
ensure
Kaminari.configure {|config| config.page_method_name = :page }
end
end
end
class ActiveRecordExtensionTest < ActiveSupport::TestCase
def assert_first_page(relation)
assert_equal 25, relation.count
assert_equal 'user001', relation.first.name
end
def assert_blank_page(relation)
assert_equal 0, relation.count
end
class << self
def startup
[User, GemDefinedModel, Device].each do |m|
1.upto(100) {|i| m.create! :name => "user#{'%03d' % i}", :age => (i / 10)}
end
super
end
def shutdown
[User, GemDefinedModel, Device].each(&:delete_all)
super
end
end
[User, Admin, GemDefinedModel, Device].each do |model_class|
sub_test_case "for #{model_class}" do
sub_test_case '#page' do
test 'page 1' do
assert_first_page model_class.page(1)
end
test 'page 2' do
relation = model_class.page 2
assert_equal 25, relation.count
assert_equal 'user026', relation.first.name
end
test 'page without an argument' do
assert_first_page model_class.page
end
test 'page < 1' do
assert_first_page model_class.page(0)
end
test 'page > max page' do
assert_blank_page model_class.page(5)
end
test 'ensure #order_values is preserved' do
relation = model_class.order('id').page 1
assert_equal ['id'], relation.order_values.uniq
end
end
sub_test_case '#per' do
test 'page 1 per 5' do
relation = model_class.page(1).per(5)
assert_equal 5, relation.count
assert_equal 'user001', relation.first.name
end
test 'page 1 per 5 with max_per_page < 5' do
begin
model_class.max_paginates_per 4
relation = model_class.page(1).per(5)
assert_equal 4, relation.count
ensure
model_class.max_paginates_per nil
end
end
test 'page 1 per nil (using default)' do
assert_equal model_class.default_per_page, model_class.page(1).per(nil).count
end
test 'page 1 per nil with max_per_page < default_per_page' do
begin
model_class.max_paginates_per(10)
assert_equal 10, model_class.page(1).per(nil).count
ensure
model_class.max_paginates_per(nil)
end
end
test 'page 1 per 0' do
assert_equal 0, model_class.page(1).per(0).count
end
# I know it's a bit strange to have this here, but I couldn't find any better place for this case
sub_test_case 'when max_per_page is given via model class, and `per` is not actually called' do
test 'with max_per_page > default_per_page' do
begin
model_class.max_paginates_per(200)
assert_equal 25, model_class.page(1).count
ensure
model_class.max_paginates_per(nil)
end
end
test 'with max_per_page < default_per_page' do
begin
model_class.max_paginates_per(5)
assert_equal 5, model_class.page(1).count
ensure
model_class.max_paginates_per(nil)
end
end
end
end
sub_test_case '#max_paginates_per' do
setup do
model_class.max_paginates_per(10)
end
teardown do
model_class.max_paginates_per(nil)
end
sub_test_case 'calling max_paginates_per() after per()' do
test 'when #max_paginates_per is greater than #per' do
assert_equal 15, model_class.page(1).per(15).max_paginates_per(20).count
end
test 'when #per is greater than #max_paginates_per' do
assert_equal 20, model_class.page(1).per(30).max_paginates_per(20).count
end
test 'when nil is given to #per and #max_paginates_per is specified' do
assert_equal 20, model_class.page(1).per(nil).max_paginates_per(20).count
end
end
sub_test_case 'calling max_paginates_per() before per()' do
test 'when #max_paginates_per is greater than #per' do
assert_equal 15, model_class.page(1).max_paginates_per(20).per(15).count
end
test 'when #per is greater than #max_paginates_per' do
assert_equal 20, model_class.page(1).max_paginates_per(20).per(30).count
end
test 'when nil is given to #per and #max_paginates_per is specified' do
assert_equal 20, model_class.page(1).max_paginates_per(20).per(nil).count
end
end
sub_test_case 'calling max_paginates_per() without per()' do
test 'when #max_paginates_per is greater than the default per_page' do
assert_equal 20, model_class.page(1).max_paginates_per(20).count
end
test 'when #max_paginates_per is less than the default per_page' do
assert_equal 25, model_class.page(1).max_paginates_per(30).count
end
end
end
sub_test_case '#padding' do
test 'page 1 per 5 padding 1' do
relation = model_class.page(1).per(5).padding(1)
assert_equal 5, relation.count
assert_equal 'user002', relation.first.name
end
test 'page 19 per 5 padding 5' do
relation = model_class.page(19).per(5).padding(5)
assert_equal 19, relation.current_page
assert_equal 19, relation.total_pages
end
end
sub_test_case '#total_pages' do
test 'per 25 (default)' do
assert_equal 4, model_class.page.total_pages
end
test 'per 7' do
assert_equal 15, model_class.page(2).per(7).total_pages
end
test 'per 65536' do
assert_equal 1, model_class.page(50).per(65536).total_pages
end
test 'per 0' do
assert_raise Kaminari::ZeroPerPageOperation do
model_class.page(50).per(0).total_pages
end
end
test 'per -1 (using default)' do
assert_equal 4, model_class.page(5).per(-1).total_pages
end
test 'per "String value that can not be converted into Number" (using default)' do
assert_equal 4, model_class.page(5).per('aho').total_pages
end
test 'with max_pages < total pages count from database' do
begin
model_class.max_pages_per 3
assert_equal 3, model_class.page.total_pages
ensure
model_class.max_pages_per nil
end
end
test 'with max_pages > total pages count from database' do
begin
model_class.max_pages_per 11
assert_equal 4, model_class.page.total_pages
ensure
model_class.max_pages_per nil
end
end
test 'with max_pages is nil (default)' do
model_class.max_pages_per nil
assert_equal 4, model_class.page.total_pages
end
test 'with per(nil) using default' do
assert_equal 4, model_class.page.per(nil).total_pages
end
end
sub_test_case '#current_page' do
test 'any page, per 0' do
assert_raise Kaminari::ZeroPerPageOperation do
model_class.page.per(0).current_page
end
end
test 'page 1' do
assert_equal 1, model_class.page.current_page
end
test 'page 2' do
assert_equal 2, model_class.page(2).per(3).current_page
end
end
sub_test_case '#next_page' do
test 'page 1' do
assert_equal 2, model_class.page.next_page
end
test 'page 5' do
assert_nil model_class.page(5).next_page
end
end
sub_test_case '#prev_page' do
test 'page 1' do
assert_nil model_class.page.prev_page
end
test 'page 3' do
assert_equal 2, model_class.page(3).prev_page
end
test 'page 5' do
assert_nil model_class.page(5).prev_page
end
end
sub_test_case '#first_page?' do
test 'on first page' do
assert_true model_class.page(1).per(10).first_page?
end
test 'not on first page' do
assert_false model_class.page(5).per(10).first_page?
end
end
sub_test_case '#last_page?' do
test 'on last page' do
assert_true model_class.page(10).per(10).last_page?
end
test 'within range' do
assert_false model_class.page(1).per(10).last_page?
end
test 'out of range' do
assert_false model_class.page(11).per(10).last_page?
end
end
sub_test_case '#out_of_range?' do
test 'on last page' do
assert_false model_class.page(10).per(10).out_of_range?
end
test 'within range' do
assert_false model_class.page(1).per(10).out_of_range?
end
test 'out of range' do
assert_true model_class.page(11).per(10).out_of_range?
end
end
sub_test_case '#count' do
test 'page 1' do
assert_equal 25, model_class.page.count
end
test 'page 2' do
assert_equal 25, model_class.page(2).count
end
end
test 'chained with .group' do
relation = model_class.group('age').page(2).per 5
# 0..10
assert_equal 11, relation.total_count
assert_equal 3, relation.total_pages
end
test 'activerecord descendants' do
assert_not_equal 0, ActiveRecord::Base.descendants.length
end
end
end
end
end

View file

@ -0,0 +1,172 @@
# frozen_string_literal: true
require 'test_helper'
class PaginatableArrayTest < ActiveSupport::TestCase
setup do
@array = Kaminari::PaginatableArray.new((1..100).to_a)
end
test 'initial state' do
assert_equal 0, Kaminari::PaginatableArray.new.count
end
test 'specifying limit and offset when initializing' do
assert_equal 3, Kaminari::PaginatableArray.new((1..100).to_a, :limit => 10, :offset => 20).current_page
end
sub_test_case '#page' do
def assert_first_page_of_array(arr)
assert_equal 25, arr.count
assert_equal 1, arr.current_page
assert_equal 1, arr.first
end
def assert_blank_array_page(arr)
assert_equal 0, arr.count
end
test 'page 1' do
assert_first_page_of_array @array.page(1)
end
test 'page 2' do
arr = @array.page 2
assert_equal 25, arr.count
assert_equal 2, arr.current_page
assert_equal 26, arr.first
end
test 'page without an argument' do
assert_first_page_of_array @array.page
end
test 'page < 1' do
assert_first_page_of_array @array.page(0)
end
test 'page > max page' do
assert_blank_array_page @array.page(5)
end
end
sub_test_case '#per' do
test 'page 1 per 5' do
arr = @array.page(1).per(5)
assert_equal 5, arr.count
assert_equal 1, arr.first
end
test 'page 1 per 0' do
assert_equal 0, @array.page(1).per(0).count
end
end
sub_test_case '#total_pages' do
test 'per 25 (default)' do
assert_equal 4, @array.page.total_pages
end
test 'per 7' do
assert_equal 15, @array.page(2).per(7).total_pages
end
test 'per 65536' do
assert_equal 1, @array.page(50).per(65536).total_pages
end
test 'per 0' do
assert_raise(Kaminari::ZeroPerPageOperation) { @array.page(50).per(0).total_pages }
end
test 'per -1 (using default)' do
assert_equal 4, @array.page(5).per(-1).total_pages
end
test 'per "String value that can not be converted into Number" (using default)' do
assert_equal 4, @array.page(5).per('aho').total_pages
end
test 'per 25, padding 25' do
assert_equal 3, @array.page(1).padding(25).total_pages
end
end
sub_test_case '#current_page' do
test 'any page, per 0' do
assert_raise(Kaminari::ZeroPerPageOperation) { @array.page.per(0).current_page }
end
test 'page 1' do
assert_equal 1, @array.page(1).current_page
end
test 'page 2' do
assert_equal 2, @array.page(2).per(3).current_page
end
end
sub_test_case '#next_page' do
test 'page 1' do
assert_equal 2, @array.page.next_page
end
test 'page 5' do
assert_nil @array.page(5).next_page
end
end
sub_test_case '#prev_page' do
test 'page 1' do
assert_nil @array.page.prev_page
end
test 'page 3' do
assert_equal 2, @array.page(3).prev_page
end
test 'page 5' do
assert_nil @array.page(5).prev_page
end
end
sub_test_case '#count' do
test 'page 1' do
assert_equal 25, @array.page.count
end
test 'page 2' do
assert_equal 25, @array.page(2).count
end
end
sub_test_case 'when setting total count explicitly' do
test 'array 1..10, page 5, per 10, total_count 9999' do
arr = Kaminari::PaginatableArray.new((1..10).to_a, :total_count => 9999).page(5).per(10)
assert_equal 10, arr.count
assert_equal 1, arr.first
assert_equal 5, arr.current_page
assert_equal 9999, arr.total_count
end
test 'array 1..15, page 1, per 10, total_count 15' do
arr = Kaminari::PaginatableArray.new((1..15).to_a, :total_count => 15).page(1).per(10)
assert_equal 10, arr.count
assert_equal 1, arr.first
assert_equal 1, arr.current_page
assert_equal 15, arr.total_count
end
test 'array 1..25, page 2, per 10, total_count 15' do
arr = Kaminari::PaginatableArray.new((1..25).to_a, :total_count => 15).page(2).per(10)
assert_equal 5, arr.count
assert_equal 11, arr.first
assert_equal 2, arr.current_page
assert_equal 15, arr.total_count
end
end
end

View file

@ -0,0 +1,104 @@
# frozen_string_literal: true
require 'test_helper'
class ConfigurationMethodsTest < ActiveSupport::TestCase
sub_test_case '#default_per_page' do
if defined? ActiveRecord
test 'AR::Base should be not polluted by configuration methods' do
assert_not_respond_to ActiveRecord::Base, :paginates_per
end
end
test 'by default' do
assert_equal 25, User.page(1).limit_value
end
test 'when configuring both on global and model-level' do
Kaminari.configure {|c| c.default_per_page = 50 }
User.paginates_per 100
assert_equal 100, User.page(1).limit_value
end
test 'when configuring multiple times' do
Kaminari.configure {|c| c.default_per_page = 10 }
Kaminari.configure {|c| c.default_per_page = 20 }
assert_equal 20, User.page(1).limit_value
end
teardown do
Kaminari.configure {|c| c.default_per_page = 25 }
User.paginates_per nil
end
end
sub_test_case '#max_per_page' do
teardown do
Kaminari.configure {|c| c.max_per_page = nil }
User.max_paginates_per nil
end
if defined? ActiveRecord
test 'AR::Base should be not polluted by configuration methods' do
assert_not_respond_to ActiveRecord::Base, :max_paginates_per
end
end
test 'by default' do
assert_equal 1000, User.page(1).per(1000).limit_value
end
test 'when configuring both on global and model-level' do
Kaminari.configure {|c| c.max_per_page = 50 }
User.max_paginates_per 100
assert_equal 100, User.page(1).per(1000).limit_value
end
test 'when configuring multiple times' do
Kaminari.configure {|c| c.max_per_page = 10 }
Kaminari.configure {|c| c.max_per_page = 20 }
assert_equal 20, User.page(1).per(1000).limit_value
end
end
sub_test_case '#max_pages' do
if defined? ActiveRecord
test 'AR::Base should be not polluted by configuration methods' do
assert_not_respond_to ActiveRecord::Base, :max_pages_per
end
end
setup do
100.times do |count|
User.create!(:name => "User#{count}")
end
end
teardown do
Kaminari.configure {|c| c.max_pages = nil }
User.max_pages_per nil
User.delete_all
end
test 'by default' do
assert_equal 20, User.page(1).per(5).total_pages
end
test 'when configuring both on global and model-level' do
Kaminari.configure {|c| c.max_pages = 10 }
User.max_pages_per 15
assert_equal 15, User.page(1).per(5).total_pages
end
test 'when configuring multiple times' do
Kaminari.configure {|c| c.max_pages = 10 }
Kaminari.configure {|c| c.max_pages = 15 }
assert_equal 15, User.page(1).per(5).total_pages
end
end
end

View file

@ -1,17 +1,26 @@
# encoding: UTF-8
# frozen_string_literal: true
require 'spec_helper'
require 'test_helper'
feature 'Users' do
background do
class NavigationTest < Test::Unit::TestCase
include Capybara::DSL
setup do
1.upto(100) {|i| User.create! :name => "user#{'%03d' % i}" }
end
scenario 'navigating by pagination links' do
teardown do
Capybara.reset_sessions!
Capybara.use_default_driver
User.delete_all
end
test 'navigating by pagination links' do
visit '/users'
within 'nav.pagination' do
within 'span.page.current' do
page.should have_content '1'
assert page.has_content? '1'
end
within 'span.next' do
click_link 'Next '
@ -20,7 +29,7 @@ feature 'Users' do
within 'nav.pagination' do
within 'span.page.current' do
page.should have_content '2'
assert page.has_content? '2'
end
within 'span.last' do
click_link 'Last »'
@ -29,7 +38,7 @@ feature 'Users' do
within 'nav.pagination' do
within 'span.page.current' do
page.should have_content '4'
assert page.has_content? '4'
end
within 'span.prev' do
click_link ' Prev'
@ -38,7 +47,7 @@ feature 'Users' do
within 'nav.pagination' do
within 'span.page.current' do
page.should have_content '3'
assert page.has_content? '3'
end
within 'span.first' do
click_link '« First'
@ -47,7 +56,7 @@ feature 'Users' do
within 'nav.pagination' do
within 'span.page.current' do
page.should have_content '1'
assert page.has_content? '1'
end
end
end

View file

@ -0,0 +1,25 @@
# encoding: UTF-8
# frozen_string_literal: true
require 'test_helper'
class RenderingWithFormatOptionTest < Test::Unit::TestCase
include Capybara::DSL
setup do
User.create! :name => 'user1'
end
teardown do
Capybara.reset_sessions!
Capybara.use_default_driver
User.delete_all
end
test "Make sure that kaminari doesn't affect the format" do
visit '/users/index_text.text'
assert_equal 200, page.status_code
assert page.has_content? 'partial1'
assert page.has_content? 'partial2'
end
end

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
#
# kaminari repo's tests successfully runs without DatabaseCleaner now.
# DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
# class ActiveSupport::TestCase
# class << self
# def startup
# DatabaseCleaner.clean_with :truncation if defined? ActiveRecord
# super
# end
# end
# setup do
# DatabaseCleaner.start
# end
# teardown do
# DatabaseCleaner.clean
# end
# end

View file

@ -23,10 +23,9 @@ Gem::Specification.new do |s|
s.add_dependency 'kaminari-actionview'
s.add_dependency 'kaminari-activerecord'
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'test-unit-rails'
s.add_development_dependency 'bundler', '>= 1.0.0'
s.add_development_dependency 'rake', '>= 0'
s.add_development_dependency 'rspec', '~> 2.14.1'
s.add_development_dependency 'rr', '>= 0'
s.add_development_dependency 'capybara', '>= 1.0'
s.add_development_dependency 'database_cleaner', '>= 1.4.1'

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'spec'))
$LOAD_PATH.unshift(File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rails'
@ -9,21 +9,14 @@ require 'active_record'
require 'bundler/setup'
Bundler.require
require 'capybara/rspec'
require 'database_cleaner'
# Simulate a gem providing a subclass of ActiveRecord::Base before the Railtie is loaded.
require 'fake_gem'
require 'fake_app/rails_app'
require 'rspec/rails'
require 'test/unit/rails/test_help'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'spec')}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rr
config.filter_run_excluding :generator_spec => true if !ENV['GENERATOR_SPEC']
end
Dir["#{File.join(Gem.loaded_specs['kaminari-core'].gem_dir, 'test')}/support/**/*.rb"].each {|f| require f}