spec/sprockets-helpers_spec.rb in sprockets-helpers-1.1.0 vs spec/sprockets-helpers_spec.rb in sprockets-helpers-1.2.0

- old
+ new

@@ -5,14 +5,16 @@ it 'sets global configuration' do within_construct do |c| c.file 'assets/main.css' expect(context.asset_path('main.css')).to eq('/assets/main.css') + Sprockets::Helpers.configure do |config| config.digest = true config.prefix = '/themes' end + expect(context.asset_path('main.css')).to match(%r(/themes/main-[0-9a-f]+.css)) Sprockets::Helpers.digest = nil Sprockets::Helpers.prefix = nil end end @@ -393,10 +395,15 @@ end end end describe '#asset_tag' do + after do + Sprockets::Helpers.debug = nil + Sprockets::Helpers.expand = nil + end + it 'receives block to generate tag' do actual = context.asset_tag('main.js') { |path| "<script src=#{path}></script>" } expect(actual).to eq('<script src=/main.js></script>') end @@ -407,50 +414,87 @@ it 'expands when configured' do within_construct do |construct| assets_layout(construct) Sprockets::Helpers.expand = true c = context - c.stub(:asset_path).and_return(context.asset_path('main.js')) # Spy - c.should_receive(:asset_path).with('main.js', {:expand => true, :debug => nil}) + allow(c).to( + receive(:asset_path) + .and_return(context.asset_path('main.js')) + ) # Spy + expect(c).to( + receive(:asset_path) + .with('main.js', {:expand => true, :debug => nil}) + ) c.asset_tag('main.js') {} Sprockets::Helpers.expand = false - c.should_receive(:asset_path).with('main.js', {:expand => false, :debug => nil}) + expect(c).to( + receive(:asset_path) + .with('main.js', {:expand => false, :debug => nil}) + ) c.asset_tag('main.js') {} end end it 'force to be debug mode when globally configured' do within_construct do |construct| assets_layout(construct) Sprockets::Helpers.debug = true c = context - c.stub(:asset_path).and_return(context.asset_path('main.js')) # Spy - c.should_receive(:asset_path).with('main.js', {:expand => true, :debug => true}) + allow(c).to( + receive(:asset_path) + .and_return(context.asset_path('main.js')) + ) # Spy + expect(c).to( + receive(:asset_path) + .with('main.js', {:expand => true, :debug => true}) + ) c.asset_tag('main.js') {} Sprockets::Helpers.debug = false - c.should_receive(:asset_path).with('main.js', {:expand => false, :debug => false}) + expect(c).to( + receive(:asset_path) + .with('main.js', {:expand => false, :debug => false}) + ) c.asset_tag('main.js') {} end end describe 'when expanding' do it 'passes uri that is no asset untouched' do context.asset_tag('main.js', :expand => true) {} end - it 'generates tag for each asset' do - within_construct do |construct| - assets_layout(construct) - tags = context.asset_tag('main.js', :expand => true) do |path| - "<script src=\"#{path}\"></script>" + context "in Sprockets 2.x" do + next if Sprockets::Helpers.are_using_sprockets_3 + it 'generates tag for each asset' do + within_construct do |construct| + assets_layout(construct) + tags = context.asset_tag('main.js', :expand => true) do |path| + "<script src=\"#{path}\"></script>" + end + expect(tags.split('</script>').size).to eq(3) + expect(tags).to include('<script src="/assets/main.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.js?body=1"></script>') end - expect(tags.split('</script>')).to have(3).scripts - expect(tags).to include('<script src="/assets/main.js?body=1"></script>') - expect(tags).to include('<script src="/assets/a.js?body=1"></script>') - expect(tags).to include('<script src="/assets/b.js?body=1"></script>') end end + + context "in Sprockets 3.x" do + next unless Sprockets::Helpers.are_using_sprockets_3 + it 'generates tag for each asset' do + within_construct do |construct| + assets_layout(construct) + tags = context.asset_tag('main.js', :expand => true) do |path| + "<script src=\"#{path}\"></script>" + end + expect(tags.split('</script>').size).to eq(3) + expect(tags).to include('<script src="/assets/main.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.self.js?body=1"></script>') + end + end + end end if defined?(Sprockets::Manifest) describe 'when globally debug mode is set' do it 'generates tag for each asset without reading the path from manifest file' do @@ -467,15 +511,22 @@ end tags = context.asset_tag('main.js') do |path| "<script src=\"#{path}\"></script>" end - expect(tags.split('</script>')).to have(3).scripts - expect(tags).to include('<script src="/assets/main.js?body=1"></script>') - expect(tags).to include('<script src="/assets/a.js?body=1"></script>') - expect(tags).to include('<script src="/assets/b.js?body=1"></script>') + expect(tags.split('</script>').size).to eq(3) + if Sprockets::Helpers.are_using_sprockets_3 + expect(tags).to include('<script src="/assets/main.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.self.js?body=1"></script>') + else + expect(tags).to include('<script src="/assets/main.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.js?body=1"></script>') + end + Sprockets::Helpers.debug = false Sprockets::Helpers.prefix = nil Sprockets::Helpers.manifest = nil end end @@ -495,19 +546,36 @@ it 'prepends the javascript dir' do expect(context.javascript_tag('main')).to eq('<script src="/javascripts/main.js"></script>') end describe 'when expanding' do - it 'generates script tag for each javascript asset' do - within_construct do |construct| - assets_layout(construct) - tags = context.javascript_tag('main.js', :expand => true) - expect(tags).to include('<script src="/assets/main.js?body=1"></script>') - expect(tags).to include('<script src="/assets/a.js?body=1"></script>') - expect(tags).to include('<script src="/assets/b.js?body=1"></script>') + context 'in Sprockets 3.x' do + next unless Sprockets::Helpers.are_using_sprockets_3 + it 'generates script tag for each javascript asset' do + within_construct do |construct| + assets_layout(construct) + tags = context.javascript_tag('main.js', :expand => true) + expect(tags).to include('<script src="/assets/main.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.self.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.self.js?body=1"></script>') + end end end + + context 'in Sprockets 2.x' do + next if Sprockets::Helpers.are_using_sprockets_3 + it 'generates script tag for each javascript asset' do + within_construct do |construct| + assets_layout(construct) + tags = context.javascript_tag('main.js', :expand => true) + + expect(tags).to include('<script src="/assets/main.js?body=1"></script>') + expect(tags).to include('<script src="/assets/a.js?body=1"></script>') + expect(tags).to include('<script src="/assets/b.js?body=1"></script>') + end + end + end end end describe '#stylesheet_tag' do it 'generates stylesheet tag' do @@ -525,23 +593,47 @@ it 'uses media attribute when provided' do expect(context.stylesheet_tag('main', :media => "print")).to eq('<link rel="stylesheet" href="/stylesheets/main.css" media="print">') end describe 'when expanding' do - it 'generates stylesheet tag for each stylesheet asset' do - within_construct do |construct| - assets_layout(construct) - tags = context.stylesheet_tag('main.css', :expand => true) - expect(tags).to include('<link rel="stylesheet" href="/assets/main.css?body=1">') - expect(tags).to include('<link rel="stylesheet" href="/assets/a.css?body=1">') - expect(tags).to include('<link rel="stylesheet" href="/assets/b.css?body=1">') + context 'in Sprockets 3.x' do + it 'generates stylesheet tag for each stylesheet asset' do + next unless Sprockets::Helpers.are_using_sprockets_3 + within_construct do |construct| + assets_layout(construct) + tags = context.stylesheet_tag('main.css', :expand => true) + expect(tags).to include('<link rel="stylesheet" href="/assets/main.self.css?body=1">') + expect(tags).to include('<link rel="stylesheet" href="/assets/a.self.css?body=1">') + expect(tags).to include('<link rel="stylesheet" href="/assets/b.self.css?body=1">') + end end end + + context "in Sprockets 2.x" do + next if Sprockets::Helpers.are_using_sprockets_3 + it 'generates stylesheet tag for each stylesheet asset' do + within_construct do |construct| + assets_layout(construct) + tags = context.stylesheet_tag('main.css', :expand => true) + expect(tags).to include('<link rel="stylesheet" href="/assets/main.css?body=1">') + expect(tags).to include('<link rel="stylesheet" href="/assets/a.css?body=1">') + expect(tags).to include('<link rel="stylesheet" href="/assets/b.css?body=1">') + end + end + end end end describe 'Sinatra integration' do + after do + ::Sprockets::Helpers.environment = nil + ::Sprockets::Helpers.public_path = nil + ::Sprockets::Helpers.digest = nil + ::Sprockets::Helpers.prefix = nil + ::Sprockets::Helpers.expand = nil + end + it 'adds the helpers' do app = Class.new(Sinatra::Base) do register Sinatra::Sprockets::Helpers end @@ -559,13 +651,27 @@ register Sinatra::Sprockets::Helpers end expect(Sprockets::Helpers.environment).to be(custom_env) expect(Sprockets::Helpers.prefix).to eq('/static') - expect(Sprockets::Helpers.digest).to be_true + expect(Sprockets::Helpers.digest).to be_truthy end + it 'uses first prefix if assets_prefix is an array' do + custom_env = Sprockets::Environment.new + + app = Class.new(Sinatra::Base) do + set :sprockets, custom_env + set :assets_prefix, %w(assets vendor/assets) + + register Sinatra::Sprockets::Helpers + end + + expect(Sprockets::Helpers.environment).to be(custom_env) + expect(Sprockets::Helpers.prefix).to eq('/assets') + end + it 'manually configures with configure method' do custom_env = Sprockets::Environment.new app = Class.new(Sinatra::Base) do register Sinatra::Sprockets::Helpers @@ -578,9 +684,9 @@ end end expect(Sprockets::Helpers.environment).to be(custom_env) expect(Sprockets::Helpers.prefix).to eq('/static') - expect(Sprockets::Helpers.expand).to be_true + expect(Sprockets::Helpers.expand).to be_truthy end end end