Skip to content

Commit

Permalink
Add some tests for Countless extra
Browse files Browse the repository at this point in the history
[refs #108]
  • Loading branch information
workgena committed Nov 19, 2018
1 parent 04fa7a2 commit c2a25b6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 8 deletions.
2 changes: 2 additions & 0 deletions docs/extras/countless.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ In a controller:
@pagy, @records = pagy_countless(some_scope, ...)
```

**Notice**: When using `ActiveRecord`, the `@records` is an `Array`. Onthe other side, the normal `Pagy` returns an `ActiveRelation`.

## Files

This extra is composed of 1 file:
Expand Down
88 changes: 88 additions & 0 deletions test/pagy/extras/countless_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'byebug'
require_relative '../../test_helper'
require 'pagy/extras/countless'

SingleCov.covered!

describe Pagy::Backend do

let(:backend) { TestController.new }

let(:last_page) { 1000 / 20 }

before do
@collection = TestCollection.new((1..1000).to_a)
end

describe "#pagy_countless" do

it 'vars[:size] = [1, 4, 4, 1], on first page only show Next 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
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
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
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
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
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

let(:backend) { TestController.new({a: 'a', page: 3, page_number: 4}) }

it '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::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, 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
16 changes: 8 additions & 8 deletions test/pagy/extras/items_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 15
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 15
end

Expand All @@ -41,7 +41,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 12
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 12
end

Expand All @@ -52,7 +52,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 21
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 21
end

Expand All @@ -63,7 +63,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 100
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 100
end

Expand All @@ -74,7 +74,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 1000
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 1000
end

Expand All @@ -87,7 +87,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 1000
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 1000
}
end
Expand All @@ -99,7 +99,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 14
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 14
end

Expand All @@ -112,7 +112,7 @@
backend.params.must_equal params
pagy, _ = backend.send :pagy, @collection, vars
pagy.items.must_equal 15
pagy_countless, _ = backend.send :pagy, @collection, vars
pagy_countless, _ = backend.send :pagy_countless, @collection, vars
pagy_countless.items.must_equal 15
}
end
Expand Down

0 comments on commit c2a25b6

Please sign in to comment.