2017-10-24 01:01:08 +00:00
|
|
|
# frozen_string_literal: true
|
2011-09-03 16:36:02 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
# Tests for f.input_field
|
|
|
|
class InputFieldTest < ActionView::TestCase
|
2015-03-25 11:58:59 +00:00
|
|
|
test "builder input_field only renders the input tag, nothing else" do
|
|
|
|
with_input_field_for @user, :name
|
|
|
|
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'form > input.required.string'
|
|
|
|
assert_no_select 'div.string'
|
|
|
|
assert_no_select 'label'
|
|
|
|
assert_no_select '.hint'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field allows overriding default input type' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, as: :text
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
assert_no_select 'input#user_name'
|
|
|
|
assert_select 'textarea#user_name.text'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field generates input type based on column type' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :age
|
2013-05-16 18:39:14 +00:00
|
|
|
|
|
|
|
assert_select 'input[type=number].integer#user_age'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field is able to disable any component' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :age, html5: false
|
2013-05-16 18:39:14 +00:00
|
|
|
|
|
|
|
assert_no_select 'input[html5=false]#user_age'
|
|
|
|
assert_select 'input[type=text].integer#user_age'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field allows passing options to input tag' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, id: 'name_input', class: 'name'
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
assert_select 'input.string.name#name_input'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field does not modify the options hash' do
|
2013-01-28 21:02:59 +00:00
|
|
|
options = { id: 'name_input', class: 'name' }
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, options
|
2012-02-21 03:42:06 +00:00
|
|
|
|
|
|
|
assert_select 'input.string.name#name_input'
|
2013-01-28 21:02:59 +00:00
|
|
|
assert_equal({ id: 'name_input', class: 'name' }, options)
|
2012-02-21 03:42:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field generates an input tag with a clean HTML' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, as: :integer, class: 'name'
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
assert_no_select 'input.integer[input_html]'
|
|
|
|
assert_no_select 'input.integer[as]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field uses i18n to translate placeholder text' do
|
2012-07-22 17:42:29 +00:00
|
|
|
store_translations(:en, simple_form: { placeholders: { user: {
|
|
|
|
name: 'Name goes here'
|
|
|
|
} } }) do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name
|
2012-07-22 17:42:29 +00:00
|
|
|
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[placeholder="Name goes here"]'
|
2012-07-22 17:42:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field uses min_max component' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @other_validating_user, :age, as: :integer
|
2012-07-22 17:42:29 +00:00
|
|
|
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input[min="18"]'
|
2012-07-22 17:42:29 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field does not use pattern component by default' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @other_validating_user, :country, as: :string
|
2012-07-22 17:42:29 +00:00
|
|
|
|
2013-11-24 15:11:05 +00:00
|
|
|
assert_no_select 'input[pattern="\w+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field infers pattern from attributes' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @other_validating_user, :country, as: :string, pattern: true
|
2013-11-24 15:11:05 +00:00
|
|
|
|
2018-08-04 00:09:43 +00:00
|
|
|
assert_select "input:match('pattern', ?)", /\w+/
|
2012-07-22 17:42:29 +00:00
|
|
|
end
|
|
|
|
|
2016-10-05 21:06:43 +00:00
|
|
|
test 'builder input_field accepts custom pattern' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @other_validating_user, :country, as: :string, pattern: '\d+'
|
2013-11-24 15:11:05 +00:00
|
|
|
|
2018-08-04 00:09:43 +00:00
|
|
|
assert_select "input:match('pattern', ?)", /\\d+/
|
2013-11-24 15:11:05 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field uses readonly component' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @other_validating_user, :age, as: :integer, readonly: true
|
2012-07-22 17:42:29 +00:00
|
|
|
|
|
|
|
assert_select 'input.integer.readonly[readonly]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder input_field uses maxlength component' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @validating_user, :name, as: :string
|
2012-07-22 17:42:29 +00:00
|
|
|
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[maxlength="25"]'
|
2012-07-22 17:42:29 +00:00
|
|
|
end
|
|
|
|
|
2016-08-22 02:36:45 +00:00
|
|
|
test 'builder input_field uses minlength component' do
|
|
|
|
with_input_field_for @validating_user, :name, as: :string
|
|
|
|
|
|
|
|
assert_select 'input.string[minlength="5"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder collection input_field generates input tag with a clean HTML' do
|
2018-01-07 17:55:19 +00:00
|
|
|
with_input_field_for @user, :status, collection: %w[Open Closed],
|
2015-03-25 11:58:59 +00:00
|
|
|
class: 'status', label_method: :to_s, value_method: :to_s
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
assert_no_select 'select.status[input_html]'
|
|
|
|
assert_no_select 'select.status[collection]'
|
|
|
|
assert_no_select 'select.status[label_method]'
|
|
|
|
assert_no_select 'select.status[value_method]'
|
|
|
|
end
|
2013-12-10 16:29:37 +00:00
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'build input_field does not treat "boolean_style" as a HTML attribute' do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :active, boolean_style: :nested
|
2013-12-10 16:29:37 +00:00
|
|
|
|
|
|
|
assert_no_select 'input.boolean[boolean_style]'
|
|
|
|
end
|
2014-11-19 21:20:17 +00:00
|
|
|
|
2016-08-09 14:33:29 +00:00
|
|
|
test 'build input_field does not treat "prompt" as a HTML attribute' do
|
|
|
|
with_input_field_for @user, :attempts, collection: [1,2,3,4,5], prompt: :translate
|
|
|
|
|
|
|
|
assert_no_select 'select[prompt]'
|
|
|
|
end
|
|
|
|
|
2014-11-19 21:20:17 +00:00
|
|
|
test 'build input_field without pattern component use the pattern string' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_html5_components do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, pattern: '\w+'
|
2014-11-19 21:20:17 +00:00
|
|
|
|
2018-08-04 00:09:43 +00:00
|
|
|
assert_select "input:match('pattern', ?)", /\w+/
|
2014-11-19 21:20:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'build input_field without placeholder component use the placeholder string' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_html5_components do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, placeholder: 'Placeholder'
|
2014-11-19 21:20:17 +00:00
|
|
|
|
|
|
|
assert_select 'input[placeholder="Placeholder"]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'build input_field without maxlength component use the maxlength string' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_html5_components do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, maxlength: 5
|
2014-11-19 21:20:17 +00:00
|
|
|
|
|
|
|
assert_select 'input[maxlength="5"]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 02:43:25 +00:00
|
|
|
test 'build input_field without minlength component use the minlength string' do
|
|
|
|
swap_wrapper :default, custom_wrapper_with_html5_components do
|
|
|
|
with_input_field_for @user, :name, minlength: 5
|
|
|
|
|
|
|
|
assert_select 'input[minlength="5"]'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-19 21:20:17 +00:00
|
|
|
test 'build input_field without readonly component use the readonly string' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_html5_components do
|
2015-03-25 11:58:59 +00:00
|
|
|
with_input_field_for @user, :name, readonly: true
|
2014-11-19 21:20:17 +00:00
|
|
|
|
|
|
|
assert_select 'input[readonly="readonly"]'
|
|
|
|
end
|
|
|
|
end
|
2018-04-03 02:07:08 +00:00
|
|
|
|
|
|
|
test 'adds valid class to input_field when it is configured' do
|
|
|
|
swap SimpleForm, input_field_valid_class: 'is-valid' do
|
|
|
|
@user.instance_eval { undef errors }
|
|
|
|
with_input_field_for @user, :name
|
|
|
|
|
|
|
|
assert_select 'input.string.required.is-valid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'adds error class to input_field when it is configured' do
|
|
|
|
swap SimpleForm, input_field_error_class: 'is-invalid' do
|
|
|
|
with_input_field_for @user, :name
|
|
|
|
|
|
|
|
assert_select 'input.string.required.is-invalid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'does not add validation classes to input_field when it is not configured' do
|
|
|
|
swap SimpleForm, input_field_error_class: nil, input_field_valid_class: nil do
|
|
|
|
with_input_field_for @user, :name
|
|
|
|
|
|
|
|
assert_select 'input.string.required'
|
|
|
|
end
|
|
|
|
end
|
2012-02-21 03:42:06 +00:00
|
|
|
end
|