2011-09-03 15:45:07 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class DiscoveryTest < ActionView::TestCase
|
|
|
|
# Setup new inputs and remove them after the test.
|
2014-03-11 22:12:08 +00:00
|
|
|
def discovery(value = false)
|
2013-01-28 21:02:59 +00:00
|
|
|
swap SimpleForm, cache_discovery: value do
|
2011-09-03 15:45:07 +00:00
|
|
|
begin
|
2011-09-03 16:49:54 +00:00
|
|
|
load "support/discovery_inputs.rb"
|
2011-09-03 15:45:07 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
SimpleForm::FormBuilder.discovery_cache.clear
|
|
|
|
Object.send :remove_const, :StringInput
|
|
|
|
Object.send :remove_const, :NumericInput
|
|
|
|
Object.send :remove_const, :CustomizedInput
|
2014-03-11 18:54:18 +00:00
|
|
|
Object.send :remove_const, :DeprecatedInput
|
2012-04-22 02:21:36 +00:00
|
|
|
Object.send :remove_const, :CollectionSelectInput
|
2014-06-05 10:46:12 +00:00
|
|
|
CustomInputs.send :remove_const, :PasswordInput
|
|
|
|
CustomInputs.send :remove_const, :NumericInput
|
2011-09-03 15:45:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'builder does not discover new inputs if cached' do
|
2011-09-03 15:45:07 +00:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'form input#user_name.string'
|
|
|
|
|
|
|
|
discovery(true) do
|
|
|
|
with_form_for @user, :name
|
|
|
|
assert_no_select 'form section input#user_name.string'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'builder discovers new inputs' do
|
2011-09-03 15:45:07 +00:00
|
|
|
discovery do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_form_for @user, :name, as: :customized
|
2011-09-03 15:45:07 +00:00
|
|
|
assert_select 'form section input#user_name.string'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'builder does not discover new inputs if discovery is off' do
|
2011-09-03 15:45:07 +00:00
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'form input#user_name.string'
|
|
|
|
|
2013-01-28 21:02:59 +00:00
|
|
|
swap SimpleForm, inputs_discovery: false do
|
2011-09-03 15:45:07 +00:00
|
|
|
discovery do
|
|
|
|
with_form_for @user, :name
|
|
|
|
assert_no_select 'form section input#user_name.string'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'builder discovers new inputs from mappings if not cached' do
|
2011-09-03 15:45:07 +00:00
|
|
|
discovery do
|
|
|
|
with_form_for @user, :name
|
|
|
|
assert_select 'form section input#user_name.string'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:19:06 +00:00
|
|
|
test 'builder discovers new inputs from internal fallbacks if not cached' do
|
2011-09-03 15:45:07 +00:00
|
|
|
discovery do
|
|
|
|
with_form_for @user, :age
|
|
|
|
assert_select 'form section input#user_age.numeric.integer'
|
|
|
|
end
|
|
|
|
end
|
2012-04-22 02:21:36 +00:00
|
|
|
|
2014-06-26 20:52:23 +00:00
|
|
|
test 'builder discovers new maped inputs from configured namespaces if not cached' do
|
2014-06-05 10:46:12 +00:00
|
|
|
discovery do
|
|
|
|
swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
|
2014-06-26 20:52:23 +00:00
|
|
|
with_form_for @user, :password
|
2014-06-05 10:46:12 +00:00
|
|
|
assert_select 'form input#user_password.password-custom-input'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-26 20:52:23 +00:00
|
|
|
test 'builder discovers new maped inputs from configured namespaces before the ones from top level namespace' do
|
2014-06-05 10:46:12 +00:00
|
|
|
discovery do
|
|
|
|
swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
|
|
|
|
with_form_for @user, :age
|
2014-06-26 18:09:53 +00:00
|
|
|
assert_select 'form input#user_age.numeric-custom-input'
|
2014-06-05 10:46:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-26 20:52:23 +00:00
|
|
|
test 'builder discovers new custom inputs from configured namespace before the ones from top level namespace' do
|
|
|
|
discovery do
|
|
|
|
swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
|
|
|
|
with_form_for @user, :name, as: 'customized'
|
|
|
|
assert_select 'form input#user_name.customized-namespace-custom-input'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-26 20:53:07 +00:00
|
|
|
test 'raises error when configured namespace does not exists' do
|
|
|
|
discovery do
|
|
|
|
swap SimpleForm, custom_inputs_namespaces: ['InvalidNamespace'] do
|
|
|
|
assert_raise NameError do
|
|
|
|
with_form_for @user, :age
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-22 02:21:36 +00:00
|
|
|
test 'new inputs can override the input_html_options' do
|
|
|
|
discovery do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_form_for @user, :active, as: :select
|
2012-04-22 02:21:36 +00:00
|
|
|
assert_select 'form select#user_active.select.chosen'
|
|
|
|
end
|
|
|
|
end
|
2014-03-11 18:54:18 +00:00
|
|
|
|
2014-03-11 22:41:25 +00:00
|
|
|
test 'inputs method without wrapper_options are deprecated' do
|
2014-03-11 18:54:18 +00:00
|
|
|
discovery do
|
|
|
|
assert_deprecated do
|
|
|
|
with_form_for @user, :name, as: :deprecated
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_select 'form section input#user_name.string'
|
|
|
|
end
|
|
|
|
end
|
2012-04-22 02:21:36 +00:00
|
|
|
end
|