2011-09-03 16:49:54 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class StringInputTest < ActionView::TestCase
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input maps text field to string attribute' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :name, :string
|
2014-09-08 21:14:53 +00:00
|
|
|
assert_select "input#user_name[type=text][name='user[name]'][value='New in SimpleForm!']"
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input generates a password field for password attributes' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :password, :password
|
|
|
|
assert_select "input#user_password.password[type=password][name='user[password]']"
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input gets maxlength from column definition for string attributes' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :name, :string
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[maxlength="100"]'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input does not get maxlength from column without size definition for string attributes' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :action, :string
|
|
|
|
assert_no_select 'input.string[maxlength]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input gets maxlength from column definition for password attributes' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :password, :password
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.password[type=password][maxlength="100"]'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input infers maxlength column definition from validation when present' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @validating_user, :name, :string
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[maxlength="25"]'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2016-08-22 02:36:45 +00:00
|
|
|
test 'input infers minlength column definition from validation when present' do
|
|
|
|
with_input_for @validating_user, :name, :string
|
|
|
|
assert_select 'input.string[minlength="5"]'
|
|
|
|
end
|
|
|
|
|
2016-07-01 17:55:15 +00:00
|
|
|
if ActionPack::VERSION::STRING < '5'
|
|
|
|
test 'input does not get maxlength from validation when tokenizer present' do
|
|
|
|
with_input_for @validating_user, :action, :string
|
|
|
|
assert_no_select 'input.string[maxlength]'
|
|
|
|
end
|
2016-08-22 02:36:45 +00:00
|
|
|
|
|
|
|
test 'input does not get minlength from validation when tokenizer present' do
|
|
|
|
with_input_for @validating_user, :action, :string
|
|
|
|
assert_no_select 'input.string[minlength]'
|
|
|
|
end
|
2012-01-02 20:48:41 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input gets maxlength from validation when :is option present' do
|
2012-01-03 18:10:50 +00:00
|
|
|
with_input_for @validating_user, :home_picture, :string
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[maxlength="12"]'
|
2012-01-03 18:10:50 +00:00
|
|
|
end
|
|
|
|
|
2016-08-22 02:36:45 +00:00
|
|
|
test 'input gets minlength from validation when :is option present' do
|
|
|
|
with_input_for @validating_user, :home_picture, :string
|
|
|
|
assert_select 'input.string[minlength="12"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input maxlength is the column limit plus one to make room for decimal point' do
|
2012-04-21 12:10:10 +00:00
|
|
|
with_input_for @user, :credit_limit, :string
|
|
|
|
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[maxlength="16"]'
|
2012-04-21 12:10:10 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input does not generate placeholder by default' do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :name, :string
|
|
|
|
assert_no_select 'input[placeholder]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input accepts the placeholder option' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @user, :name, :string, placeholder: 'Put in some text'
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[placeholder="Put in some text"]'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input generates a password field for password attributes that accept placeholder' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @user, :password, :password, placeholder: 'Password Confirmation'
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input[type=password].password[placeholder="Password Confirmation"]#user_password'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input does not infer pattern from attributes by default' do
|
2012-01-11 21:57:01 +00:00
|
|
|
with_input_for @other_validating_user, :country, :string
|
|
|
|
assert_no_select 'input[pattern="\w+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input infers pattern from attributes' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @other_validating_user, :country, :string, pattern: true
|
2011-12-04 12:40:00 +00:00
|
|
|
assert_select 'input[pattern="\w+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input infers pattern from attributes using proc' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @other_validating_user, :name, :string, pattern: true
|
2012-01-01 14:32:08 +00:00
|
|
|
assert_select 'input[pattern="\w+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input does not infer pattern from attributes if root default is false' do
|
2011-12-04 13:57:12 +00:00
|
|
|
swap_wrapper do
|
|
|
|
with_input_for @other_validating_user, :country, :string
|
2012-01-11 20:12:49 +00:00
|
|
|
assert_no_select 'input[pattern="\w+"]'
|
2011-12-04 13:57:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input uses given pattern from attributes' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @other_validating_user, :country, :string, input_html: { pattern: "\\d+" }
|
2011-12-04 12:40:00 +00:00
|
|
|
assert_select 'input[pattern="\d+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input does not use pattern if model has :without validation option' do
|
2013-02-28 06:57:57 +00:00
|
|
|
with_input_for @other_validating_user, :description, :string, pattern: true
|
|
|
|
assert_no_select 'input[pattern="\d+"]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input uses i18n to translate placeholder text' do
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: { placeholders: { user: {
|
|
|
|
name: 'Name goes here'
|
2011-09-03 16:49:54 +00:00
|
|
|
} } }) do
|
|
|
|
with_input_for @user, :name, :string
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[placeholder="Name goes here"]'
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'input uses custom i18n scope to translate placeholder text' do
|
2014-04-04 15:00:04 +00:00
|
|
|
store_translations(:en, my_scope: { placeholders: { user: {
|
|
|
|
name: 'Name goes here'
|
|
|
|
} } }) do
|
|
|
|
swap SimpleForm, i18n_scope: :my_scope do
|
|
|
|
with_input_for @user, :name, :string
|
2014-09-08 20:19:40 +00:00
|
|
|
assert_select 'input.string[placeholder="Name goes here"]'
|
2014-04-04 15:00:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-03 16:49:54 +00:00
|
|
|
[:email, :url, :search, :tel].each do |type|
|
2014-05-14 17:19:06 +00:00
|
|
|
test "input allows type #{type}" do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :name, type
|
|
|
|
assert_select "input.string.#{type}"
|
|
|
|
assert_select "input[type=#{type}]"
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test "input does not allow type #{type} if HTML5 compatibility is disabled" do
|
2011-12-04 15:15:27 +00:00
|
|
|
swap_wrapper do
|
2011-09-03 16:49:54 +00:00
|
|
|
with_input_for @user, :name, type
|
2011-12-04 15:15:27 +00:00
|
|
|
assert_select "input[type=text]"
|
2011-09-03 16:49:54 +00:00
|
|
|
assert_no_select "input[type=#{type}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-01-26 12:51:12 +00:00
|
|
|
|
|
|
|
test 'input strips extra spaces from class html attribute with default classes' do
|
|
|
|
with_input_for @user, :name, :string
|
|
|
|
assert_select "input[class='string required']"
|
|
|
|
assert_no_select "input[class='string required ']"
|
|
|
|
assert_no_select "input[class=' string required']"
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input strips extra spaces from class html attribute when giving a custom class' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_input_for @user, :name, :string, input_html: { class: "my_input" }
|
2012-01-26 12:51:12 +00:00
|
|
|
assert_select "input[class='string required my_input']"
|
|
|
|
assert_no_select "input[class='string required my_input ']"
|
|
|
|
assert_no_select "input[class=' string required my_input']"
|
|
|
|
end
|
2011-09-03 16:49:54 +00:00
|
|
|
end
|