require 'html/spec_helper'
describe "ModelHelper" do
before do
class MockModelHelperContext < Rad::MockTemplateContext
inherit Rad::Html::FormHelper
end
@t = MockModelHelperContext.new
end
it "form_for" do
@t.form_for(:aname, nil, id: 'the_form', action: '/'){"content"}
@t.buffer.to_xhtml('#the_form').should_be_fuzzy_equal_to(
id: 'the_form', action: '/', content: 'content', method: 'post'
)
end
it "error_messages" do
@t.form_for(:aname, nil){|f| f.error_messages}
lambda{@t.buffer.to_xhtml('form div')}.should raise_error(/not found/)
model = {errors: {base: ['some error']}}
@t.buffer = ""
@t.form_for(:book, model){|f| f.error_messages}
@t.buffer.to_xhtml('form div').should_be_fuzzy_equal_to class: 'error_messages', content: 'some error'
end
it "field error" do
model = {
title: "Super Hero",
errors: {title: ['some error in title']}
}
@t.form_for(:book, model){|f| f.text_field(:title)}
doc = @t.buffer.to_xhtml
doc.css("form div.field_error_messages").first.content.should == "some error in title"
doc.css("form span.field_with_errors input").first.should_be_fuzzy_equal_to name: "book[title]", value: "Super Hero"
end
it "should insert human readable label if not specified" do
model = {}
model.stub(:t).and_return{|k| "translated #{k}"}
@t.form_for(:book, model){|f| f.text_field(:title)}
@t.buffer.should =~ /translated title/
@t.form_for(:book, model){|f| f.text_field(:title, label: 'some label')}
@t.buffer.should =~ /some label/
end
it "field_helpers" do
model = {
available: false,
title: "Super Hero",
theme: 'simple'
}
@t.form_for(:book, model){|f| %{
#{f.check_box :available}
#{f.file_field :title}
#{f.hidden_field :title}
#{f.password_field :title}
#{f.radio_button :available}
#{f.submit 'Ok'}
#{f.text_field :title}
#{f.text_area :title}
#{f.select :theme, %w(a b)}
} }
doc = @t.buffer.to_xhtml
# checkbox is special case, we are using it with tag.
doc.css("*[type='hidden']").first.should_be_fuzzy_equal_to name: "book[available]", value: '0'
doc.css("*[type='checkbox']").first.should_be_fuzzy_equal_to name: "book[available]", value: '1'
doc.css("*[type='file']").first.should_be_fuzzy_equal_to name: "book[title]"
doc.css("*[type='hidden']").last.should_be_fuzzy_equal_to name: "book[title]", value: 'Super Hero'
doc.css("*[type='password']").first.should_be_fuzzy_equal_to name: "book[title]"
doc.css("*[type='radio']").first.should_be_fuzzy_equal_to name: "book[available]", value: '1'
doc.css("*[type='submit']").first.should_be_fuzzy_equal_to value: 'Ok'
doc.css("*[type='text']").first.should_be_fuzzy_equal_to name: "book[title]", value: 'Super Hero'
doc.css("textarea").first.should_be_fuzzy_equal_to name: "book[title]", content: 'Super Hero'
doc.css("select").first.should_be_fuzzy_equal_to name: "book[theme]"
end
end