forked from mislav/will_paginate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_spec.rb
131 lines (106 loc) · 3.79 KB
/
collection_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'will_paginate/array'
require 'spec_helper'
describe WillPaginate::Collection do
before :all do
@simple = ('a'..'e').to_a
end
it "should be a subset of original collection" do
@simple.paginate(:page => 1, :per_page => 3).should == %w( a b c )
end
it "can be shorter than per_page if on last page" do
@simple.paginate(:page => 2, :per_page => 3).should == %w( d e )
end
it "should include whole collection if per_page permits" do
@simple.paginate(:page => 1, :per_page => 5).should == @simple
end
it "should be empty if out of bounds" do
@simple.paginate(:page => 2, :per_page => 5).should be_empty
end
it "should default to 1 as current page and 30 per-page" do
result = (1..50).to_a.paginate
result.current_page.should == 1
result.size.should == 30
end
it "should give total_entries precedence over actual size" do
%w(a b c).paginate(:total_entries => 5).total_entries.should == 5
end
it "should be an augmented Array" do
entries = %w(a b c)
collection = create(2, 3, 10) do |pager|
pager.replace(entries).should == entries
end
collection.should == entries
for method in %w(total_pages each offset size current_page per_page total_entries)
collection.should respond_to(method)
end
collection.should be_kind_of(Array)
collection.entries.should be_instance_of(Array)
# TODO: move to another expectation:
collection.offset.should == 3
collection.total_pages.should == 4
collection.should_not be_out_of_bounds
end
describe "previous/next pages" do
it "should have previous_page nil when on first page" do
collection = create(1, 1, 3)
collection.previous_page.should be_nil
collection.next_page.should == 2
end
it "should have both prev/next pages" do
collection = create(2, 1, 3)
collection.previous_page.should == 1
collection.next_page.should == 3
end
it "should have next_page nil when on last page" do
collection = create(3, 1, 3)
collection.previous_page.should == 2
collection.next_page.should be_nil
end
end
it "should show out of bounds when page number is too high" do
create(2, 3, 2).should be_out_of_bounds
end
it "should not show out of bounds when inside collection" do
create(1, 3, 2).should_not be_out_of_bounds
end
describe "guessing total count" do
it "can guess when collection is shorter than limit" do
collection = create { |p| p.replace array }
collection.total_entries.should == 8
end
it "should allow explicit total count to override guessed" do
collection = create(2, 5, 10) { |p| p.replace array }
collection.total_entries.should == 10
end
it "should not be able to guess when collection is same as limit" do
collection = create { |p| p.replace array(5) }
collection.total_entries.should be_nil
end
it "should not be able to guess when collection is empty" do
collection = create { |p| p.replace array(0) }
collection.total_entries.should be_nil
end
it "should be able to guess when collection is empty and this is the first page" do
collection = create(1) { |p| p.replace array(0) }
collection.total_entries.should == 0
end
end
it "should not respond to page_count anymore" do
Proc.new { create.page_count }.should raise_error(NoMethodError)
end
it "inherits per_page from global value" do
collection = described_class.new(1)
collection.per_page.should == 30
end
private
def create(page = 2, limit = 5, total = nil, &block)
if block_given?
described_class.create(page, limit, total, &block)
else
described_class.new(page, limit, total)
end
end
def array(size = 3)
Array.new(size)
end
end