Skip to content

Commit

Permalink
a few fixes for the countless tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Nov 20, 2018
1 parent 0bf2f46 commit 1b8a080
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
21 changes: 8 additions & 13 deletions test/pagy/countless_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

let(:last_page) { 3 }

it 'jump to 2 page with empty collection' do
it 'raises exception with no retrieved items' do
proc { Pagy::Countless.new(page: 2).finalize(0) }.must_raise Pagy::OverflowError
end

it 'first page on empty collection' do
it 'initializes empty collection' do
pagy, _ = backend.send(:pagy_countless, @empty_collection, page: 1)

pagy.items.must_equal 20
pagy.pages.must_equal 1
pagy.last.must_equal 1
Expand All @@ -32,10 +31,9 @@
pagy.next.must_be_nil
end

it 'first page' do
it 'initializes first page' do
pagy, _ = backend.send(:pagy_countless, @collection, page: 1)
pagy.must_be_instance_of Pagy::Countless

pagy.items.must_equal 20
pagy.last.must_equal 2
pagy.pages.must_equal 2 # current + 1. `Countless` does not know real count
Expand All @@ -45,20 +43,18 @@
pagy.next.must_equal 2
end

it 'when only one full page exists' do
it 'initializes single full page' do
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..20)), page: 1)

pagy.items.must_equal 20
pagy.pages.must_equal 1
pagy.from.must_equal 1
pagy.to.must_equal 20
pagy.prev.must_be_nil
pagy.next.must_be_nil
end

it 'when only one not full page exists' do
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..4)), page: 1)

it 'initialize single partial page' do
pagy, _ = backend.send(:pagy_countless, TestCollection.new(Array(1..4)), page: 1)
pagy.items.must_equal 4
pagy.pages.must_equal 1
pagy.from.must_equal 1
Expand All @@ -68,17 +64,16 @@

end

it 'when last page has less records then var[:items]' do
it 'initializes last partial page' do
pagy, _ = backend.send(:pagy_countless, @collection, page: last_page)

pagy.items.must_equal 19
pagy.pages.must_equal last_page
pagy.from.must_equal 41
pagy.to.must_equal 59
pagy.prev.must_equal(last_page - 1)
pagy.next.must_be_nil
end

end

end
34 changes: 17 additions & 17 deletions test/pagy/extras/countless_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,83 @@
let(:last_page) { 1000 / 20 }

before do
@default_page_param = Pagy::Countless::VARS[:page_param]
@default_page_param = Pagy::VARS[:page_param]
@collection = TestCollection.new((1..1000).to_a)
end

after do
Pagy::Countless::VARS[:page_param] = @default_page_param
Pagy::VARS[:page_param] = @default_page_param
end

describe "#pagy_countless" do

it 'vars[:size] = [1, 4, 4, 1], on first page only show Next page' do
it 'shows current and next for first page' do
pagy, _ = backend.send(:pagy_countless, @collection, { size: [1, 4, 4, 1], page: 1 })
pagy.series.must_equal ['1', 2]
pagy.prev.must_be_nil
pagy.next.must_equal 2
end

it 'vars[:size] = [1, 4, 4, 1] on 25 page, show one next page and previous pages with :gap' do
it 'shows start-pages, :gap, before-pages, current and next for intermediate page' do
pagy, _ = backend.send(:pagy_countless, @collection, {page: 25})
pagy.series.must_equal [1, :gap, 21, 22, 23, 24, '25', 26]
pagy.prev.must_equal 24
pagy.next.must_equal 26
end

it 'vars[:size] = [1, 4, 4, 1] on last page show previous pages with :gap' do
it 'shows start-pages, :gap, before-pages, current and next for last page' do
pagy, _ = backend.send(:pagy_countless, @collection, {page: last_page})
pagy.series.must_equal [1, :gap, 46, 47, 48, 49, '50']
pagy.prev.must_equal 49
pagy.next.must_be_nil
end

it 'vars[:size] = [], on first page, should hide seris' do
it 'returns empty series for empty :size variable for first page' do
pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: 1})
pagy.series.must_equal []
pagy.prev.must_be_nil
pagy.next.must_equal 2
end

it 'vars[:size] = [], in the middle, should hide seris' do
it 'returns empty series for empty :size variable for intermediate page' do
pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: 25})
pagy.series.must_equal []
pagy.prev.must_equal 24
pagy.next.must_equal 26
end

it 'vars[:size] = [], on last page, should hide seris' do
it 'returns empty series for empty :size variable for last page' do
pagy, _ = backend.send(:pagy_countless, @collection, {size: [], page: last_page})
pagy.series.must_equal []
pagy.prev.must_equal 49
pagy.next.must_be_nil
end
end

describe 'page_param in privete method "pagy_countless_get_vars"' do
describe '#pagy_countless_get_vars' do

let(:backend) { TestController.new({a: 'a', page: 3, page_number: 4}) }
it 'page_param from defaults' do
Pagy::Countless::VARS[:page_param] = :page_number

it 'sets :page_param from defaults' do
Pagy::VARS[:page_param] = :page_number
pagy, items = backend.send(:pagy_countless, @collection)
pagy.page.must_equal 4
items.must_equal Array(61..80)
end

it 'page_param from vars' do
Pagy::Countless::VARS[:page_param] = :page
it 'sets :page_param from vars' do
Pagy::VARS[:page_param] = :page
pagy, items = backend.send(:pagy_countless, @collection, {page_param: :page_number})
pagy.page.must_equal 4
items.must_equal Array(61..80)
end

it 'bypass page_param with vars[:page]' do
Pagy::Countless::VARS[:page_param] = :page_number
it 'bypasses :page_param with :page variable' do
Pagy::VARS[:page_param] = :another_page_number
pagy, items = backend.send(:pagy_countless, @collection, {page_param: :page_number, page: 1})
pagy.page.must_equal 1
items.must_equal Array(1..20)
end

end
end

0 comments on commit 1b8a080

Please sign in to comment.