Skip to content

Commit

Permalink
Test coverage for Pagy::Countless
Browse files Browse the repository at this point in the history
[refs #108]
  • Loading branch information
workgena committed Nov 19, 2018
1 parent c2a25b6 commit 2f93227
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/pagy/countless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Pagy

class Countless < Pagy

# Merge and validate the options, do some simple aritmetic and set a few instance variables
# Merge and validate the options, do some simple arithmetic and set a few instance variables
def initialize(vars={})
@vars ||= VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be ovverridden)
@vars ||= VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' }) # default vars + cleaned vars (can be overridden)
{ items:1, outset:0, page:1 }.each do |k,min| # validate instance variables
(@vars[k] && instance_variable_set(:"@#{k}", @vars[k].to_i) >= min) \
or raise(ArgumentError, "expected :#{k} >= #{min}; got #{@vars[k].inspect}")
Expand Down
84 changes: 84 additions & 0 deletions test/pagy/countless_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require_relative '../test_helper'
require 'pagy/extras/countless'

SingleCov.covered!

describe Pagy::Countless do

let(:backend) { TestController.new } # page = 3, items = 20

describe "#finalize" do

before do
@empty_collection = TestCollection.new([])
@collection = TestCollection.new(Array(1..59))
end

let(:last_page) { 3 }

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

it 'first page on 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
pagy.from.must_equal 0
pagy.to.must_equal 0
pagy.prev.must_be_nil
pagy.next.must_be_nil
end

it '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
pagy.from.must_equal 1
pagy.to.must_equal 20
pagy.prev.must_be_nil
pagy.next.must_equal 2
end

it 'when only one full page exists' 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)

pagy.items.must_equal 4
pagy.pages.must_equal 1
pagy.from.must_equal 1
pagy.to.must_equal 4
pagy.prev.must_be_nil
pagy.next.must_be_nil

end

it 'when last page has less records then var[:items]' 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
12 changes: 8 additions & 4 deletions test/pagy/extras/countless_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'byebug'
require_relative '../../test_helper'
require 'pagy/extras/countless'

Expand All @@ -11,9 +10,14 @@
let(:last_page) { 1000 / 20 }

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

after do
Pagy::Countless::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
Expand Down Expand Up @@ -64,21 +68,21 @@
let(:backend) { TestController.new({a: 'a', page: 3, page_number: 4}) }

it 'page_param from defaults' do
Pagy::VARS[:page_param] = :page_number
Pagy::Countless::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::VARS[:page_param] = :page
Pagy::Countless::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::VARS[:page_param] = :page_number
Pagy::Countless::VARS[:page_param] = :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)
Expand Down

0 comments on commit 2f93227

Please sign in to comment.