require 'spec_helper'
describe CrazyHarry::Change do
# Change br to p
# Change b to h3 where b has text: "location"
# Change b to em where b inside p
#
# Add attribute: "partner" to all tags
#
# Another ADS suggestion. Not sure it if it needed for the first cut.
# Transform "lodging" to "hotel"
context "change" do
let(:harry){ CrazyHarry }
it "should be able to change one tag to another" do
harry.fragment( 'Location:' ).change!( from: 'b', to: 'h3' ).to_s.should == '
Location:
'
end
it "should unwrap unnecessary paragraphs" do
harry.fragment('Header
').change!(from: 'strong', to: 'h3').to_s.should ==
'Header
'
end
it "should not unwrap paragraphs indiscriminately" do
harry.fragment('Header
Content
').change!(from: 'b', to: 'h3').to_s.should ==
'Header
Content'
end
it "should wrap orphaned list items" do
harry.fragment('orphan- item
').change!(from: 'b', to: 'h3').to_s.should ==
''
end
context "chaining" do
it "should be able to chain calls" do
harry.fragment( 'Location:
Saigon' ).change!( from: 'b', to: 'h3').change!( from: 'br', to: 'p' ).to_s.should ==
'Location:
Saigon
'
end
it "should not care about the order of the chain" do
harry.fragment( 'Location:
Saigon' ).change!( from: 'br', to: 'p' ).change!( from: 'b', to: 'h3' ).to_s.should ==
'Location:
Saigon
'
end
end
it "should be able to change all the tags it finds" do
harry.fragment( '123' ).change!( from: 'b', to: 'h3' ).to_s.should == '1
2
3
'
end
it "should be able to change a span tag to a div" do
harry.fragment( 'Hotel Details' ).change!( from: 'span', to: 'div' ).to_s.should == 'Hotel Details
'
end
it "should not change things that it isn't directed to" do
harry.fragment( 'Hotel Details
' ).change!( from: 'span', to: 'div' ).to_s.should == 'Hotel Details
'
end
it "shouldn't choke if it gets invalid tags" do
harry.fragment( 'Hotel Details
' ).change!( from: 'snoogenflozen', to: 'snarglebat' ).to_s.should == 'Hotel Details
'
end
context "targeting and scoping" do
it "should change all occurrences" do
harry.fragment( 'Hotels
Hotels
Tents' ).change!( from: 'b', to: 'em').to_s.should ==
'Hotels
Hotels
Tents'
end
it "should be able to target changes by content" do
harry.fragment( 'Hot hotels in Saigon' ).change!( from: 'b', to: 'em', text: 'hotels' ).to_s.should ==
'Hot hotels in Saigon'
end
it "should be able to scope changes to specific blocks" do
harry.fragment( 'Hotels
Hotels
Tents' ).change!( from: 'b', to: 'em', scope: 'p' ).to_s.should ==
'Hotels
Hotels
Tents'
end
end
end
end