PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/will_paginate/test/view_test.rb

https://github.com/karthikeyan7585/adva_cms
Ruby | 278 lines | 220 code | 49 blank | 9 comment | 1 complexity | ccf1e2e07f9b8f965e96a0bf2c4d73f2 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. require 'helper'
  2. require 'lib/view_test_process'
  3. class ViewTest < WillPaginate::ViewTestCase
  4. ## basic pagination ##
  5. def test_will_paginate
  6. paginate do |pagination|
  7. assert_select 'a[href]', 3 do |elements|
  8. validate_page_numbers [2,3,2], elements
  9. assert_select elements.last, ':last-child', "Next &raquo;"
  10. end
  11. assert_select 'span', 2
  12. assert_select 'span.disabled:first-child', '&laquo; Previous'
  13. assert_select 'span.current', '1'
  14. assert_equal '&laquo; Previous 1 2 3 Next &raquo;', pagination.first.inner_text
  15. end
  16. end
  17. def test_no_pagination_when_page_count_is_one
  18. paginate :per_page => 30
  19. assert_equal '', @html_result
  20. end
  21. def test_will_paginate_with_options
  22. paginate({ :page => 2 },
  23. :class => 'will_paginate', :prev_label => 'Prev', :next_label => 'Next') do
  24. assert_select 'a[href]', 4 do |elements|
  25. validate_page_numbers [1,1,3,3], elements
  26. # test rel attribute values:
  27. assert_select elements[1], 'a', '1' do |link|
  28. assert_equal 'prev start', link.first['rel']
  29. end
  30. assert_select elements.first, 'a', "Prev" do |link|
  31. assert_equal 'prev start', link.first['rel']
  32. end
  33. assert_select elements.last, 'a', "Next" do |link|
  34. assert_equal 'next', link.first['rel']
  35. end
  36. end
  37. assert_select 'span.current', '2'
  38. end
  39. end
  40. def test_prev_next_links_have_classnames
  41. paginate do |pagination|
  42. assert_select 'span.disabled.prev_page:first-child'
  43. assert_select 'a.next_page[href]:last-child'
  44. end
  45. end
  46. def test_full_output
  47. paginate
  48. expected = <<-HTML
  49. <div class="pagination"><span class="disabled prev_page">&laquo; Previous</span>
  50. <span class="current">1</span>
  51. <a href="/foo/bar?page=2" rel="next">2</a>
  52. <a href="/foo/bar?page=3">3</a>
  53. <a href="/foo/bar?page=2" class="next_page" rel="next">Next &raquo;</a></div>
  54. HTML
  55. expected.strip!.gsub!(/\s{2,}/, ' ')
  56. assert_dom_equal expected, @html_result
  57. end
  58. ## advanced options for pagination ##
  59. def test_will_paginate_without_container
  60. paginate({}, :container => false)
  61. assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
  62. assert_select 'a[href]', 3
  63. end
  64. def test_will_paginate_without_page_links
  65. paginate({ :page => 2 }, :page_links => false) do
  66. assert_select 'a[href]', 2 do |elements|
  67. validate_page_numbers [1,3], elements
  68. end
  69. end
  70. end
  71. def test_will_paginate_windows
  72. paginate({ :page => 6, :per_page => 1 }, :inner_window => 1) do |pagination|
  73. assert_select 'a[href]', 8 do |elements|
  74. validate_page_numbers [5,1,2,5,7,10,11,7], elements
  75. assert_select elements.first, 'a', '&laquo; Previous'
  76. assert_select elements.last, 'a', 'Next &raquo;'
  77. end
  78. assert_select 'span.current', '6'
  79. assert_equal '&laquo; Previous 1 2 &hellip; 5 6 7 &hellip; 10 11 Next &raquo;', pagination.first.inner_text
  80. end
  81. end
  82. def test_will_paginate_eliminates_small_gaps
  83. paginate({ :page => 6, :per_page => 1 }, :inner_window => 2) do
  84. assert_select 'a[href]', 12 do |elements|
  85. validate_page_numbers [5,1,2,3,4,5,7,8,9,10,11,7], elements
  86. end
  87. end
  88. end
  89. def test_container_id
  90. paginate do |div|
  91. assert_nil div.first['id']
  92. end
  93. # magic ID
  94. paginate({}, :id => true) do |div|
  95. assert_equal 'fixnums_pagination', div.first['id']
  96. end
  97. # explicit ID
  98. paginate({}, :id => 'custom_id') do |div|
  99. assert_equal 'custom_id', div.first['id']
  100. end
  101. end
  102. ## other helpers ##
  103. def test_paginated_section
  104. @template = <<-ERB
  105. <% paginated_section collection, options do %>
  106. <%= content_tag :div, '', :id => "developers" %>
  107. <% end %>
  108. ERB
  109. paginate
  110. assert_select 'div.pagination', 2
  111. assert_select 'div.pagination + div#developers', 1
  112. end
  113. def test_page_entries_info
  114. @template = '<%= page_entries_info collection %>'
  115. array = ('a'..'z').to_a
  116. paginate array.paginate(:page => 2, :per_page => 5)
  117. assert_equal %{Displaying entries <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total},
  118. @html_result
  119. paginate array.paginate(:page => 7, :per_page => 4)
  120. assert_equal %{Displaying entries <b>25&nbsp;-&nbsp;26</b> of <b>26</b> in total},
  121. @html_result
  122. end
  123. def test_page_entries_info_with_single_page_collection
  124. @template = '<%= page_entries_info collection %>'
  125. paginate(('a'..'d').to_a.paginate(:page => 1, :per_page => 5))
  126. assert_equal %{Displaying <b>all 4</b> entries}, @html_result
  127. paginate(['a'].paginate(:page => 1, :per_page => 5))
  128. assert_equal %{Displaying <b>1</b> entry}, @html_result
  129. paginate([].paginate(:page => 1, :per_page => 5))
  130. assert_equal %{No entries found}, @html_result
  131. end
  132. ## parameter handling in page links ##
  133. def test_will_paginate_preserves_parameters_on_get
  134. @request.params :foo => { :bar => 'baz' }
  135. paginate
  136. assert_links_match /foo%5Bbar%5D=baz/
  137. end
  138. def test_will_paginate_doesnt_preserve_parameters_on_post
  139. @request.post
  140. @request.params :foo => 'bar'
  141. paginate
  142. assert_no_links_match /foo=bar/
  143. end
  144. def test_adding_additional_parameters
  145. paginate({}, :params => { :foo => 'bar' })
  146. assert_links_match /foo=bar/
  147. end
  148. def test_adding_anchor_parameter
  149. paginate({}, :params => { :anchor => 'anchor' })
  150. assert_links_match /#anchor$/
  151. end
  152. def test_removing_arbitrary_parameters
  153. @request.params :foo => 'bar'
  154. paginate({}, :params => { :foo => nil })
  155. assert_no_links_match /foo=bar/
  156. end
  157. def test_adding_additional_route_parameters
  158. paginate({}, :params => { :controller => 'baz', :action => 'list' })
  159. assert_links_match %r{\Wbaz/list\W}
  160. end
  161. def test_will_paginate_with_custom_page_param
  162. paginate({ :page => 2 }, :param_name => :developers_page) do
  163. assert_select 'a[href]', 4 do |elements|
  164. validate_page_numbers [1,1,3,3], elements, :developers_page
  165. end
  166. end
  167. end
  168. def test_complex_custom_page_param
  169. @request.params :developers => { :page => 2 }
  170. paginate({ :page => 2 }, :param_name => 'developers[page]') do
  171. assert_select 'a[href]', 4 do |links|
  172. assert_links_match /\?developers%5Bpage%5D=\d+$/, links
  173. validate_page_numbers [1,1,3,3], links, 'developers[page]'
  174. end
  175. end
  176. end
  177. def test_custom_routing_page_param
  178. @request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
  179. paginate :per_page => 2 do
  180. assert_select 'a[href]', 6 do |links|
  181. assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
  182. end
  183. end
  184. end
  185. def test_custom_routing_page_param_with_dot_separator
  186. @request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
  187. paginate :per_page => 2 do
  188. assert_select 'a[href]', 6 do |links|
  189. assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
  190. end
  191. end
  192. end
  193. ## internal hardcore stuff ##
  194. class LegacyCollection < WillPaginate::Collection
  195. alias :page_count :total_pages
  196. undef :total_pages
  197. end
  198. def test_deprecation_notices_with_page_count
  199. collection = LegacyCollection.new(1, 1, 2)
  200. assert_deprecated collection.class.name do
  201. paginate collection
  202. end
  203. end
  204. uses_mocha 'view internals' do
  205. def test_collection_name_can_be_guessed
  206. collection = mock
  207. collection.expects(:total_pages).returns(1)
  208. @template = '<%= will_paginate options %>'
  209. @controller.controller_name = 'developers'
  210. @view.assigns['developers'] = collection
  211. paginate(nil)
  212. end
  213. end
  214. def test_inferred_collection_name_raises_error_when_nil
  215. @template = '<%= will_paginate options %>'
  216. @controller.controller_name = 'developers'
  217. e = assert_raise ArgumentError do
  218. paginate(nil)
  219. end
  220. assert e.message.include?('@developers')
  221. end
  222. if ActionController::Base.respond_to? :rescue_responses
  223. # only on Rails 2
  224. def test_rescue_response_hook_presence
  225. assert_equal :not_found,
  226. ActionController::Base.rescue_responses['WillPaginate::InvalidPage']
  227. end
  228. end
  229. end