2011-09-03 12:36:02 -04:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class RequiredTest < ActionView::TestCase
|
|
|
|
# REQUIRED AND PRESENCE VALIDATION
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input obtains required from ActiveModel::Validations when it is included' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :name
|
|
|
|
assert_select 'input.required[required]#validating_user_name'
|
|
|
|
with_form_for @validating_user, :status
|
|
|
|
assert_select 'input.optional#validating_user_status'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input allows overriding required when ActiveModel::Validations is included' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @validating_user, :name, required: false
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'input.optional#validating_user_name'
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @validating_user, :status, required: true
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_select 'input.required[required]#validating_user_status'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input is required by default when ActiveModel::Validations is not included' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'input.required[required]#user_name'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input does not be required by default when ActiveModel::Validations is not included if option is set to false' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, required_by_default: false do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'input.optional#user_name'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'when not using browser validations, input does not generate required html attribute' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, browser_validations: false do
|
2012-01-26 08:54:04 -05:00
|
|
|
with_input_for @user, :name, :string
|
|
|
|
assert_select 'input[type=text].required'
|
|
|
|
assert_no_select 'input[type=text][required]'
|
2016-08-13 16:34:02 -04:00
|
|
|
assert_no_select 'input[type=text][aria-required]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'when not using browser validations, when required option is set to false, input does not generate required html attribute' do
|
|
|
|
swap SimpleForm, browser_validations: false do
|
|
|
|
with_input_for @user, :name, :string, required: false
|
|
|
|
assert_no_select 'input[type=text].required'
|
|
|
|
assert_no_select 'input[type=text][required]'
|
|
|
|
assert_no_select 'input[type=text][aria-required]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'when not using browser validations, when required option is set to true, input generates required html attribute' do
|
|
|
|
swap SimpleForm, browser_validations: false do
|
|
|
|
with_input_for @user, :name, :string, required: true
|
|
|
|
assert_select 'input[type=text].required'
|
|
|
|
assert_select 'input[type=text][required]'
|
|
|
|
assert_select 'input[type=text][aria-required]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'when not using browser validations, when required option is true in the wrapper, input does not generate required html attribute' do
|
|
|
|
swap SimpleForm, browser_validations: false do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_required_input do
|
|
|
|
with_concat_form_for(@user) do |f|
|
|
|
|
concat f.input :name
|
|
|
|
end
|
|
|
|
assert_select 'input[type=text].required'
|
|
|
|
assert_no_select 'input[type=text][required]'
|
|
|
|
assert_no_select 'input[type=text][aria-required]'
|
|
|
|
end
|
2012-01-26 08:54:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input allows disabling required when ActiveModel::Validations is not included' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_form_for @user, :name, required: false
|
2011-09-03 12:36:02 -04:00
|
|
|
assert_no_select 'input.required'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
assert_select 'input.optional#user_name'
|
|
|
|
end
|
|
|
|
|
2011-12-04 18:37:45 -05:00
|
|
|
test 'when not the required component the input does not have the required attribute but has the required class' do
|
|
|
|
swap_wrapper do
|
|
|
|
with_input_for @user, :name, :string
|
|
|
|
assert_select 'input[type=text].required'
|
|
|
|
assert_no_select 'input[type=text][required]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-03 12:36:02 -04:00
|
|
|
# VALIDATORS :if :unless
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input does not be required when ActiveModel::Validations is included and if option is present' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :age
|
|
|
|
assert_no_select 'input.required'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
assert_select 'input.optional#validating_user_age'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input does not be required when ActiveModel::Validations is included and unless option is present' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :amount
|
|
|
|
assert_no_select 'input.required'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
assert_select 'input.optional#validating_user_amount'
|
|
|
|
end
|
|
|
|
|
|
|
|
# VALIDATORS :on
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input is required when validation is on create and is not persisted' do
|
2011-09-03 12:36:02 -04:00
|
|
|
@validating_user.new_record!
|
|
|
|
with_form_for @validating_user, :action
|
|
|
|
assert_select 'input.required'
|
|
|
|
assert_select 'input[required]'
|
|
|
|
assert_select 'input.required[required]#validating_user_action'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input does not be required when validation is on create and is persisted' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :action
|
|
|
|
assert_no_select 'input.required'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
assert_select 'input.optional#validating_user_action'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input is required when validation is on save' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :credit_limit
|
|
|
|
assert_select 'input.required'
|
|
|
|
assert_select 'input[required]'
|
|
|
|
assert_select 'input.required[required]#validating_user_credit_limit'
|
|
|
|
|
|
|
|
@validating_user.new_record!
|
|
|
|
with_form_for @validating_user, :credit_limit
|
|
|
|
assert_select 'input.required'
|
|
|
|
assert_select 'input[required]'
|
|
|
|
assert_select 'input.required[required]#validating_user_credit_limit'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input is required when validation is on update and is persisted' do
|
2011-09-03 12:36:02 -04:00
|
|
|
with_form_for @validating_user, :phone_number
|
|
|
|
assert_select 'input.required'
|
|
|
|
assert_select 'input[required]'
|
|
|
|
assert_select 'input.required[required]#validating_user_phone_number'
|
|
|
|
end
|
|
|
|
|
2014-05-14 13:19:06 -04:00
|
|
|
test 'builder input does not be required when validation is on update and is not persisted' do
|
2011-09-03 12:36:02 -04:00
|
|
|
@validating_user.new_record!
|
|
|
|
with_form_for @validating_user, :phone_number
|
|
|
|
assert_no_select 'input.required'
|
|
|
|
assert_no_select 'input[required]'
|
|
|
|
assert_select 'input.optional#validating_user_phone_number'
|
|
|
|
end
|
2015-05-03 13:02:09 -04:00
|
|
|
|
|
|
|
test 'builder input does not generate required html attribute when option is set to false when it is set to true in wrapper' do
|
|
|
|
swap SimpleForm, browser_validations: true do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_with_required_input do
|
|
|
|
with_concat_form_for(@user) do |f|
|
|
|
|
concat f.input :name, required: false
|
|
|
|
end
|
|
|
|
assert_no_select 'input[type=text][required]'
|
2015-05-03 13:02:56 -04:00
|
|
|
assert_no_select 'input[type=text][aria-required]'
|
2015-05-03 13:02:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-04 18:37:45 -05:00
|
|
|
end
|