mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
87 lines
3 KiB
Ruby
87 lines
3 KiB
Ruby
require 'test_helper'
|
|
|
|
class HintTest < ActionView::TestCase
|
|
|
|
def with_hint_for(object, attribute_name, type, options={}, &block)
|
|
simple_form_for object do |f|
|
|
f.attribute_name = attribute_name
|
|
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
|
f.input_type = type
|
|
f.options = options
|
|
|
|
hint = SimpleForm::Components::Hint.new(f, SimpleForm::FormBuilder::TERMINATOR)
|
|
concat(hint.call)
|
|
yield hint if block_given?
|
|
end
|
|
end
|
|
|
|
test 'hint should not be generated by default' do
|
|
with_hint_for @user, :name, :string do |hint|
|
|
assert hint.call.blank?
|
|
end
|
|
end
|
|
|
|
test 'hint should not be generated for hidden fields' do
|
|
with_hint_for @user, :name, :hidden, :hint => 'Use with care...' do |hint|
|
|
assert hint.call.blank?
|
|
end
|
|
end
|
|
|
|
test 'hint should be generated with input text' do
|
|
with_hint_for @user, :name, :string, :hint => 'Use with care...'
|
|
assert_select 'span.hint', 'Use with care...'
|
|
end
|
|
|
|
test 'hint uses the current component tag set' do
|
|
swap SimpleForm, :component_tag => :p do
|
|
with_hint_for @user, :name, :string, :hint => 'Use with care...'
|
|
assert_select 'p.hint', 'Use with care...'
|
|
end
|
|
end
|
|
|
|
test 'hint should use i18n based on model, action, and attribute to lookup translation' do
|
|
store_translations(:en, :simple_form => { :hints => { :user => {
|
|
:edit => { :name => 'Content of this input will be truncated...' }
|
|
} } }) do
|
|
with_hint_for @user, :name, :string
|
|
assert_select 'span.hint', 'Content of this input will be truncated...'
|
|
end
|
|
end
|
|
|
|
test 'hint should use i18n with model and attribute to lookup translation' do
|
|
store_translations(:en, :simple_form => { :hints => { :user => {
|
|
:name => 'Content of this input will be capitalized...'
|
|
} } }) do
|
|
with_hint_for @user, :name, :string
|
|
assert_select 'span.hint', 'Content of this input will be capitalized...'
|
|
end
|
|
end
|
|
|
|
test 'hint should use i18n just with attribute to lookup translation' do
|
|
store_translations(:en, :simple_form => { :hints => {
|
|
:name => 'Content of this input will be downcased...'
|
|
} }) do
|
|
with_hint_for @user, :name, :string
|
|
assert_select 'span.hint', 'Content of this input will be downcased...'
|
|
end
|
|
end
|
|
|
|
test 'hint should use i18n with lookup for association name' do
|
|
store_translations(:en, :simple_form => { :hints => {
|
|
:user => { :company => 'My company!' }
|
|
} } ) do
|
|
with_hint_for @user, :company_id, :string, :setup_association => true
|
|
assert_select 'span.hint', /My company!/
|
|
end
|
|
end
|
|
|
|
test 'hint should generate properly when object is not present' do
|
|
with_hint_for :project, :name, :string, :hint => 'Test without object'
|
|
assert_select 'span.hint', 'Test without object'
|
|
end
|
|
|
|
test 'hint should be able to pass html options' do
|
|
with_hint_for @user, :name, :string, :hint => 'Yay!', :hint_html => { :id => 'hint', :class => 'yay' }
|
|
assert_select 'span#hint.hint.yay'
|
|
end
|
|
end
|