require 'spec_helper'
require 'html/spec_helper'
describe "UrlHelper" do
isolate :conveyors, :router, before: :all
before :all do
rad.web
rad.reset :conveyors
class MockRouter < Rad::Router
def url_for_path path, params
path = if params.blank?
path
else
path + '?' + params.to_a.collect{|a, b| "#{a}=#{b}"}.join("&")
end
"url_for: #{path}"
end
def url_for_class klass, method, params
path = "#{klass}.#{method}"
url_for_path path, params
end
end
MOCK_ROUTER = MockRouter.new :class
class MockUrlHelperContext < Rad::MockTemplateContext
inherit Rad::ViewRoutingHelper
def url_for *args
opt = args.extract_options!
format = opt[:format]
args << opt
# url = args.inspect.gsub('"', "'")
url = MOCK_ROUTER.url_for *args
# url = "url_for: #{args.to_s}"
url.marks.format = format
url
end
end
end
after :all do
remove_constants :MOCK_ROUTER, :MockRouter, :MockUrlHelperContext
end
before do
@t = MockUrlHelperContext.new
end
describe "link_to" do
it "should works with url_for attributes" do
@t.link_to('Book', Object, :method, {format: 'html'}, class: 'highlight').to_xhtml('a').
should_be_fuzzy_equal_to(class: "highlight", href: "url_for: Object.method?format=html", content: 'Book')
@t.link_to(Object, :method, {format: 'html'}, class: 'highlight'){'Book'}.to_xhtml('a').
should_be_fuzzy_equal_to(class: "highlight", href: "url_for: Object.method?format=html", content: 'Book')
@t.link_to('Book', :method).to_xhtml('a').
should_be_fuzzy_equal_to(href: "url_for: .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_fuzzy_equal_to(class: "highlight", href: "/some_book", content: 'Book')
@t.link_to('/some_book', class: 'highlight'){'Book'}.to_xhtml('a').
should_be_fuzzy_equal_to(class: "highlight", href: "/some_book", content: 'Book')
@t.link_to('/some_book'){'Book'}.to_xhtml('a').
should_be_fuzzy_equal_to(href: "/some_book", content: 'Book')
end
it "links for 'js', 'json' formats should be automatically became remote" do
@t.link_to('Book', @t.url_for('/some_book', format: 'json')).to_xhtml('a').should_be_fuzzy_equal_to(
href: '#', content: 'Book',
'data-action' => 'url_for: /some_book?format=json', 'data-method' => 'post', 'data-remote' => 'true'
)
@t.link_to('Book', :method, format: 'js').to_xhtml('a').should_be_fuzzy_equal_to(
href: '#', content: 'Book',
'data-action' => 'url_for: .method?format=js', 'data-method' => 'post', 'data-remote' => 'true'
)
@t.link_to('Book', :method, {format: 'js'}, class: 'highlight').to_xhtml('a').should_be_fuzzy_equal_to(
href: '#', content: 'Book', class: 'highlight',
'data-action' => 'url_for: .method?format=js', 'data-method' => 'post', 'data-remote' => 'true'
)
end
it "confirm" do
attrs = @t.link_to('Book', '/some_book', confirm: 'Are you shure?').to_xhtml('a')
attrs.should_not include('data-remote')
attrs.should_not include('data-action')
attrs.should_not include('data-method')
attrs.should_be_fuzzy_equal_to(
href: "/some_book", content: 'Book',
'data-confirm' => "Are you shure?"
)
end
it "POST method" do
attrs = @t.link_to('Book', '/some_book', method: :post).to_xhtml('a')
attrs.should_not include('data-remote')
attrs.should_be_fuzzy_equal_to(
href: "#", content: 'Book',
'data-action' => '/some_book', 'data-method' => 'post'
)
end
it "remote" do
@t.link_to('Book', '/some_book', remote: true).to_xhtml('a').should_be_fuzzy_equal_to(
href: "#", content: 'Book',
'data-action' => '/some_book', 'data-method' => 'post', 'data-remote' => 'true'
)
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_fuzzy_equal_to(href: "/go_back", content: 'Book')
@t.link_to('Book', :back, class: '_some_js_mark').to_xhtml('a').
should_be_fuzzy_equal_to(href: "/go_back", class: '_some_js_mark', content: 'Book')
end
it "#" do
@t.link_to('Book', '#').to_xhtml('a').should_be_fuzzy_equal_to(href: "#", content: 'Book')
end
end
end