2011-09-03 12:36:02 -04:00
|
|
|
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
|
2011-09-03 12:36:02 -04:00
|
|
|
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
|
2011-09-03 12:36:02 -04:00
|
|
|
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'
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'span#error.error.yay'
|
|
|
|
end
|
|
|
|
|
2012-02-20 22:42:06 -05:00
|
|
|
test 'error should not modify the options hash' do
|
2013-01-28 16:02:59 -05:00
|
|
|
options = { id: 'error', class: 'yay' }
|
2012-02-20 22:42:06 -05:00
|
|
|
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)
|
2012-02-20 22:42:06 -05:00
|
|
|
end
|
|
|
|
|
2011-09-03 12:36:02 -04:00
|
|
|
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, {})
|
2011-09-03 12:36:02 -04:00
|
|
|
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
|
2011-09-03 12:36:02 -04:00
|
|
|
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
|
|
|
|
|
2013-11-26 20:47:12 -05:00
|
|
|
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>'
|
2013-11-26 20:47:12 -05:00
|
|
|
assert_select 'span.error', "<b>Name</b> 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
|
2012-02-16 12:07:03 -05:00
|
|
|
assert_select 'span.error', "Name can't be blank"
|
|
|
|
assert_select 'span.error b', "Name"
|
|
|
|
end
|
|
|
|
|
2011-09-03 12:36:02 -04:00
|
|
|
# FULL ERRORS
|
|
|
|
|
2011-09-03 13:04:40 -04:00
|
|
|
test 'full error should generate an full error tag for the attribute' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_full_error_for @user, :name
|
|
|
|
assert_select 'span.error', "Super User Name! can't be blank"
|
|
|
|
end
|
|
|
|
|
2012-02-20 22:42:06 -05:00
|
|
|
test 'full error should generate an full error tag with a clean HTML' do
|
2011-09-03 12:36:02 -04:00
|
|
|
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"
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'span.error#name_error', "Your name can't be blank"
|
|
|
|
end
|
2011-09-03 13:04:40 -04:00
|
|
|
|
2012-02-20 22:42:06 -05:00
|
|
|
test 'full error should not modify the options hash' do
|
2013-01-28 16:02:59 -05:00
|
|
|
options = { id: 'name_error' }
|
2012-02-20 22:42:06 -05:00
|
|
|
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)
|
2012-02-20 22:42:06 -05:00
|
|
|
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
|
2014-04-03 11:08:29 -04:00
|
|
|
|
|
|
|
# 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
|
2011-09-03 12:36:02 -04:00
|
|
|
end
|