require 'helper' require 'action_view/test_case' class TestViewHelper < Test::Unit::TestCase class MyView < ActionView::Base include SimpleSearch::ViewHelper end def setup @view = MyView.new router = ActionDispatch::Routing::RouteSet.new router.draw do match ':controller(/:action(/:id(.:format)))' end @view.instance_variable_set(:@_routes, router) end should "generate order by link" do @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&a=1&b=2"}) assert_match "order_by=foo+asc", @view.order_link(:foo) assert_match ">Foo Bar<", @view.order_link(:foo_bar) assert_match ">Custom Name<", @view.order_link(:foo, "Custom Name") assert_match "class=\"bar\"", @view.order_link(:foo, "Name", :class=>:bar) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&order_by=foo+asc&a=1&b=2"}) assert_match "order_by=foo+desc", @view.order_link(:foo) assert_match ">Foo &#9650;<", @view.order_link(:foo) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&order_by=foo+desc&a=1&b=2"}) assert_match "order_by=foo+asc", @view.order_link(:foo) assert_match ">Foo &#9660;<", @view.order_link(:foo) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[order_by]=foo+desc&a=1&b=2"}) assert_match "ss%5Border_by%5D=foo+asc", @view.order_link(:foo) assert_match ">Foo &#9660;<", @view.order_link(:foo) end should "generate page properties" do @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&page_by=2"}) arel = Post.select('posts.*, comments.*').joins(:comments).simplesearch(:page_by=>2) @view.page_properties(arel) assert_equal ({ :total_rows => 6, :last_page => 3, :current_page => 1, :rows_per_page => 2, :page_arr_key => nil }), @view.page_properties(arel) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page_by]=2"}) assert_equal ({ :total_rows => 6, :last_page => 3, :current_page => 1, :rows_per_page => 2, :page_arr_key => "ss" }), @view.page_properties(arel) end should "generate url params for single page" do @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&page_by=2"}) params = @view.page_params(3) assert_equal 2, params[:page_by].to_i assert_equal 3, params[:page] @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page_by]=2"}) params = @view.page_params(3, "ss") assert_equal 2, params[:ss][:page_by].to_i assert_equal 3, params[:ss][:page] end should "generate url params for many pages" do arel = Post.select('posts.*, post_likes.*').joins(:post_likes).simplesearch(:page_by=>30) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&page=5&page_by=10"}) url_params = @view.page_url_params(arel) assert_equal [1,3,4,5,6,7,30], url_params.keys assert_equal 10, url_params[5][:page_by].to_i assert_equal 5, url_params[5][:page].to_i @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page_by]=2"}) url_params = @view.page_url_params(arel) assert_equal 2, url_params[1][:ss][:page_by].to_i assert_equal 1, url_params[1][:ss][:page].to_i end should "generate page urls" do arel = Post.select('posts.*, post_likes.*').joins(:post_likes).simplesearch(:page_by=>30) @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&page=5&page_by=10"}) page_urls = @view.page_urls(arel) assert_equal 9, page_urls.length # 1 ... 3 4 5 6 7 ... 10 assert_equal "<span class='filler'>...</span>", page_urls[1] assert_equal "<span class='filler'>...</span>", page_urls[7] end end