require 'spec_helper' require 'action_view/test_case' class MyView < ActionView::Base include SimpleSearch::ViewHelper end describe SimpleSearch::ViewHelper do before(:all) do @view = MyView.new router = ActionDispatch::Routing::RouteSet.new router.draw do match ':controller(/:action(/:id(.:format)))' end @view.instance_variable_set(:@_routes, router) end describe "#order_link" do it "generate order by link" do @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&a=1&b=2"}) @view.order_link(:foo).should match /order_by=foo/ @view.order_link(:foo_bar).should match />Foo BarCustom Name:bar).should match /class=\"bar\"/ @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&order_by=foo+asc&a=1&b=2"}) @view.order_link(:foo).should match /order_by=foo\+desc/ @view.order_link(:foo).should match />Foo ▲"/tests", "QUERY_STRING"=>"controller=tests&order_by=foo+desc&a=1&b=2"}) @view.order_link(:foo).should match /order_by=foo\+asc/ @view.order_link(:foo).should match />Foo ▼"/tests", "QUERY_STRING"=>"controller=tests&ss[order_by]=foo+desc&a=1&b=2"}) @view.order_link(:foo).should match /ss%5Border_by%5D=foo\+asc/ @view.order_link(:foo).should match />Foo ▼"/tests", "QUERY_STRING"=>"controller=tests&page_by=2"}) arel = Post.select('posts.*, comments.*').joins(:comments).simplesearch(:page_by=>2) @view.page_properties(arel).should == {:total_rows => 6, :last_page => 3, :current_page => 1, :rows_per_page => 2, :page_arr_key => nil} @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page_by]=2"}) @view.page_properties(arel).should == {:total_rows => 6, :last_page => 3, :current_page => 1, :rows_per_page => 2, :page_arr_key => "ss"} end end describe "#page_params" do it "generate url params for single page" do @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&page=3&page_by=30"}) params = @view.page_params(3, 30) #page number, rows per page params[:page_by].to_i.should == 30 params[:page].should == 3 @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page]=3&ss[page_by]=2"}) params = @view.page_params(3, 30, "ss") params[:ss][:page_by].to_i.should == 30 params[:ss][:page].should == 3 end end describe "#page_url_params" do it "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) url_params.keys.should == [1,3,4,5,6,7,30] url_params[5][:page_by].to_i.should == 10 url_params[5][:page].to_i.should == 5 @view.request = ActionController::TestRequest.new({"SCRIPT_NAME"=>"/tests", "QUERY_STRING"=>"controller=tests&ss[page_by]=2"}) url_params = @view.page_url_params(arel) url_params[1][:ss][:page_by].to_i.should == 2 url_params[1][:ss][:page].to_i.should == 1 end end describe "#page_urls" do it "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) page_urls.length.should == 9 # 1 ... 3 4 5 6 7 ... 10 page_urls[1].should eq "..." page_urls[7].should eq "..." end end end