require 'spec_helper' module ActionView describe Template do before(:all) do Deface::Override.all.clear end describe "with no overrides defined" do before(:each) do @updated_at = Time.now - 600 @template = ActionView::Template.new("

test

", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html, :updated_at => @updated_at}) #stub for Rails < 3.1 unless defined?(@template.updated_at) @template.stub(:updated_at).and_return(@updated_at) end end it "should initialize new template object" do @template.is_a?(ActionView::Template).should == true end it "should return unmodified source" do @template.source.should == "

test

" end it "should not change updated_at" do @template.updated_at.should == @updated_at end end describe "with a single remove override defined" do before(:each) do @updated_at = Time.now - 300 Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "

Argh!

") @template = ActionView::Template.new("

test

<%= raw(text) %>", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html, :updated_at => @updated_at}) #stub for Rails < 3.1 unless defined?(@template.updated_at) @template.stub(:updated_at).and_return(@updated_at + 500) end end it "should return modified source" do @template.source.should == "<%= raw(text) %>" end it "should change updated_at" do @template.updated_at.should > @updated_at end end describe "with a single replace override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "p", :text => "

Argh!

") @template = ActionView::Template.new("

test

", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.should == "

Argh!

" end end describe "with a single insert_after override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "img.button", :text => "<% help %>") @template = ActionView::Template.new("
", "/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.gsub("\n", "").should == "
<% help %>
" end end describe "with a single insert_before override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "ul li:last", :text => "<%= help %>") @template = ActionView::Template.new("", "/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.gsub("\n", "").should == "" end end describe "with a single insert_top override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_top => "ul", :text => "
  • me first
  • ") @template = ActionView::Template.new("", "/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.gsub("\n", "").should == "" end end describe "with a single insert_bottom override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_bottom => "ul", :text => "
  • I'm always last
  • ") @template = ActionView::Template.new("", "/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.gsub("\n", "").should == "" end end describe "with a single disabled override defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "

    Argh!

    ", :disabled => true) @template = ActionView::Template.new("

    test

    <%= raw(text) %>", "/some/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return unmodified source" do @template.source.should == "

    test

    <%= raw(text) %>" end end describe "with mulitple sequenced overrides defined" do before(:all) do Deface::Override.new(:virtual_path => "posts/index", :name => "third", :insert_after => "li:contains('second')", :text => "
  • third
  • ", :sequence => {:after => "second"}) Deface::Override.new(:virtual_path => "posts/index", :name => "second", :insert_after => "li", :text => "
  • second
  • ", :sequence => {:after => "first"}) Deface::Override.new(:virtual_path => "posts/index", :name => "first", :replace => "li", :text => "
  • first
  • ") @template = ActionView::Template.new("", "/path/to/file.erb", ActionView::Template::Handlers::ERB, {:virtual_path=>"posts/index", :format=>:html}) end it "should return modified source" do @template.source.gsub("\n", "").should == "" end end end end