require 'spec_helper'
describe Arbo do
let(:helpers){ nil }
let(:assigns){ {} }
def output_buffer(actx)
actx.render_in(actx) && actx.output_buffer
end
it "should render a single element" do
actx = arbo {
span "Hello World"
}
html = "Hello World\n"
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should render a child element" do
actx = arbo {
span do
span "Hello World"
end
}
html = <<-HTML
Hello World
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should render an unordered list" do
actx = arbo {
ul do
li "First"
li "Second"
li "Third"
end
}
html = <<-HTML
First
Second
Third
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should allow local variables inside the tags" do
actx = arbo {
first = "First"
second = "Second"
ul do
li first
li second
end
}
html = <<-HTML
First
Second
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should add children and nested" do
actx = arbo {
div do
ul
li do
li
end
end
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should pass the element in to the block if asked for" do
actx = arbo {
div do |d|
d.ul do
li
end
end
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should move content tags between parents" do
actx = arbo {
div do
span(ul(li))
end
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should add content to the parent if the element is passed into block" do
actx = arbo {
div do |d|
d.id = "my-tag"
ul do
li
end
end
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should have the parent set on it" do
list, item = nil
arbo {
list = ul do
li "Hello"
item = li "World"
end
}
expect(item.parent).to eq list
end
it "should set a string content return value with no children" do
actx = arbo {
li do
"Hello World"
end
}
html = <<-HTML
Hello World
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should turn string return values into text nodes" do
node = nil
arbo {
list = li do
"Hello World"
end
node = list.children.first
}
expect(node).to be_a Arbo::HTML::TextNode
end
it "should not render blank arrays" do
actx = arbo {
tbody do
[]
end
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
describe "self-closing nodes" do
it "should not self-close script tags" do
actx = arbo {
script type: 'text/javascript'
}
html = "\n"
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should self-close meta tags" do
actx = arbo {
meta content: "text/html; charset=utf-8"
}
html = "\n"
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should self-close link tags" do
actx = arbo {
link rel: "stylesheet"
}
html = "\n"
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
Arbo::HTML::Tag::SELF_CLOSING_ELEMENTS.each do |tag|
it "should self-close #{tag} tags" do
actx = arbo {
send(tag)
}
html = "<#{tag}/>\n"
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
end
end
describe "html safe" do
it "should escape the contents" do
actx = arbo {
span(" ")
}
html = <<-HTML
<br />
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should return html safe strings" do
expect(arbo {
span(" ")
}.to_s).to be_html_safe
end
it "should not escape html passed in" do
actx = arbo {
span(span(" "))
}
html = <<-HTML
<br />
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should escape string contents when passed in block" do
actx = arbo {
span {
span {
" "
}
}
}
html = <<-HTML
<br />
HTML
expect(actx.to_s).to eq(html)
expect(output_buffer actx).to eq(html)
end
it "should escape the contents of attributes" do
actx = arbo {
span(class: " ")
}
html = <<-HTML
HTML
expect(actx.to_s).to eq(html)
end
end
end