-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsinatra_helpers_test.rb
More file actions
267 lines (224 loc) · 7.13 KB
/
Copy pathsinatra_helpers_test.rb
File metadata and controls
267 lines (224 loc) · 7.13 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# frozen_string_literal: true
require 'test_helper'
ERB_TEMPLATE_FOR_PAGINATE = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= paginate @users, @options %>
</div>
EOT
ERB_TEMPLATE_FOR_PREVIOUS_PAGE = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= link_to_previous_page(@users, "Previous!", {:id => 'previous_page_link'}.merge(@options || {})) %>
</div>
EOT
ERB_TEMPLATE_FOR_PREVIOUS_PAGE_WITH_BLOCK = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= link_to_previous_page(@users, "Previous!", {id: 'previous_page_link'}) do '<span id="no_previous_page">No Previous Page</span>' end %>
</div>
EOT
ERB_TEMPLATE_FOR_NEXT_PAGE = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}.merge(@options || {})) %>
</div>
EOT
ERB_TEMPLATE_FOR_NEXT_PAGE_WITH_BLOCK = <<EOT
<div>
<ul>
<% @users.each do |user| %>
<li class="user_info"><%= user.id %></li>
<% end %>
</ul>
<%= link_to_next_page(@users, "Next!", {:id => 'next_page_link'}) do '<span id="no_next_page">No Next Page</span>' end %>
</div>
EOT
class SinatraHelperTest < ActiveSupport::TestCase
include Rack::Test::Methods
include Sinatra::TestHelpers
def last_document
Nokogiri::HTML(last_response.body)
end
setup do
50.times {|i| User.create! name: "user#{i}"}
end
teardown do
User.delete_all
end
sub_test_case '#paginate' do
setup do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page)
@options = {}
erb ERB_TEMPLATE_FOR_PAGINATE.dup
end
end
end
sub_test_case 'normal paginations with Sinatra' do
setup { get '/users' }
test 'should have a navigation tag' do
assert_not_empty last_document.search('nav.pagination')
end
test 'should have pagination links' do
assert_not_empty last_document.search('.page a')
assert_not_empty last_document.search('.next a')
assert_not_empty last_document.search('.last a')
end
test 'should point to current page' do
assert_match(/1/, last_document.search('.current').text)
get '/users?page=2'
assert_match(/2/, last_document.search('.current').text)
end
test 'should load 25 users' do
assert_equal 25, last_document.search('li.user_info').length
end
test 'should preserve params' do
get '/users?foo=bar'
last_document.search('.page a').each do |elm|
assert_match(/foo=bar/, elm.attribute('href').value)
end
end
end
sub_test_case 'optional paginations with Sinatra' do
test 'should have 5 windows with 1 gap' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(5)
@options = {}
erb ERB_TEMPLATE_FOR_PAGINATE.dup
end
end
get '/users'
assert_equal 6, last_document.search('.page').length
assert_equal 1, last_document.search('.gap').length
end
test 'should control the inner window size' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(3)
@options = {window: 10}
erb ERB_TEMPLATE_FOR_PAGINATE.dup
end
end
get '/users'
assert_equal 12, last_document.search('.page').length
assert_equal 1, last_document.search('.gap').length
end
test 'should specify a page param name' do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page).per(3)
@options = {param_name: :user_page}
erb ERB_TEMPLATE_FOR_PAGINATE.dup
end
end
get '/users'
last_document.search('.page a').each do |elm|
assert_match(/user_page=\d+/, elm.attribute('href').value)
end
end
test 'Include nested query in query' do
get '/users', attribute: [:adult, :children]
assert_match(/users\?attribute%5B%5D=adult&attribute%5B%5D=children/, last_document.search('a').attribute('href').value)
end
end
end
sub_test_case '#link_to_previous_page' do
setup do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 2
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_PREVIOUS_PAGE.dup
end
get '/users_placeholder' do
@page = params[:page] || 2
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_PREVIOUS_PAGE_WITH_BLOCK.dup
end
end
end
sub_test_case 'having more page' do
test 'should have a more page link' do
get '/users'
assert_not_empty last_document.search('a#previous_page_link')
assert_match(/Previous!/, last_document.search('a#previous_page_link').text)
end
end
sub_test_case 'the first page' do
test 'should not have a more page link' do
get '/users?page=1'
assert_empty last_document.search('a#previous_page_link')
end
test 'should have a no more page notation using placeholder' do
get '/users_placeholder?page=1'
assert_empty last_document.search('a#previous_page_link')
assert_not_empty last_document.search('span#no_previous_page')
assert_match(/No Previous Page/, last_document.search('span#no_previous_page').text)
end
end
end
sub_test_case '#link_to_next_page' do
setup do
mock_app do
register Kaminari::Helpers::SinatraHelpers
get '/users' do
@page = params[:page] || 1
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_NEXT_PAGE.dup
end
get '/users_placeholder' do
@page = params[:page] || 1
@users = User.page(@page)
erb ERB_TEMPLATE_FOR_NEXT_PAGE_WITH_BLOCK.dup
end
end
end
sub_test_case 'having more page' do
test 'should have a more page link' do
get '/users'
assert_not_empty last_document.search('a#next_page_link')
assert_match(/Next!/, last_document.search('a#next_page_link').text)
end
end
sub_test_case 'the last page' do
test 'should not have a more page link' do
get '/users?page=2'
assert_empty last_document.search('a#next_page_link')
end
test 'should have a no more page notation using placeholder' do
get '/users_placeholder?page=2'
assert_empty last_document.search('a#next_page_link')
assert_not_empty last_document.search('span#no_next_page')
assert_match(/No Next Page/, last_document.search('span#no_next_page').text)
end
end
end
end