Skip to content

Commit

Permalink
converted tests to spec syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ddnexus committed Jul 1, 2018
1 parent 0422568 commit b14f446
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 754 deletions.
86 changes: 42 additions & 44 deletions test/pagy/backend_test.rb
Expand Up @@ -7,87 +7,85 @@
let(:backend) { TestController.new }

describe "#pagy" do

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

def test_pagy_method_with_default
it 'paginates with defaults' do
pagy, records = backend.send(:pagy, @collection)

assert_instance_of Pagy, pagy
assert_equal 1000, pagy.count
assert_equal Pagy::VARS[:items], pagy.items
assert_equal backend.params[:page], pagy.page

assert_equal Pagy::VARS[:items], records.count
assert_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60], records
pagy.must_be_instance_of Pagy
pagy.count.must_equal 1000
pagy.items.must_equal Pagy::VARS[:items]
pagy.page.must_equal backend.params[:page]
records.count.must_equal Pagy::VARS[:items]
records.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
end

def test_pagy_method_with_vars
it 'paginates with vars' do
pagy, records = backend.send(:pagy, @collection, page: 2, items: 10, link_extra: 'X')

assert_instance_of Pagy, pagy
assert_equal 1000, pagy.count
assert_equal 10, pagy.items
assert_equal 2, pagy.page
assert_equal 'X', pagy.vars[:link_extra]

assert_equal 10, records.count
assert_equal [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], records
pagy.must_be_instance_of Pagy
pagy.count.must_equal 1000
pagy.items.must_equal 10
pagy.page.must_equal 2
pagy.vars[:link_extra].must_equal 'X'
records.count.must_equal 10
records.must_equal [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
end

end

describe "#pagy_get_vars" do

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

def test_pagy_get_vars_with_default
it 'gets defaults' do
vars = {}
merged = backend.send :pagy_get_vars, @collection, vars
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_equal(1000, merged[:count])
assert_equal(3, merged[:page])
merged.keys.must_include :count
merged.keys.must_include :page
merged[:count].must_equal 1000
merged[:page].must_equal 3
end

def test_pagy_get_vars_with_vars
it 'gets vars' do
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_get_vars, @collection, vars
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_includes(merged.keys, :items)
assert_includes(merged.keys, :link_extra)
assert_equal(1000, merged[:count])
assert_equal(2, merged[:page])
assert_equal(10, merged[:items])
assert_equal('X', merged[:link_extra])
merged.keys.must_include :count
merged.keys.must_include :page
merged.keys.must_include :items
merged.keys.must_include :link_extra
merged[:count].must_equal 1000
merged[:page].must_equal 2
merged[:items].must_equal 10
merged[:link_extra].must_equal 'X'
end

def test_pagy_get_vars_with_grouped_collection
it 'works with grouped collections' do
@collection = TestGroupedCollection.new((1..1000).to_a)
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_get_vars, @collection, vars
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_includes(merged.keys, :items)
assert_includes(merged.keys, :link_extra)
assert_equal(1000, merged[:count])
assert_equal(2, merged[:page])
assert_equal(10, merged[:items])
assert_equal('X', merged[:link_extra])
merged.keys.must_include :count
merged.keys.must_include :page
merged.keys.must_include :items
merged.keys.must_include :link_extra
merged[:count].must_equal 1000
merged[:page].must_equal 2
merged[:items].must_equal 10
merged[:link_extra].must_equal 'X'
end

end

describe "#pagy_get_items" do

def test_pagy_get_items
it 'gets items' do
collection = TestCollection.new((1..1000).to_a)
pagy = Pagy.new count: 1000
items = backend.send :pagy_get_items, collection, pagy
assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], items
items.must_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
end

end
Expand Down
69 changes: 33 additions & 36 deletions test/pagy/extras/array_test.rb
Expand Up @@ -5,66 +5,63 @@

describe Pagy::Backend do


let(:backend) { TestController.new }

