require "spec_helper" describe Form::Tag do subject { described_class.new(:p) } describe "#initialize" do it "yields instance" do tag = nil described_class.new(:p) {|t| tag = t} tag.should be_a(described_class) end it "evaluates block" do tag = nil described_class.new(:p) { tag = self } tag.should be_a(described_class) end context "argument position" do it "moves position" do tag = described_class.new(:p, class: "hello") tag.attributes.should eql(class: "hello") tag.content.should eql("") end it "keeps position" do tag = described_class.new(:p, "Hello", class: "hello") tag.attributes.should eql(class: "hello") tag.content.should eql("Hello") end end end describe "#<<" do it "appends content" do tag = described_class.new(:p) tag << "Hello" tag.to_s.should eql("

Hello

") end end describe "#void?" do it "detects void tag" do subject = described_class.new(:br) subject.should be_void end it "rejects as open tag" do subject = described_class.new(:p) subject.should_not be_void end end describe "#to_s" do it "renders tag" do subject = described_class.new(:p, "Hello") subject.to_s.should eql("

Hello

") end it "renders tag with attributes" do subject = described_class.new(:p, "Hello", class: "hello") subject.to_s.should eql(%[

Hello

]) end it "renders open tag" do subject = described_class.new(:hr) subject.to_s.should eql("
") end it "renders open tag with attributes" do subject = described_class.new(:hr, class: "separator") subject.to_s.should eql(%[
]) end it "renders boolean attributes" do subject = described_class.new(:input, autofocus: true) subject.to_s.should eql("") end it "skips attributes with false value" do subject = described_class.new(:input, autofocus: false) subject.to_s.should eql("") end it "skips attributes with empty value" do subject = described_class.new(:input, value: "") subject.to_s.should eql("") end it "escapes attribute value" do subject = described_class.new(:img, alt: "<3") subject.to_s.should eql(%[<3]) end end describe "#tag" do it "renders simple tag" do subject = described_class.new(:p) subject.tag(:strong, "Hello") subject.to_s.should eql("

Hello

") end it "renders complex tag" do subject = described_class.new(:form, action: "some/path") do |form| form.tag :p do |p| p.tag :label, "E-mail", for: "user_email" p.tag :input, type: "email", autofocus: true end end subject.to_s.should have_tag("form[action='some/path']") do |form| form.should have_tag("p") do |p| p.should have_tag("label[for=user_email]") p.should have_tag("input[type=email][autofocus]") end end end end end