forked from jekyll/jekyll-paginate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager_spec.rb
178 lines (144 loc) · 5.76 KB
/
pager_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'spec_helper'
RSpec.describe(Jekyll::Paginate::Pager) do
it "calculate number of pages" do
expect(described_class.calculate_pages([], '2')).to eql(0)
expect(described_class.calculate_pages([1], '2')).to eql(1)
expect(described_class.calculate_pages([1,2], '2')).to eql(1)
expect(described_class.calculate_pages([1,2,3], '2')).to eql(2)
expect(described_class.calculate_pages([1,2,3,4], '2')).to eql(2)
expect(described_class.calculate_pages([1,2,3,4,5], '2')).to eql(3)
end
context "with the default paginate_path" do
let(:site) { build_site }
it "determines the correct pagination path for each page" do
if Jekyll::VERSION < '3.0.0'
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
else
expect(described_class.paginate_path(site, 1)).to eql("/")
end
expect(described_class.paginate_path(site, 2)).to eql("/page2")
end
end
context "with paginate_path set to a subdirectory with no index.html" do
let(:site) { build_site({'paginate_path' => '/blog/page-:num'}) }
it "determines the correct pagination path for each page" do
if Jekyll::VERSION < '3.0.0'
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
else
expect(described_class.paginate_path(site, 1)).to eql("/")
end
expect(described_class.paginate_path(site, 2)).to eql("/blog/page-2")
end
end
context "with paginate_path set to a subdirectory with no index.html with num pages being in subdirectories" do
let(:site) { build_site({'paginate_path' => '/blog/page/:num'}) }
it "determines the correct pagination path for each page" do
if Jekyll::VERSION < '3.0.0'
expect(described_class.paginate_path(site, 1)).to eql("/index.html")
else
expect(described_class.paginate_path(site, 1)).to eql("/")
end
expect(described_class.paginate_path(site, 2)).to eql("/blog/page/2")
end
end
context "with paginate_path set to a subdirectory wherein an index.html exists" do
let(:site) { build_site({'paginate_path' => '/contacts/page:num'}) }
it "determines the correct pagination path for each page" do
if Jekyll::VERSION < '3.0.0'
expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html")
else
expect(described_class.paginate_path(site, 1)).to eql("/contacts/")
end
expect(described_class.paginate_path(site, 2)).to eql("/contacts/page2")
end
end
context "with paginate_path set to a subdir wherein an index.html exists with pages in subdirs" do
let(:site) { build_site({'paginate_path' => '/contacts/page/:num'}) }
it "determines the correct pagination path for each page" do
if Jekyll::VERSION < '3.0.0'
expect(described_class.paginate_path(site, 1)).to eql("/contacts/index.html")
else
expect(described_class.paginate_path(site, 1)).to eql("/contacts/")
end
expect(described_class.paginate_path(site, 2)).to eql("/contacts/page/2")
end
end
context "with an paginate_path devoid of :num" do
let(:site) { build_site({'paginate_path' => '/blog/page'}) }
it "determines the correct pagination path for each page" do
expect(-> { described_class.paginate_path(site, 1) }).to raise_error
end
end
context "pagination disabled" do
let(:site) { build_site('paginate' => nil) }
it "report that pagination is disabled" do
expect(described_class.pagination_enabled?(site)).to be_falsey
end
end
context "pagination enabled for 2" do
let(:site) { build_site('paginate' => 2) }
if Jekyll::VERSION < '3.0.0'
let(:posts) { site.posts }
else
let(:posts) { site.posts.docs }
end
it "report that pagination is enabled" do
expect(described_class.pagination_enabled?(site)).to be_truthy
end
context "with 4 posts" do
if Jekyll::VERSION < '3.0.0'
let(:posts) { site.posts[1..4] }
else
let(:posts) { site.posts.docs[1..4] }
end
it "create first pager" do
pager = described_class.new(site, 1, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(2)
expect(pager.previous_page).to be_nil
expect(pager.next_page).to eql(2)
end
it "create second pager" do
pager = described_class.new(site, 2, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(2)
expect(pager.previous_page).to eql(1)
expect(pager.next_page).to be_nil
end
it "not create third pager" do
expect { described_class.new(site, 3, posts) }.to raise_error
end
end
context "with 5 posts" do
if Jekyll::VERSION < '3.0.0'
let(:posts) { site.posts[1..5] }
else
let(:posts) { site.posts.docs[1..5] }
end
it "create first pager" do
pager = described_class.new(site, 1, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to be_nil
expect(pager.next_page).to eql(2)
end
it "create second pager" do
pager = described_class.new(site, 2, posts)
expect(pager.posts.size).to eql(2)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to eql(1)
expect(pager.next_page).to eql(3)
end
it "create third pager" do
pager = described_class.new(site, 3, posts)
expect(pager.posts.size).to eql(1)
expect(pager.total_pages).to eql(3)
expect(pager.previous_page).to eql(2)
expect(pager.next_page).to be_nil
end
it "not create fourth pager" do
expect { described_class.new(site, 4, posts) }.to raise_error(RuntimeError)
end
end
end
end