require "#{File.expand_path(File.dirname(__FILE__))}/helper" describe "UrlHelper" do class MockUrlHelperContext < Crystal::MockTemplateContext inherit Crystal::ControllerUrlHelper, Crystal::ViewUrlHelper def url_for *args opt = args.extract_options! format = opt.stringify_keys['format'] args << opt url = args.inspect.gsub('"', "'") url.marks.format = format url end end before :each do @t = MockUrlHelperContext.new end describe "link_to" do before :all do crystal[:config] = Crystal::Config.new end after :all do crystal.clear end it "should works with url_for_remote attributes" do @t.link_to('Book', Object, :method, {:format => :json}, :class => 'highlight').to_xhtml('a'). should_be_equal_to(:class => "highlight", :href => "[Object, :method, {:format=>:json}]", :content => "Book") @t.link_to(Object, :method, {:format => :json}, :class => 'highlight'){'Book'}.to_xhtml('a'). should_be_equal_to(:class => "highlight", :href => "[Object, :method, {:format=>:json}]", :content => "Book") @t.link_to('Book', :method).to_xhtml('a'). should_be_equal_to(:href => "[:method, {}]", :content => "Book") end it "should works with url_for_path attributes" do @t.link_to('Book', '/some_book', {}, :class => 'highlight').to_xhtml('a'). should_be_equal_to(:class => "highlight", :href => "['/some_book', {}]", :content => "Book") @t.link_to('/some_book', {}, :class => 'highlight'){'Book'}.to_xhtml('a'). should_be_equal_to(:class => "highlight", :href => "['/some_book', {}]", :content => "Book") @t.link_to('/some_book'){'Book'}.to_xhtml('a'). should_be_equal_to(:href => "['/some_book', {}]", :content => "Book") end it "links for :js, :json formats should be automatically became remote" do @t.link_to('Book', '/some_book', :format => :json).to_xhtml('a').should_be_equal_to( :href => "['/some_book', {:format=>:json}]", :content => "Book", :onclick => "$(this).link_to({method: 'get', ajax: true}); return false;" ) @t.link_to('Book', :method, {:format => :js}, :class => 'highlight').to_xhtml('a').should_be_equal_to( :href => "[:method, {:format=>:js}]", :content => "Book", :class => 'highlight', :onclick => "$(this).link_to({method: 'get', ajax: true}); return false;" ) end it "confirm" do @t.link_to('Book', '/some_book', {}, :confirm => 'Are you shure?').to_xhtml('a').should_be_equal_to( :href => "['/some_book', {}]", :content => "Book", :onclick => "return confirm(Are you shure?);" ) end it "POST method" do @t.link_to('Book', '/some_book', {}, :method => :post).to_xhtml('a').should_be_equal_to( :href => "['/some_book', {}]", :content => "Book", :onclick => "$(this).link_to({method: 'post'}); return false;" ) end it "remote" do @t.link_to('Book', '/some_book', {}, :remote => true).to_xhtml('a').should_be_equal_to( :href => "['/some_book', {}]", :content => "Book", :onclick => "$(this).link_to({method: 'get', ajax: true}); return false;" ) end it ":back" do env = Object.new env.stub(:[]).and_return('/go_back') request = Object.new request.stub(:env).and_return(env) workspace = Object.new workspace.stub(:request).and_return(request) @t.stub(:workspace).and_return(workspace) @t.link_to('Book', :back).to_xhtml('a').should_be_equal_to(:href => "/go_back", :content => "Book") end it "#" do @t.link_to('Book', '#').to_xhtml('a').should_be_equal_to(:href => "#", :content => "Book") end end def stub_workspace @response = Crystal::Response.new @params = Crystal::Params.new @workspace = Object.new @workspace.stub(:params).and_return(@params) @workspace.stub(:response).and_return(@response) @t.stub(:workspace).and_return(@workspace) end describe "redirect_to" do before :each do stub_workspace end it "html format" do @workspace.stub(:params).and_return({:format => 'html'}.to_openobject) @t.redirect_to('/some_book').should == nil @response.status.should == 301 @response.headers['Location'].should == "['/some_book', {}]" @response.body.should =~ /You are being/ end it "js format" do @workspace.stub(:params).and_return({:format => 'js'}.to_openobject) @t.redirect_to('/some_book').should be_nil @response.status.should == 200 @response.headers['Location'].should be_blank @response.body.should == "window.location = '['/some_book', {}]';" end end describe "reload_page" do before :each do stub_workspace end it "basic" do @params.format = 'js' @t.reload_page @response.status.should == 200 @response.body.should =~ /reload/ end end end