require File.dirname(__FILE__) + '/spec_helper'
include Merb::AssetsMixin
describe "Accessing Assets" do
it "should create link to name with absolute url" do
link_to("The Merb home page", "http://www.merbivore.com/").should ==
"The Merb home page"
end
it "should create link to name with relative url" do
link_to("The Entry Title", "/blog/show/13").should ==
"The Entry Title"
end
it "should create link with attributes" do
link_to("The Ruby home page", "http://www.ruby-lang.org", {'class' => 'special', 'target' => 'blank'}).should match(%r{class="special"})
link_to("The Ruby home page", "http://www.ruby-lang.org", {'class' => 'special', 'target' => 'blank'}).should match(%r{target="blank"})
end
it "should create link with explicit href" do
link_to("The Foo home page", "http://not.foo.example.com/", :href => "http://foo.example.com").should ==
"The Foo home page"
end
it "should create image tag with absolute url" do
image_tag('http://example.com/foo.gif').should ==
""
end
it "should create image tag with relative url" do
image_tag('foo.gif').should ==
""
end
it "should create image tag with class" do
result = image_tag('foo.gif', :class => 'bar')
result.should match(%r{})
result.should match(%r{src="/images/foo.gif"})
result.should match(/class="bar"/)
end
it "should create image tag with specified path" do
image_tag('foo.gif', :path => '/files/').should ==
""
end
it "should create image tag without extension" do
image_tag('/dynamic/charts').should ==
""
end
it "should create image tag without extension and with specified path" do
image_tag('charts', :path => '/dynamic/').should ==
""
end
it "should create image tag with a random query string" do
result = image_tag('foo.gif', :reload => true)
result.should match(%r{})
end
end
describe "JavaScript related functions" do
it "should escape js having quotes" do
escape_js("'Lorem ipsum!' -- Some guy").should ==
"\\'Lorem ipsum!\\' -- Some guy"
end
it "should escape js having new lines" do
escape_js("Please keep text\nlines as skinny\nas possible.").should ==
"Please keep text\\nlines as skinny\\nas possible."
end
it "should convert objects that respond to to_json to json" do
js({'user' => 'Lewis', 'page' => 'home'}).should ==
"{\"user\":\"Lewis\",\"page\":\"home\"}"
end
it "should convert objects using inspect that don't respond to_json to json" do
js([ 1, 2, {"a"=>3.141}, false, true, nil, 4..10 ]).should ==
"[1,2,{\"a\":3.141},false,true,null,\"4..10\"]"
end
end
describe "External JavaScript and Stylesheets" do
it "should require a js file only once" do
require_js :jquery
require_js :jquery, :effects
include_required_js.scan(%r{src="/javascripts/jquery.js"}).should have(1).things
include_required_js.scan(%r{src="/javascripts/effects.js"}).should have(1).things
end
it "should require a css file only once" do
require_css :style
require_css :style, 'ie-specific'
include_required_css.scan(%r{href="/stylesheets/style.css"}).should have(1).things
include_required_css.scan(%r{href="/stylesheets/ie-specific.css"}).should have(1).things
end
it "should require included js" do
require_js 'jquery', 'effects', 'validation'
result = include_required_js
result.scan(/"
end
it "should create a js include tag and and the extension" do
js_include_tag('jquery').should ==
""
end
it "should create a js include tag for multiple includes" do
result = js_include_tag('jquery.js', :effects)
result.scan(/})
result.should match(/charset="utf-8"/)
result.should match(%r{type="text/css"})
result.should match(%r{href="/stylesheets/style.css"})
result.should match(%r{rel="Stylesheet"})
result.should match(%r{media="all"})
end
it "should create a css include tag and add the extension" do
result = css_include_tag('style')
result.should match(%r{})
result.should match(/charset="utf-8"/)
result.should match(%r{type="text/css"})
result.should match(%r{href="/stylesheets/style.css"})
result.should match(%r{rel="Stylesheet"})
result.should match(%r{media="all"})
end
it "should create a css include tag for multiple includes" do
result = css_include_tag('style.css', :layout)
result.scan(/ true)
result.should match(%r{/stylesheets/style.css\?\d+})
end
it "should create a css include tag with the specified media" do
css_include_tag('style', :media => :print).should match(%r{media="print"})
end
it "should create a css include tag with the specified charset" do
css_include_tag('style', :charset => 'iso-8859-1').should match(%r{charset="iso-8859-1"})
end
it "should return a uniq path for a single asset" do
uniq_path("/javascripts/my.js").should ==
"http://assets2.my-awesome-domain.com/javascripts/my.js"
end
it "should return a uniq path for multiple assets" do
uniq_path("/javascripts/my.js","/javascripts/my.css").should ==
["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets2.my-awesome-domain.com/javascripts/my.css"]
end
it "should return a uniq path for multiple assets passed as a single array" do
uniq_path(["/javascripts/my.js","/javascripts/my.css"]).should ==
["http://assets2.my-awesome-domain.com/javascripts/my.js", "http://assets2.my-awesome-domain.com/javascripts/my.css"]
end
it "should return a uniq js path for a single js file" do
uniq_js_path("my").should ==
"http://assets2.my-awesome-domain.com/javascripts/my.js"
end
it "should return a uniq js path for multiple js files" do
uniq_js_path(["admin/secrets","home/signup"]).should ==
["http://assets1.my-awesome-domain.com/javascripts/admin/secrets.js", "http://assets2.my-awesome-domain.com/javascripts/home/signup.js"]
end
it "should return a uniq css path for a single css file" do
uniq_css_path("my").should ==
"http://assets1.my-awesome-domain.com/stylesheets/my.css"
end
it "should return a uniq css path for multiple css files" do
uniq_css_path(["admin/secrets","home/signup"]).should ==
["http://assets4.my-awesome-domain.com/stylesheets/admin/secrets.css", "http://assets1.my-awesome-domain.com/stylesheets/home/signup.css"]
end
it "should create a uniq js tag for a single js file" do
uniq_js_tag("my").should ==
""
end
it "should create a uniq js tag for each js file specified" do
result = uniq_js_tag("jquery.js", :effects)
result.scan(/})
result.should match(/charset="utf-8"/)
result.should match(%r{type="text/css"})
result.should match(%r{http://assets1.my-awesome-domain.com/stylesheets/my.css})
result.should match(%r{rel="Stylesheet"})
result.should match(%r{media="all"})
end
it "should create a uniq css tag for each css file specified" do
result = uniq_css_tag("style.css", :layout)
result.scan(/