diff --git a/kaminari-core/test/helpers/helpers_test.rb b/kaminari-core/test/helpers/helpers_test.rb index e3501e3..adcf966 100644 --- a/kaminari-core/test/helpers/helpers_test.rb +++ b/kaminari-core/test/helpers/helpers_test.rb @@ -43,109 +43,4 @@ class PaginatorHelperTest < ActiveSupport::TestCase paginator = Paginator.new(template, param_name: :pagina) assert_equal :pagina, paginator.page_tag(template).instance_variable_get('@param_name') end - - #TODO test somehow... -# describe '#tagify_links' do -# def tags_with(options) -# PaginationRenderer.new(template, options).tagify_links -# end - -# context '1 page in total' do -# subject { tags_with :total_pages => 1, :current_page => 1 } -# it { should have(0).tags } -# end - -# context '10 pages in total' do -# context 'first page' do -# subject { tags_with :total_pages => 10, :current_page => 1 } -# it { should_not contain_tag PrevLink } -# it { should contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should_not contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should contain_tag TruncatedSpan } -# end - -# context 'second page' do -# subject { tags_with :total_pages => 10, :current_page => 2 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should contain_tag TruncatedSpan } -# end - -# context 'third page' do -# subject { tags_with :total_pages => 10, :current_page => 3 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should contain_tag TruncatedSpan } -# end - -# context 'fourth page(no truncation)' do -# subject { tags_with :total_pages => 10, :current_page => 4 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should_not contain_tag TruncatedSpan } -# end - -# context 'seventh page(no truncation)' do -# subject { tags_with :total_pages => 10, :current_page => 7 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should_not contain_tag TruncatedSpan } -# end - -# context 'eighth page' do -# subject { tags_with :total_pages => 10, :current_page => 8 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should contain_tag NextLink } -# it { should_not contain_tag NextSpan } -# it { should contain_tag TruncatedSpan } -# end - -# context 'last page' do -# subject { tags_with :total_pages => 10, :current_page => 10 } -# it { should contain_tag PrevLink } -# it { should_not contain_tag PrevSpan } -# it { should contain_tag CurrentPage } -# it { should contain_tag FirstPageLink } -# it { should_not contain_tag LastPageLink } -# it { should contain_tag PageLink } -# it { should_not contain_tag NextLink } -# it { should contain_tag NextSpan } -# it { should contain_tag TruncatedSpan } -# end -# end -# end end diff --git a/kaminari-core/test/helpers/paginator_tags_test.rb b/kaminari-core/test/helpers/paginator_tags_test.rb new file mode 100644 index 0000000..021a881 --- /dev/null +++ b/kaminari-core/test/helpers/paginator_tags_test.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true +require 'test_helper' + +class PaginatorTagsTest < ActionView::TestCase + # A test paginator that can detect instantiated tags inside + class TagSpy < Kaminari::Helpers::Paginator + def initialize(*) + super + @tags = [] + end + + def page_tag(page) + @tags << page.to_i + super + end + + %w[first_page prev_page next_page last_page gap].each do |tag| + eval <<-DEF, nil, __FILE__, __LINE__ + 1 + def #{tag}_tag + @tags << :#{tag} + super + end + DEF + end + + def partial_path + 'kaminari/paginator' + end + + def to_s + super + @tags + end + end + + def tags_for(collection, window: 4, outer_window: 0) + view.paginate(collection, paginator_class: TagSpy, window: window, outer_window: outer_window, params: {controller: 'users', action: 'index'}) + end + + teardown do + User.delete_all + end + + test '1 page in total' do + 3.times {|i| User.create! name: "user#{i}"} + assert_empty tags_for(User.page(1).per(3)) + end + + sub_test_case 'when having 1 outer_window (and 1 inner window)' do + def tags_for(collection, window: 1, outer_window: 1) + super + end + + test '10 pages in total' do + 20.times {|i| User.create! name: "user#{i}"} + + assert_equal [1, 2, :gap, 10, :next_page, :last_page], tags_for(User.page(1).per(2)) + assert_equal [:first_page, :prev_page, 1, 2, 3, :gap, 10, :next_page, :last_page], tags_for(User.page(2).per(2)) + assert_equal [:first_page, :prev_page, 1, 2, 3, 4, :gap, 10, :next_page, :last_page], tags_for(User.page(3).per(2)) + # the 3rd page doesn't become a gap because it's a single gap + assert_equal [:first_page, :prev_page, 1, 2, 3, 4, 5, :gap, 10, :next_page, :last_page], tags_for(User.page(4).per(2)) + assert_equal [:first_page, :prev_page, 1, :gap, 4, 5, 6, :gap, 10, :next_page, :last_page], tags_for(User.page(5).per(2)) + assert_equal [:first_page, :prev_page, 1, :gap, 5, 6, 7, :gap, 10, :next_page, :last_page], tags_for(User.page(6).per(2)) + # the 9th page doesn't become a gap because it's a single gap + assert_equal [:first_page, :prev_page, 1, :gap, 6, 7, 8, 9, 10, :next_page, :last_page], tags_for(User.page(7).per(2)) + assert_equal [:first_page, :prev_page, 1, :gap, 7, 8, 9, 10, :next_page, :last_page], tags_for(User.page(8).per(2)) + assert_equal [:first_page, :prev_page, 1, :gap, 8, 9, 10, :next_page, :last_page], tags_for(User.page(9).per(2)) + assert_equal [:first_page, :prev_page, 1, :gap, 9, 10], tags_for(User.page(10).per(2)) + end + end +end