describe "#pagy_array" do

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

def test_extra_pagy_array_with_default
pagy, records = backend.send(:pagy_array, @collection)

assert_instance_of Pagy, pagy
assert_equal 1000, pagy.count
assert_equal Pagy::VARS[:items], pagy.items
assert_equal backend.params[:page], pagy.page

assert_equal Pagy::VARS[:items], records.count
assert_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60], records
it 'paginates with defaults' do
pagy, items = backend.send(:pagy_array, @collection)
pagy.must_be_instance_of Pagy
pagy.count.must_equal 1000
pagy.items.must_equal Pagy::VARS[:items]
pagy.page.must_equal backend.params[:page]
items.count.must_equal Pagy::VARS[:items]
items.must_equal [41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
end

def test_extra_pagy_array_with_vars
pagy, records = backend.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X')

assert_instance_of Pagy, pagy
assert_equal 1000, pagy.count
assert_equal 10, pagy.items
assert_equal 2, pagy.page
assert_equal 'X', pagy.vars[:link_extra]

assert_equal 10, records.count
assert_equal [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], records
it 'paginates with vars' do
pagy, items = backend.send(:pagy_array, @collection, page: 2, items: 10, link_extra: 'X')
pagy.must_be_instance_of Pagy
pagy.count.must_equal 1000
pagy.items.must_equal 10
pagy.page.must_equal 2
pagy.vars[:link_extra].must_equal 'X'
items.count.must_equal 10
items.must_equal [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
end

end

describe "#pagy_array_get_vars" do

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

def test_extra_pagy_array_get_vars_with_default
it 'gets defaults' do
vars = {}
merged = backend.send :pagy_array_get_vars, @collection, vars
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_equal(1000, merged[:count])
assert_equal(3, merged[:page])
merged.keys.must_include :count
merged.keys.must_include :page
merged[:count].must_equal 1000
merged[:page].must_equal 3
end

def test_extra_pagy_array_get_vars_with_vars
it 'gets vars' do
vars = {page: 2, items: 10, link_extra: 'X'}
merged = backend.send :pagy_array_get_vars, @collection, vars
assert_includes(merged.keys, :count)
assert_includes(merged.keys, :page)
assert_includes(merged.keys, :items)
assert_includes(merged.keys, :link_extra)
assert_equal(1000, merged[:count])
assert_equal(2, merged[:page])
assert_equal(10, merged[:items])
assert_equal('X', merged[:link_extra])
merged.keys.must_include :count
merged.keys.must_include :page
merged.keys.must_include :items
merged.keys.must_include :link_extra
merged[:count].must_equal 1000
merged[:page].must_equal 2
merged[:items].must_equal 10
merged[:link_extra].must_equal 'X'
end

end
Expand Down
63 changes: 26 additions & 37 deletions test/pagy/extras/bootstrap_test.rb
Expand Up @@ -8,34 +8,31 @@
let(:frontend) { TestView.new }

describe "#pagy_nav_bootstrap" do

before do
@array = (1..103).to_a.extend(Pagy::Array::PageMethod)
end

def test_pagy_nav_bootstrap_page_1
it 'renders page 1' do
pagy, _ = @array.pagy(1)

assert_equal(
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
'<ul class="pagination">' \
'<li class="page-item prev disabled"><a href="#" class="page-link">&lsaquo;&nbsp;Prev</a></li>' \
'<li class="page-item active"><a href="/foo?page=1" class="page-link" >1</a></li>'\
'<li class="page-item"><a href="/foo?page=2" class="page-link" rel="next" >2</a></li>' \
'<li class="page-item"><a href="/foo?page=3" class="page-link" >3</a></li>' \
'<li class="page-item"><a href="/foo?page=4" class="page-link" >4</a></li>' \
'<li class="page-item"><a href="/foo?page=5" class="page-link" >5</a></li>' \
'<li class="page-item"><a href="/foo?page=6" class="page-link" >6</a></li>' \
'<li class="page-item next"><a href="/foo?page=2" class="page-link" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></li>' \
'</ul>' \
'</nav>',
frontend.pagy_nav_bootstrap(pagy)
)
frontend.pagy_nav_bootstrap(pagy).must_equal \
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
'<ul class="pagination">' \
'<li class="page-item prev disabled"><a href="#" class="page-link">&lsaquo;&nbsp;Prev</a></li>' \
'<li class="page-item active"><a href="/foo?page=1" class="page-link" >1</a></li>'\
'<li class="page-item"><a href="/foo?page=2" class="page-link" rel="next" >2</a></li>' \
'<li class="page-item"><a href="/foo?page=3" class="page-link" >3</a></li>' \
'<li class="page-item"><a href="/foo?page=4" class="page-link" >4</a></li>' \
'<li class="page-item"><a href="/foo?page=5" class="page-link" >5</a></li>' \
'<li class="page-item"><a href="/foo?page=6" class="page-link" >6</a></li>' \
'<li class="page-item next"><a href="/foo?page=2" class="page-link" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></li>' \
'</ul>' \
'</nav>'
end

def test_pagy_nav_bootstrap_page_3
it 'renders page 3' do
pagy, _ = @array.pagy(3)

assert_equal(
frontend.pagy_nav_bootstrap(pagy).must_equal \
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
'<ul class="pagination">' \
'<li class="page-item prev"><a href="/foo?page=2" class="page-link" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></li>' \
Expand All @@ -47,17 +44,14 @@ def test_pagy_nav_bootstrap_page_3
'<li class="page-item"><a href="/foo?page=6" class="page-link" >6</a></li>' \
'<li class="page-item next"><a href="/foo?page=4" class="page-link" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></li>' \
'</ul>' \
'</nav>',
frontend.pagy_nav_bootstrap(pagy)
)
'</nav>'
end


def test_pagy_nav_bootstrap_page_6
it 'renders page 6' do
pagy, _ = @array.pagy(6)

assert_equal(
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
frontend.pagy_nav_bootstrap(pagy).must_equal \
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
'<ul class="pagination">' \
'<li class="page-item prev"><a href="/foo?page=5" class="page-link" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></li>' \
'<li class="page-item"><a href="/foo?page=1" class="page-link" >1</a></li>' \
Expand All @@ -68,16 +62,13 @@ def test_pagy_nav_bootstrap_page_6
'<li class="page-item active"><a href="/foo?page=6" class="page-link" >6</a></li>' \
'<li class="page-item next disabled"><a href="#" class="page-link">Next&nbsp;&rsaquo;</a></li>' \
'</ul>' \
'</nav>',
frontend.pagy_nav_bootstrap(pagy)
)
'</nav>'
end

def test_pagy_nav_bootstrap_page_10
it 'renders page 10' do
@array = (1..1000).to_a.extend(Pagy::Array::PageMethod)
pagy, _ = @array.pagy(10)

assert_equal(
frontend.pagy_nav_bootstrap(pagy).must_equal \
'<nav class="pagy-nav-bootstrap pagination" role="navigation" aria-label="pager">' \
'<ul class="pagination">' \
'<li class="page-item prev"><a href="/foo?page=9" class="page-link" rel="prev" aria-label="previous">&lsaquo;&nbsp;Prev</a></li>' \
Expand All @@ -96,11 +87,9 @@ def test_pagy_nav_bootstrap_page_10
'<li class="page-item"><a href="/foo?page=50" class="page-link" >50</a></li>' \
'<li class="page-item next"><a href="/foo?page=11" class="page-link" rel="next" aria-label="next">Next&nbsp;&rsaquo;</a></li>' \
'</ul>' \
'</nav>',
frontend.pagy_nav_bootstrap(pagy)
)
'</nav>'
end


end

end

0 comments on commit b14f446

Please sign in to comment.