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