/
action_view_spec.rb
362 lines (294 loc) · 9.9 KB
/
action_view_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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require 'spec_helper'
require 'active_support/rescuable' # needed for Ruby 1.9.1
require 'action_controller'
require 'will_paginate/view_helpers/action_view'
require 'will_paginate/collection'
ActionView::Base.send(:include, WillPaginate::ActionView)
Routes = ActionDispatch::Routing::RouteSet.new
Routes.draw do
match 'dummy/page/:page' => 'dummy#index'
match 'dummy/dots/page.:page' => 'dummy#dots'
match 'ibocorp(/:page)' => 'ibocorp#index',
:constraints => { :page => /\d+/ }, :defaults => { :page => 1 }
match ':controller(/:action(/:id(.:format)))'
end
describe WillPaginate::ActionView do
before(:all) do
I18n.load_path << File.expand_path('../../../lib/will_paginate/locale/en.yml', __FILE__)
I18n.reload!
end
before(:each) do
@assigns = {}
@controller = DummyController.new
@request = @controller.request
@template = '<%= will_paginate collection, options %>'
end
attr_reader :assigns, :controller, :request
def render(locals)
@view = ActionView::Base.new([], @assigns, @controller)
@view.request = @request
@view.singleton_class.send(:include, @controller._routes.url_helpers)
@view.render(:inline => @template, :locals => locals)
end
## basic pagination ##
it "should render" do
paginate do |pagination|
assert_select 'a[href]', 3 do |elements|
validate_page_numbers [2,3,2], elements
assert_select elements.last, ':last-child', "Next →"
end
assert_select 'span', 1
assert_select 'span.disabled:first-child', '← Previous'
assert_select 'em.current', '1'
pagination.first.inner_text.should == '← Previous 1 2 3 Next →'
end
end
it "should render nothing when there is only 1 page" do
paginate(:per_page => 30).should be_empty
end
it "should paginate with options" do
paginate({ :page => 2 }, :class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
assert_select 'a[href]', 4 do |elements|
validate_page_numbers [1,1,3,3], elements
# test rel attribute values:
assert_select elements[1], 'a', '1' do |link|
link.first['rel'].should == 'prev start'
end
assert_select elements.first, 'a', "Prev" do |link|
link.first['rel'].should == 'prev start'
end
assert_select elements.last, 'a', "Next" do |link|
link.first['rel'].should == 'next'
end
end
assert_select '.current', '2'
end
end
it "should paginate using a custom renderer class" do
paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
assert_select 'a[default=true]', 3
end
end
it "should paginate using a custom renderer instance" do
renderer = WillPaginate::ActionView::LinkRenderer.new
def renderer.gap() '<span class="my-gap">~~</span>' end
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
assert_select 'span.my-gap', '~~'
end
renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
paginate({}, :renderer => renderer) do
assert_select 'a[title=rendered]', 3
end
end
it "should have classnames on previous/next links" do
paginate do |pagination|
assert_select 'span.disabled.previous_page:first-child'
assert_select 'a.next_page[href]:last-child'
end
end
it "should match expected markup" do
paginate
expected = <<-HTML
<div class="pagination"><span class="previous_page disabled">← Previous</span>
<em class="current">1</em>
<a href="/foo/bar?page=2" rel="next">2</a>
<a href="/foo/bar?page=3">3</a>
<a href="/foo/bar?page=2" class="next_page" rel="next">Next →</a></div>
HTML
expected.strip!.gsub!(/\s{2,}/, ' ')
expected_dom = HTML::Document.new(expected).root
html_document.root.should == expected_dom
end
it "should output escaped URLs" do
paginate({:page => 1, :per_page => 1, :total_entries => 2},
:page_links => false, :params => { :tag => '<br>' })
assert_select 'a[href]', 1 do |links|
query = links.first['href'].split('?', 2)[1]
query.split('&').sort.should == %w(page=2 tag=%3Cbr%3E)
end
end
## advanced options for pagination ##
it "should be able to render without container" do
paginate({}, :container => false)
assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
assert_select 'a[href]', 3
end
it "should be able to render without page links" do
paginate({ :page => 2 }, :page_links => false) do
assert_select 'a[href]', 2 do |elements|
validate_page_numbers [1,3], elements
end
end
end
## other helpers ##
it "should render a paginated section" do
@template = <<-ERB
<%= paginated_section collection, options do %>
<%= content_tag :div, '', :id => "developers" %>
<% end %>
ERB
paginate
assert_select 'div.pagination', 2
assert_select 'div.pagination + div#developers', 1
end
it "should not render a paginated section with a single page" do
@template = <<-ERB
<%= paginated_section collection, options do %>
<%= content_tag :div, '', :id => "developers" %>
<% end %>
ERB
paginate(:total_entries => 1)
assert_select 'div.pagination', 0
assert_select 'div#developers', 1
end
## parameter handling in page links ##
it "should preserve parameters on GET" do
request.params :foo => { :bar => 'baz' }
paginate
assert_links_match /foo\[bar\]=baz/
end
it "should not preserve parameters on POST" do
request.post
request.params :foo => 'bar'
paginate
assert_no_links_match /foo=bar/
end
it "should add additional parameters to links" do
paginate({}, :params => { :foo => 'bar' })
assert_links_match /foo=bar/
end
it "should add anchor parameter" do
paginate({}, :params => { :anchor => 'anchor' })
assert_links_match /#anchor$/
end
it "should remove arbitrary parameters" do
request.params :foo => 'bar'
paginate({}, :params => { :foo => nil })
assert_no_links_match /foo=bar/
end
it "should override default route parameters" do
paginate({}, :params => { :controller => 'baz', :action => 'list' })
assert_links_match %r{\Wbaz/list\W}
end
it "should paginate with custom page parameter" do
paginate({ :page => 2 }, :param_name => :developers_page) do
assert_select 'a[href]', 4 do |elements|
validate_page_numbers [1,1,3,3], elements, :developers_page
end
end
end
it "should paginate with complex custom page parameter" do
request.params :developers => { :page => 2 }
paginate({ :page => 2 }, :param_name => 'developers[page]') do
assert_select 'a[href]', 4 do |links|
assert_links_match /\?developers\[page\]=\d+$/, links
validate_page_numbers [1,1,3,3], links, 'developers[page]'
end
end
end
it "should paginate with custom route page parameter" do
request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
paginate :per_page => 2 do
assert_select 'a[href]', 6 do |links|
assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
end
end
end
it "should paginate with custom route with dot separator page parameter" do
request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
paginate :per_page => 2 do
assert_select 'a[href]', 6 do |links|
assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
end
end
end
it "should paginate with custom route and first page number implicit" do
request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
paginate :page => 2, :per_page => 2 do
assert_select 'a[href]', 7 do |links|
assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
end
end
# Routes.recognize_path('/ibocorp/2').should == {:page=>'2', :action=>'index', :controller=>'ibocorp'}
# Routes.recognize_path('/ibocorp/foo').should == {:action=>'foo', :controller=>'ibocorp'}
end
## internal hardcore stuff ##
it "should be able to guess the collection name" do
collection = mock
collection.expects(:total_pages).returns(1)
@template = '<%= will_paginate options %>'
controller.controller_name = 'developers'
assigns['developers'] = collection
paginate(nil)
end
it "should fail if the inferred collection is nil" do
@template = '<%= will_paginate options %>'
controller.controller_name = 'developers'
lambda {
paginate(nil)
}.should raise_error(ActionView::TemplateError, /@developers/)
end
end
class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
def initialize(link_attributes = nil)
super()
@additional_link_attributes = link_attributes || { :default => 'true' }
end
def link(text, target, attributes = {})
super(text, target, attributes.merge(@additional_link_attributes))
end
end
class DummyController
attr_reader :request
attr_accessor :controller_name
include ActionController::UrlFor
include Routes.url_helpers
def initialize
@request = DummyRequest.new
end
def params
@request.params
end
def env
{}
end
def _prefixes
[]
end
end
class IbocorpController < DummyController
end
class DummyRequest
attr_accessor :symbolized_path_parameters
def initialize
@get = true
@params = {}
@symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
end
def get?
@get
end
def post
@get = false
end
def relative_url_root
''
end
def script_name
''
end
def params(more = nil)
@params.update(more) if more
@params
end
def host_with_port
'example.com'
end
alias host host_with_port
def optional_port
''
end
def protocol
'http:'
end
end