2011-09-03 16:36:02 +00:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
# Tests for f.hint
|
|
|
|
class HintTest < ActionView::TestCase
|
|
|
|
def with_hint_for(object, *args)
|
|
|
|
with_concat_form_for(object) do |f|
|
|
|
|
f.hint(*args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint should not be generated by default' do
|
|
|
|
with_hint_for @user, :name
|
|
|
|
assert_no_select 'span.hint'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint should be generated with optional text' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :name, hint: 'Use with care...'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'span.hint', 'Use with care...'
|
|
|
|
end
|
|
|
|
|
2012-02-21 03:42:06 +00:00
|
|
|
test 'hint should not modify the options hash' do
|
2013-01-28 21:02:59 +00:00
|
|
|
options = { hint: 'Use with care...' }
|
2012-02-21 03:42:06 +00:00
|
|
|
with_hint_for @user, :name, options
|
|
|
|
assert_select 'span.hint', 'Use with care...'
|
2013-01-28 21:02:59 +00:00
|
|
|
assert_equal({ hint: 'Use with care...' }, options)
|
2012-02-21 03:42:06 +00:00
|
|
|
end
|
|
|
|
|
2011-09-03 16:36:02 +00:00
|
|
|
test 'hint should be generated cleanly with optional text' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :span
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_no_select 'span.hint[hint]'
|
2012-02-22 10:34:02 +00:00
|
|
|
assert_no_select 'span.hint[hint_tag]'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_no_select 'span.hint[hint_html]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint uses the current component tag set' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :p
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'p.hint', 'Use with care...'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint should be able to pass html options' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :name, hint: 'Yay!', id: 'hint', class: 'yay'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'span#hint.hint.yay'
|
|
|
|
end
|
2012-06-27 01:58:18 +00:00
|
|
|
|
2012-03-07 13:44:56 +00:00
|
|
|
test 'hint should be output as html_safe' do
|
2013-11-27 01:33:24 +00:00
|
|
|
with_hint_for @user, :name, hint: '<b>Bold</b> and not...'.html_safe
|
2012-06-27 01:58:18 +00:00
|
|
|
assert_select 'span.hint', 'Bold and not...'
|
2013-11-27 01:33:24 +00:00
|
|
|
assert_select 'span.hint b', 'Bold'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'builder should escape hint text' do
|
|
|
|
with_hint_for @user, :name, hint: '<script>alert(1337)</script>'
|
|
|
|
assert_select 'span.hint', "<script>alert(1337)</script>"
|
2012-03-07 13:44:56 +00:00
|
|
|
end
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
# Without attribute name
|
|
|
|
|
|
|
|
test 'hint without attribute name' do
|
|
|
|
with_hint_for @validating_user, 'Hello World!'
|
|
|
|
assert_select 'span.hint', 'Hello World!'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint without attribute name should generate component tag with a clean HTML' do
|
|
|
|
with_hint_for @validating_user, 'Hello World!'
|
|
|
|
assert_no_select 'span.hint[hint]'
|
|
|
|
assert_no_select 'span.hint[hint_html]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint without attribute name uses the current component tag set' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, 'Hello World!', hint_tag: :p
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_no_select 'p.hint[hint]'
|
|
|
|
assert_no_select 'p.hint[hint_html]'
|
|
|
|
assert_no_select 'p.hint[hint_tag]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint without attribute name should be able to pass html options' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, 'Yay', id: 'hint', class: 'yay'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'span#hint.hint.yay', 'Yay'
|
|
|
|
end
|
|
|
|
|
|
|
|
# I18n
|
|
|
|
|
|
|
|
test 'hint should use i18n based on model, action, and attribute to lookup translation' do
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: { hints: { user: {
|
|
|
|
edit: { name: 'Content of this input will be truncated...' }
|
2011-09-03 16:36:02 +00:00
|
|
|
} } }) do
|
|
|
|
with_hint_for @user, :name
|
|
|
|
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
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: { hints: { user: {
|
|
|
|
name: 'Content of this input will be capitalized...'
|
2011-09-03 16:36:02 +00:00
|
|
|
} } }) do
|
|
|
|
with_hint_for @user, :name
|
|
|
|
assert_select 'span.hint', 'Content of this input will be capitalized...'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-10 21:50:03 +00:00
|
|
|
test 'hint should use i18n under defaults namespace to lookup translation' do
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: {
|
2013-04-13 22:50:14 +00:00
|
|
|
hints: { defaults: { name: 'Content of this input will be downcased...' } }
|
2011-12-10 21:50:03 +00:00
|
|
|
}) do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_hint_for @user, :name
|
|
|
|
assert_select 'span.hint', 'Content of this input will be downcased...'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'hint should use i18n with lookup for association name' do
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: { hints: {
|
|
|
|
user: { company: 'My company!' }
|
2011-09-03 16:36:02 +00:00
|
|
|
} } ) do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :company_id, as: :string, reflection: Association.new(Company, :company, {})
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'span.hint', /My company!/
|
|
|
|
end
|
|
|
|
end
|
2012-06-27 01:58:18 +00:00
|
|
|
|
2012-03-07 13:44:56 +00:00
|
|
|
test 'hint should output translations as html_safe' do
|
2013-01-28 21:02:59 +00:00
|
|
|
store_translations(:en, simple_form: { hints: { user: {
|
|
|
|
edit: { name: '<b>This is bold</b> and this is not...' }
|
2012-03-07 13:44:56 +00:00
|
|
|
} } }) do
|
|
|
|
with_hint_for @user, :name
|
|
|
|
assert_select 'span.hint', 'This is bold and this is not...'
|
|
|
|
end
|
|
|
|
end
|
2012-06-27 01:58:18 +00:00
|
|
|
|
2011-09-03 16:36:02 +00:00
|
|
|
|
|
|
|
# No object
|
|
|
|
|
|
|
|
test 'hint should generate properly when object is not present' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for :project, :name, hint: 'Test without object'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'span.hint', 'Test without object'
|
|
|
|
end
|
2011-09-03 17:04:40 +00:00
|
|
|
|
|
|
|
# Custom wrappers
|
|
|
|
|
|
|
|
test 'hint with custom wrappers works' do
|
2011-09-04 09:31:24 +00:00
|
|
|
swap_wrapper do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_hint_for @user, :name, hint: "can't be blank"
|
2013-11-27 01:33:24 +00:00
|
|
|
assert_select 'div.omg_hint', "can't be blank"
|
2011-09-03 17:04:40 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-21 22:54:45 +00:00
|
|
|
end
|