2010-06-17 18:12:36 -04:00
|
|
|
# encoding: UTF-8
|
2009-12-09 17:36:21 -05:00
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class LabelTest < ActionView::TestCase
|
|
|
|
|
|
|
|
setup do
|
2010-01-09 10:05:02 -05:00
|
|
|
SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html
|
2009-12-09 17:36:21 -05:00
|
|
|
end
|
|
|
|
|
2009-12-13 16:16:00 -05:00
|
|
|
def with_label_for(object, attribute_name, type, options={})
|
2010-09-26 18:24:30 -04:00
|
|
|
with_concat_form_for(object) do |f|
|
2009-12-13 16:16:00 -05:00
|
|
|
f.attribute_name = attribute_name
|
|
|
|
f.reflection = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
|
|
|
f.input_type = type
|
|
|
|
f.options = options
|
2009-12-10 08:01:44 -05:00
|
|
|
|
2010-09-26 18:16:25 -04:00
|
|
|
SimpleForm::Inputs::Base.new(f).label
|
2010-09-26 18:24:30 -04:00
|
|
|
end
|
2009-12-09 17:36:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should generate a default humanized description' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_name]', /Name/
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should allow a customized description' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string, :label => 'My label!'
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_name]', /My label!/
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use human attribute name from object when available' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :description, :text
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_description]', /User Description!/
|
|
|
|
end
|
|
|
|
|
2009-12-10 23:00:37 -05:00
|
|
|
test 'label should use human attribute name based on association name' do
|
2009-12-13 15:42:27 -05:00
|
|
|
with_label_for @user, :company_id, :string, :setup_association => true
|
2009-12-10 23:00:37 -05:00
|
|
|
assert_select 'label', /Company Human Name!/
|
|
|
|
end
|
|
|
|
|
2009-12-10 10:14:24 -05:00
|
|
|
test 'label should use i18n based on model, action, and attribute to lookup translation' do
|
2009-12-11 15:32:57 -05:00
|
|
|
@controller.action_name = "new"
|
|
|
|
store_translations(:en, :simple_form => { :labels => { :user => {
|
|
|
|
:new => { :description => 'Nova descrição' }
|
|
|
|
} } } ) do
|
|
|
|
with_label_for @user, :description, :text
|
|
|
|
assert_select 'label[for=user_description]', /Nova descrição/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should fallback to new when action is create' do
|
|
|
|
@controller.action_name = "create"
|
2009-12-10 10:14:24 -05:00
|
|
|
store_translations(:en, :simple_form => { :labels => { :user => {
|
|
|
|
:new => { :description => 'Nova descrição' }
|
|
|
|
} } } ) do
|
|
|
|
with_label_for @user, :description, :text
|
|
|
|
assert_select 'label[for=user_description]', /Nova descrição/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-09 17:36:21 -05:00
|
|
|
test 'label should use i18n based on model and attribute to lookup translation' do
|
|
|
|
store_translations(:en, :simple_form => { :labels => { :user => {
|
|
|
|
:description => 'Descrição'
|
|
|
|
} } } ) do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :description, :text
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_description]', /Descrição/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input should use i18n based only on attribute to lookup translation' do
|
|
|
|
store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :age, :integer
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_age]', /Idade/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-10 23:00:37 -05:00
|
|
|
test 'label should use i18n with lookup for association name' do
|
|
|
|
store_translations(:en, :simple_form => { :labels => {
|
|
|
|
:user => { :company => 'My company!' }
|
|
|
|
} } ) do
|
2009-12-13 15:42:27 -05:00
|
|
|
with_label_for @user, :company_id, :string, :setup_association => true
|
2009-12-10 23:00:37 -05:00
|
|
|
assert_select 'label[for=user_company_id]', /My company!/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-09 17:36:21 -05:00
|
|
|
test 'label should have css class from type' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.string'
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :description, :text
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.text'
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :age, :integer
|
|
|
|
assert_select 'label.integer'
|
|
|
|
with_label_for @user, :born_at, :date
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.date'
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :created_at, :datetime
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.datetime'
|
|
|
|
end
|
2010-06-17 18:12:36 -04:00
|
|
|
|
2010-05-14 17:58:36 -04:00
|
|
|
test 'label should obtain required from ActiveModel::Validations when it is included' do
|
|
|
|
with_label_for @validating_user, :name, :string
|
|
|
|
assert_select 'label.required'
|
|
|
|
with_label_for @validating_user, :status, :string
|
|
|
|
assert_select 'label.optional'
|
|
|
|
end
|
2010-06-17 18:12:36 -04:00
|
|
|
|
2010-05-14 17:58:36 -04:00
|
|
|
test 'label should allow overriding required when ActiveModel::Validations is included' do
|
|
|
|
with_label_for @validating_user, :name, :string, :required => false
|
|
|
|
assert_select 'label.optional'
|
|
|
|
with_label_for @validating_user, :status, :string, :required => true
|
|
|
|
assert_select 'label.required'
|
|
|
|
end
|
2009-12-09 17:36:21 -05:00
|
|
|
|
2010-05-14 17:58:36 -04:00
|
|
|
test 'label should be required by default when ActiveModel::Validations is not included' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.required'
|
|
|
|
end
|
|
|
|
|
2010-05-14 17:58:36 -04:00
|
|
|
test 'label should be able to disable required when ActiveModel::Validations is not included' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string, :required => false
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_no_select 'label.required'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should add required text when required' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label.required abbr[title=required]', '*'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should not have required text in no required inputs' do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string, :required => false
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_no_select 'form label abbr'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use i18n to find required text' do
|
|
|
|
store_translations(:en, :simple_form => { :required => { :text => 'campo requerido' }}) do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'form label abbr[title=campo requerido]', '*'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use i18n to find required mark' do
|
|
|
|
store_translations(:en, :simple_form => { :required => { :mark => '*-*' }}) do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'form label abbr', '*-*'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use i18n to find required string tag' do
|
2009-12-10 15:09:26 -05:00
|
|
|
store_translations(:en, :simple_form => { :required => { :html => '<span class="required" title="requerido">*</span>' }}) do
|
2009-12-10 06:31:40 -05:00
|
|
|
with_label_for @user, :name, :string
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_no_select 'form label abbr'
|
|
|
|
assert_select 'form label span.required[title=requerido]', '*'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should allow overwriting input id' do
|
2009-12-10 09:11:29 -05:00
|
|
|
with_label_for @user, :name, :string, :input_html => { :id => 'my_new_id' }
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=my_new_id]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use default input id when it was not overridden' do
|
2009-12-10 09:11:29 -05:00
|
|
|
with_label_for @user, :name, :string, :input_html => { :class => 'my_new_id' }
|
2009-12-09 17:36:21 -05:00
|
|
|
assert_select 'label[for=user_name]'
|
|
|
|
end
|
2009-12-10 06:31:40 -05:00
|
|
|
|
|
|
|
test 'label should be generated properly when object is not present' do
|
|
|
|
with_label_for :project, :name, :string
|
|
|
|
assert_select 'label[for=project_name]', /Name/
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should use i18n properly when object is not present' do
|
|
|
|
store_translations(:en, :simple_form => { :labels => {
|
|
|
|
:project => { :name => 'Nome' }
|
|
|
|
} } ) do
|
|
|
|
with_label_for :project, :name, :string
|
|
|
|
assert_select 'label[for=project_name]', /Nome/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'label should add required by default when object is not present' do
|
|
|
|
with_label_for :project, :name, :string
|
|
|
|
assert_select 'label.required[for=project_name]'
|
|
|
|
with_label_for :project, :description, :string, :required => false
|
|
|
|
assert_no_select 'label.required[for=project_description]'
|
|
|
|
end
|
2009-12-09 17:36:21 -05:00
|
|
|
end
|