require 'html/spec_helper'
require 'integration/spec_helper'

describe "UrlHelper" do
  before :all do
    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 :each do        
    @t = MockUrlHelperContext.new
  end
  
  describe "link_to" do
    before :all do
      rad[:config] = Rad::Config.new
    end
    
    after :all do
      rad.clear
    end
    
    it "should works with url_for attributes" do
      @t.link_to('Book', Object, :method, {format: 'json'}, class: 'highlight').to_xhtml('a').
        should_be_fuzzy_equal_to(class: "highlight", href: "url_for: Object.method?format=json", content: "Book")
        
      @t.link_to(Object, :method, {format: 'json'}, class: 'highlight'){'Book'}.to_xhtml('a').
        should_be_fuzzy_equal_to(class: "highlight", href: "url_for: Object.method?format=json", 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: "url_for: /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_fuzzy_equal_to(
        href: "url_for: .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_fuzzy_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_fuzzy_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_fuzzy_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_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