require 'spec_helper' module Deface describe Override do before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Argh!

") end it "should return correct action" do Deface::Override.actions.each do |action| @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", action => "h1", :text => "

Argh!

") @override.action.should == action end end it "should return correct selector" do @override.selector.should == "h1" end describe "#original_source" do it "should return nil with not specified" do @override.original_source.should be_nil end it "should return parsed nokogiri document when present" do @original = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Argh!

", :original => "

<%= something %>

") @original.original_source.should be_an_instance_of Nokogiri::HTML::DocumentFragment @original.original_source.to_s.should == "

something

" end end describe "#validate_original" do before(:each) do @original = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Argh!

", :original => "

<%= something %>

") end it "should return true when :original is not present" do @override.validate_original("").should be_true end it "should return true when :original present, and input contains similar (ignoring whitespace)" do @original.validate_original("

something

").should be_true @original.validate_original("

something\n

").should be_true end it "should return false when :original present, and input contains different string" do @original.validate_original("wrong").should be_false end end describe "#find" do it "should find by virtual_path" do Deface::Override.find({:virtual_path => "posts/index"}).size.should == 1 end it "should return empty array when no details hash passed" do Deface::Override.find({}).should == [] end end describe "#new" do it "should increase all#size by 1" do expect { Deface::Override.new(:virtual_path => "posts/new", :name => "Posts#new", :replace => "h1", :text => "

argh!

") }.to change{Deface::Override.all.size}.by(1) end end describe "with :text" do before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

\">Argh!

") end it "should return un-convert text as source" do @override.source.should == "

\">Argh!

" end end describe "with :partial" do before(:each) do #stub view paths to be local spec/assets directory ActionController::Base.stub(:view_paths).and_return([File.join(File.dirname(__FILE__), '..', "assets")]) @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :partial => "shared/post") end it "should return un-convert partial contents as source" do @override.source.should == "

I'm from shared/post partial

\n<%= \"And I've got ERB\" %>\n" end end describe "with :template" do before(:each) do #stub view paths to be local spec/assets directory ActionController::Base.stub(:view_paths).and_return([File.join(File.dirname(__FILE__), '..', "assets")]) @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :template => "shared/person") end it "should return un-convert template contents as source" do @override.source.should == "

I'm from shared/person template

\n<%= \"I've got ERB too\" %>\n" end end describe "#source_element" do before(:each) do @override = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "<%= method :opt => 'x' & 'y' %>") end it "should return escaped source" do @override.source_element.should be_an_instance_of Nokogiri::HTML::DocumentFragment @override.source_element.to_s.should == " method :opt => 'x' & 'y' " #do it twice to ensure it doesn't change as it's destructive @override.source_element.to_s.should == " method :opt => 'x' & 'y' " end end describe "when redefining an existing virutal_path and name" do before(:each) do @replacement = Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Arrrr!

") end it "should not increase all#size by 1" do expect { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :text => "

Arrrr!

") }.to change{Deface::Override.all.size}.by(0) end it "should return new source" do @replacement.source.should_not == @override.source @replacement.source.should == "

Arrrr!

" end end end end