2017-10-24 01:01:08 +00:00
|
|
|
# frozen_string_literal: true
|
2011-09-03 16:36:02 +00:00
|
|
|
# encoding: UTF-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class LabelTest < ActionView::TestCase
|
2011-09-15 16:02:02 +00:00
|
|
|
def with_label_for(object, *args, &block)
|
2011-09-03 16:36:02 +00:00
|
|
|
with_concat_form_for(object) do |f|
|
2011-09-15 16:02:02 +00:00
|
|
|
f.label(*args, &block)
|
2011-09-03 16:36:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder generates a label for the attribute' do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_label_for @user, :name
|
|
|
|
assert_select 'label.string[for=user_name]', /Name/
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder generates a label for the boolean attrbiute' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_label_for @user, :name, as: :boolean
|
2012-02-25 11:47:56 +00:00
|
|
|
assert_select 'label.boolean[for=user_name]', /Name/
|
|
|
|
assert_no_select 'label[as=boolean]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder generates a label component tag with a clean HTML' do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_label_for @user, :name
|
|
|
|
assert_no_select 'label.string[label_html]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder adds a required class to label if the attribute is required' do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_label_for @validating_user, :name
|
|
|
|
assert_select 'label.string.required[for=validating_user_name]', /Name/
|
|
|
|
end
|
|
|
|
|
2015-02-16 17:49:15 +00:00
|
|
|
test 'builder adds a disabled class to label if the attribute is disabled' do
|
|
|
|
with_label_for @validating_user, :name, disabled: true
|
|
|
|
assert_select 'label.string.disabled[for=validating_user_name]', /Name/
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'builder does not add a disabled class to label if the attribute is not disabled' do
|
|
|
|
with_label_for @validating_user, :name, disabled: false
|
|
|
|
assert_no_select 'label.string.disabled[for=validating_user_name]', /Name/
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder escapes label text' do
|
2013-11-26 23:07:52 +00:00
|
|
|
with_label_for @user, :name, label: '<script>alert(1337)</script>', required: false
|
2014-11-17 19:27:13 +00:00
|
|
|
assert_no_select 'label.string script'
|
2013-11-26 23:07:52 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder does not escape label text if it is safe' do
|
2013-11-26 23:07:52 +00:00
|
|
|
with_label_for @user, :name, label: '<script>alert(1337)</script>'.html_safe, required: false
|
|
|
|
assert_select 'label.string script', "alert(1337)"
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder allows passing options to label tag' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_label_for @user, :name, label: 'My label', id: 'name_label'
|
2011-09-03 16:36:02 +00:00
|
|
|
assert_select 'label.string#name_label', /My label/
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder label generates label tag with clean HTML' do
|
2013-01-28 21:02:59 +00:00
|
|
|
with_label_for @user, :name, label: 'My label', required: true, id: 'name_label'
|
2012-02-22 10:34:02 +00:00
|
|
|
assert_select 'label.string#name_label', /My label/
|
|
|
|
assert_no_select 'label[label]'
|
|
|
|
assert_no_select 'label[required]'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder does not modify the options hash' do
|
2013-01-28 21:02:59 +00:00
|
|
|
options = { label: 'My label', id: 'name_label' }
|
2012-02-21 03:42:06 +00:00
|
|
|
with_label_for @user, :name, options
|
|
|
|
assert_select 'label.string#name_label', /My label/
|
2013-01-28 21:02:59 +00:00
|
|
|
assert_equal({ label: 'My label', id: 'name_label' }, options)
|
2012-02-21 03:42:06 +00:00
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder fallbacks to default label when string is given' do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_label_for @user, :name, 'Nome do usuário'
|
|
|
|
assert_select 'label', 'Nome do usuário'
|
|
|
|
assert_no_select 'label.string'
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder fallbacks to default label when block is given' do
|
2011-09-15 16:02:02 +00:00
|
|
|
with_label_for @user, :name do
|
|
|
|
'Nome do usuário'
|
|
|
|
end
|
|
|
|
assert_select 'label', 'Nome do usuário'
|
|
|
|
assert_no_select 'label.string'
|
|
|
|
end
|
|
|
|
|
2011-09-03 16:36:02 +00:00
|
|
|
test 'builder allows label order to be changed' do
|
2013-04-17 15:35:55 +00:00
|
|
|
swap SimpleForm, label_text: proc { |l, r| "#{l}:" } do
|
2011-09-03 16:36:02 +00:00
|
|
|
with_label_for @user, :age
|
|
|
|
assert_select 'label.integer[for=user_age]', "Age:"
|
|
|
|
end
|
|
|
|
end
|
2012-09-11 12:45:26 +00:00
|
|
|
|
|
|
|
test 'configuration allow set label text for wrappers' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_label_text do
|
2012-09-11 12:45:26 +00:00
|
|
|
with_concat_form_for(@user) do |f|
|
|
|
|
concat f.input :age
|
|
|
|
end
|
|
|
|
assert_select "label.integer[for=user_age]", "**Age**"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-19 08:11:27 +00:00
|
|
|
test 'configuration allow set rewrited label tag for wrappers' do
|
2015-03-25 11:17:42 +00:00
|
|
|
swap_wrapper :default, custom_wrapper_with_custom_label_component do
|
2014-06-19 08:11:27 +00:00
|
|
|
with_concat_form_for(@user) do |f|
|
|
|
|
concat f.input :age
|
|
|
|
end
|
|
|
|
assert_select "span.integer.user_age", /Age/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder allows custom formatting when label is explicitly specified' do
|
2014-04-02 23:09:22 +00:00
|
|
|
swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
|
2013-04-17 13:56:25 +00:00
|
|
|
with_label_for @user, :time_zone, 'What is your home time zone?'
|
|
|
|
assert_select 'label[for=user_time_zone]', 'What is your home time zone?'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-14 17:18:43 +00:00
|
|
|
test 'builder allows custom formatting when label is generated' do
|
2014-04-02 23:09:22 +00:00
|
|
|
swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
|
2013-04-17 13:56:25 +00:00
|
|
|
with_label_for @user, :time_zone
|
|
|
|
assert_select 'label[for=user_time_zone]', 'Time Zone:'
|
|
|
|
end
|
|
|
|
end
|
2016-07-13 02:19:46 +00:00
|
|
|
|
|
|
|
test 'builder allows label specific `label_text` option' do
|
|
|
|
with_label_for @user, :time_zone, label_text: lambda { |l, _, _| "#{l.titleize}:" }
|
|
|
|
|
|
|
|
assert_no_select 'label[label_text]'
|
|
|
|
assert_select 'label[for=user_time_zone]', 'Time Zone:'
|
|
|
|
end
|
2011-09-03 16:36:02 +00:00
|
|
|
end
|