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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error does not generate content for attribute without errors' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_error_for @user, :active
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error does not generate messages when object is not present' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_error_for :project, :name
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test "error does not generate messages when object doesn't respond to errors method" do
|
2011-09-03 12:36:02 -04:00
|
|
|
@user.instance_eval { undef errors }
|
|
|
|
with_error_for @user, :name
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates messages for attribute with single error' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_error_for @user, :name
|
|
|
|
assert_select 'span.error', "can't be blank"
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error is 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error does 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error finds 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates an error tag with a clean HTML' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_error_for @user, :name
|
|
|
|
assert_no_select 'span.error[error_html]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error escapes 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'error generates an error message with raw HTML tags' do
|
2013-11-26 20:47:12 -05:00
|
|
|
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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'full error generates a 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'full error generates a 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'full error allows 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
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'full error does 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
|
|
|
|
|
2014-05-14 13:18:43 -04:00
|
|
|
test 'full error finds errors on association' do
|
2014-04-03 11:08:29 -04:00
|
|
|
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
|
2014-04-03 16:31:54 -04:00
|
|
|
|
2014-09-29 13:50:01 -04:00
|
|
|
test 'full error finds errors on association with reflection' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
|
|
|
with_form_for @user, :company_id, as: :select,
|
|
|
|
reflection: Association.new(Company, :company, {})
|
|
|
|
assert_select 'span.error', 'Company must be valid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-03 17:00:07 -04:00
|
|
|
test 'full error can be disabled' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
|
|
|
with_form_for @user, :company_id, as: :select, full_error: false
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'full error can be disabled setting error to false' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
|
|
|
with_form_for @user, :company_id, as: :select, error: false
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
end
|
2014-04-03 17:01:50 -04:00
|
|
|
|
2014-01-26 08:09:19 -05:00
|
|
|
# CUSTOM ERRORS
|
2014-04-03 16:31:54 -04:00
|
|
|
|
2014-01-26 08:09:19 -05:00
|
|
|
test 'input with custom error works' do
|
|
|
|
error_text = "Super User Name! can't be blank"
|
2014-04-03 16:43:03 -04:00
|
|
|
with_form_for @user, :name, error: error_text
|
2014-04-03 16:31:54 -04:00
|
|
|
|
2014-04-03 17:18:17 -04:00
|
|
|
assert_select 'span.error', error_text
|
2014-01-26 08:09:19 -05:00
|
|
|
end
|
2014-04-03 16:38:25 -04:00
|
|
|
|
2014-04-03 17:28:28 -04:00
|
|
|
test 'input with error option as true does not use custom error' do
|
2014-04-03 17:24:17 -04:00
|
|
|
with_form_for @user, :name, error: true
|
|
|
|
|
|
|
|
assert_select 'span.error', "can't be blank"
|
|
|
|
end
|
|
|
|
|
2014-04-03 17:18:00 -04:00
|
|
|
test 'input with custom error does not generate the error if there is no error on the attribute' do
|
2014-04-03 17:24:30 -04:00
|
|
|
with_form_for @user, :active, error: "Super User Active! can't be blank"
|
2014-04-03 17:18:00 -04:00
|
|
|
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
|
2014-04-03 16:38:25 -04:00
|
|
|
test 'input with custom error works when using full_error component' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
|
|
|
error_text = "Super User Name! can't be blank"
|
2014-04-03 16:43:03 -04:00
|
|
|
with_form_for @user, :name, error: error_text
|
2014-04-03 16:38:25 -04:00
|
|
|
|
2014-04-03 17:18:17 -04:00
|
|
|
assert_select 'span.error', error_text
|
2014-04-03 16:38:25 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-03 17:18:00 -04:00
|
|
|
|
|
|
|
test 'input with custom error when using full_error component does not generate the error if there is no error on the attribute' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
2014-04-03 17:24:30 -04:00
|
|
|
with_form_for @user, :active, error: "Super User Active! can't be blank"
|
2014-04-03 17:18:00 -04:00
|
|
|
|
|
|
|
assert_no_select 'span.error'
|
|
|
|
end
|
|
|
|
end
|
2011-09-03 12:36:02 -04:00
|
|
|
end
|