require 'spec_helper' def attributes_to_sorted_array(src) Nokogiri::HTML::DocumentFragment.parse(src).children.first.attributes end module Deface describe Applicator do include_context "mock Rails.application" before { Dummy.all.clear } describe "with a single disabled override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :remove => "p", :text => "

Argh!

", :disabled => true) } let(:source) { "

test

<%= raw(text) %>" } it "should return unmodified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"})).to eq("

test

<%= raw(text) %>") end end describe "with a single :copy override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "p", :copy => "h1") } let(:source) { "

World

Hello

" } it "should return modified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"})).to eq("

World

Hello

World

") end end describe "with a single :copy using :start and :end" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_before => "h1", :copy => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) } let(:source) { "

World

<% if true %>

True that!

<% end %>

Hello

" } it "should return modified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"})).to eq("<% if true %>

True that!

<% end %>

World

<% if true %>

True that!

<% end %>

Hello

") end end describe "with a single :cut override defined" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :insert_after => "p", :cut => "h1") } let(:source) { "

World

Hello

" } it "should return modified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"})).to eq("

Hello

World

") end end describe "with a single :cut using :start and :end" do before { Deface::Override.new(:virtual_path => "posts/index", :name => "Posts#index", :replace => "h1", :cut => {:start => "erb:contains('if true')", :end => "erb:contains('end')"}) } let(:source) { "

World

<% if true %>

True that!

<% end %>

Hello

" } it "should return modified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"})).to eq("<% if true %>

True that!

<% end %>

Hello

") end end describe "with mulitple sequenced overrides defined" do before 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
  • ") end let(:source) { "" } it "should return modified source" do expect(Dummy.apply(source, {:virtual_path => "posts/index"}).gsub("\n", "")).to eq("") end end describe "with incompatible actions and :closing_selector" do let(:source) { "" } it "should return modified source" do [:insert_before, :insert_after, :insert_top, :insert_bottom, :set_attributes, :remove_from_attributes, :add_to_attributes].each do |action| Deface::Override.all.clear Deface::Override.new(:virtual_path => "posts/index", :name => "first", action => "li", :closing_selector => "p", :text => "
  • first
  • ") expect { Dummy.apply(source, {:virtual_path => "posts/index"}) }.to raise_error(Deface::NotSupportedError) end end end end end