1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/test/form_builder/error_test.rb

136 lines
4.4 KiB
Ruby
Raw Normal View History

require 'test_helper'
# Tests for f.error and f.full_error
class ErrorTest < ActionView::TestCase
def with_error_for(object, *args)
with_concat_form_for(object) do |f|
f.error(*args)
end
end
def with_full_error_for(object, *args)
with_concat_form_for(object) do |f|
f.full_error(*args)
end
end
test 'error should not generate content for attribute without errors' do
with_error_for @user, :active
assert_no_select 'span.error'
end
test 'error should not generate messages when object is not present' do
with_error_for :project, :name
assert_no_select 'span.error'
end
test "error should not generate messages when object doesn't respond to errors method" do
@user.instance_eval { undef errors }
with_error_for @user, :name
assert_no_select 'span.error'
end
test 'error should generate messages for attribute with single error' do
with_error_for @user, :name
assert_select 'span.error', "can't be blank"
end
test 'error should generate messages for attribute with one error when using first' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, error_method: :first do
with_error_for @user, :age
assert_select 'span.error', 'is not a number'
end
end
test 'error should generate messages for attribute with several errors when using to_sentence' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, error_method: :to_sentence do
with_error_for @user, :age
assert_select 'span.error', 'is not a number and must be greater than 18'
end
end
test 'error should be able to pass html options' do
2013-01-28 16:02:59 -05:00
with_error_for @user, :name, id: 'error', class: 'yay'
assert_select 'span#error.error.yay'
end
test 'error should not modify the options hash' do
2013-01-28 16:02:59 -05:00
options = { id: 'error', class: 'yay' }
with_error_for @user, :name, options
assert_select 'span#error.error.yay'
2013-01-28 16:02:59 -05:00
assert_equal({ id: 'error', class: 'yay' }, options)
end
test 'error should find errors on attribute and association' do
2013-01-28 16:02:59 -05:00
with_error_for @user, :company_id, as: :select,
error_method: :to_sentence, reflection: Association.new(Company, :company, {})
assert_select 'span.error', 'must be valid and company must be present'
end
test 'error should generate an error tag with a clean HTML' do
with_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
test 'error should generate an error tag with a clean HTML when errors options are present' do
2013-01-28 16:02:59 -05:00
with_error_for @user, :name, error_tag: :p, error_prefix: 'Name', error_method: :first
assert_no_select 'p.error[error_html]'
assert_no_select 'p.error[error_tag]'
assert_no_select 'p.error[error_prefix]'
assert_no_select 'p.error[error_method]'
end
test 'error should escape error prefix text' do
2013-01-28 16:02:59 -05:00
with_error_for @user, :name, error_prefix: '<b>Name</b>'
assert_select 'span.error', "&lt;b&gt;Name&lt;/b&gt; can't be blank"
end
test 'error should generate an error message with raw HTML tags' do
with_error_for @user, :name, error_prefix: '<b>Name</b>'.html_safe
assert_select 'span.error', "Name can't be blank"
assert_select 'span.error b', "Name"
end
# FULL ERRORS
2011-09-03 13:04:40 -04:00
test 'full error should generate an full error tag for the attribute' do
with_full_error_for @user, :name
assert_select 'span.error', "Super User Name! can't be blank"
end
test 'full error should generate an full error tag with a clean HTML' do
with_full_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
2011-09-03 13:04:40 -04:00
test 'full error should allow passing options to full error tag' do
2013-01-28 16:02:59 -05:00
with_full_error_for @user, :name, id: 'name_error', error_prefix: "Your name"
assert_select 'span.error#name_error', "Your name can't be blank"
end
2011-09-03 13:04:40 -04:00
test 'full error should not modify the options hash' do
2013-01-28 16:02:59 -05:00
options = { id: 'name_error' }
with_full_error_for @user, :name, options
assert_select 'span.error#name_error', "Super User Name! can't be blank"
2013-01-28 16:02:59 -05:00
assert_equal({ id: 'name_error' }, options)
end
2011-09-03 13:04:40 -04:00
# CUSTOM WRAPPERS
test 'error with custom wrappers works' do
2011-09-04 05:31:24 -04:00
swap_wrapper do
2011-09-03 13:04:40 -04:00
with_error_for @user, :name
assert_select 'span.omg_error', "can't be blank"
end
end
# FULL_ERROR_WRAPPER
test 'full error should find errors on association' do
swap_wrapper :default, self.custom_wrapper_with_full_error do
with_form_for @user, :company_id, as: :select
assert_select 'span.error', 'Company must be valid'
end
end
end