1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/test/inputs/collection_radio_buttons_input_test.rb

329 lines
13 KiB
Ruby
Raw Normal View History

# encoding: UTF-8
require 'test_helper'
class CollectionRadioButtonsInputTest < ActionView::TestCase
setup do
SimpleForm::Inputs::CollectionRadioButtonsInput.reset_i18n_cache :boolean_collection
end
test 'input should generate boolean radio buttons by default for radio types' do
with_input_for @user, :active, :radio_buttons
assert_select 'input[type=radio][value=true].radio_buttons#user_active_true'
assert_select 'input[type=radio][value=false].radio_buttons#user_active_false'
end
test 'input as radio should generate internal labels by default' do
with_input_for @user, :active, :radio_buttons
assert_select 'label[for=user_active_true]', 'Yes'
assert_select 'label[for=user_active_false]', 'No'
end
test 'input as radio should use i18n to translate internal labels' do
2013-01-28 16:02:59 -05:00
store_translations(:en, simple_form: { yes: 'Sim', no: 'Não' }) do
with_input_for @user, :active, :radio_buttons
assert_select 'label[for=user_active_true]', 'Sim'
assert_select 'label[for=user_active_false]', 'Não'
end
end
test 'input radio should not include for attribute by default' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]
assert_select 'label'
assert_no_select 'label[for=user_gender]'
end
test 'input radio should include for attribute when giving as html option' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female], label_html: { for: 'gender' }
assert_select 'label[for=gender]'
end
test 'input should mark the checked value when using boolean and radios' do
@user.active = false
with_input_for @user, :active, :radio_buttons
assert_no_select 'input[type=radio][value=true][checked]'
assert_select 'input[type=radio][value=false][checked]'
end
test 'input should allow overriding collection for radio types' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'
assert_select 'input[type=radio][value=Carlos]'
assert_select 'label.collection_radio_buttons[for=user_name_jose]', 'Jose'
assert_select 'label.collection_radio_buttons[for=user_name_carlos]', 'Carlos'
end
test 'input should do automatic collection translation for radio types using defaults key' do
2013-01-28 16:02:59 -05:00
store_translations(:en, simple_form: { options: { defaults: {
gender: { male: 'Male', female: 'Female'}
} } } ) do
2013-01-28 16:02:59 -05:00
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]
assert_select 'input[type=radio][value=male]'
assert_select 'input[type=radio][value=female]'
assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
end
end
test 'input should do automatic collection translation for radio types using specific object key' do
2013-01-28 16:02:59 -05:00
store_translations(:en, simple_form: { options: { user: {
gender: { male: 'Male', female: 'Female'}
} } } ) do
2013-01-28 16:02:59 -05:00
with_input_for @user, :gender, :radio_buttons, collection: [:male, :female]
assert_select 'input[type=radio][value=male]'
assert_select 'input[type=radio][value=female]'
assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
end
end
test 'input should mark the current radio value by default' do
@user.name = "Carlos"
2013-01-28 16:02:59 -05:00
with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Carlos][checked=checked]'
end
test 'input should accept html options as the last element of collection' do
with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose', class: 'foo']]
assert_select 'input.foo[type=radio][value=jose]'
end
test 'input should allow using a collection with text/value arrays' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose'], ['Carlos', 'carlos']]
assert_select 'input[type=radio][value=jose]'
assert_select 'input[type=radio][value=carlos]'
assert_select 'label.collection_radio_buttons', 'Jose'
assert_select 'label.collection_radio_buttons', 'Carlos'
end
test 'input should allow using a collection with a Proc' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :name, :radio_buttons, collection: Proc.new { ['Jose', 'Carlos' ] }
assert_select 'label.collection_radio_buttons', 'Jose'
assert_select 'label.collection_radio_buttons', 'Carlos'
end
test 'input should allow overriding only label method for collections' do
with_input_for @user, :name, :radio_buttons,
2013-04-13 18:50:14 -04:00
collection: ['Jose', 'Carlos'],
2013-01-28 16:02:59 -05:00
label_method: :upcase
assert_select 'label.collection_radio_buttons', 'JOSE'
assert_select 'label.collection_radio_buttons', 'CARLOS'
end
test 'input should allow overriding only value method for collections' do
with_input_for @user, :name, :radio_buttons,
2013-04-13 18:50:14 -04:00
collection: ['Jose', 'Carlos'],
2013-01-28 16:02:59 -05:00
value_method: :upcase
assert_select 'input[type=radio][value=JOSE]'
assert_select 'input[type=radio][value=CARLOS]'
end
test 'input should allow overriding label and value method for collections' do
with_input_for @user, :name, :radio_buttons,
2013-04-13 18:50:14 -04:00
collection: ['Jose', 'Carlos'],
2013-01-28 16:02:59 -05:00
label_method: :upcase,
value_method: :downcase
assert_select 'input[type=radio][value=jose]'
assert_select 'input[type=radio][value=carlos]'
assert_select 'label.collection_radio_buttons', 'JOSE'
assert_select 'label.collection_radio_buttons', 'CARLOS'
end
test 'input should allow overriding label and value method using a lambda for collections' do
with_input_for @user, :name, :radio_buttons,
2013-04-13 18:50:14 -04:00
collection: ['Jose', 'Carlos'],
2013-01-28 16:02:59 -05:00
label_method: lambda { |i| i.upcase },
value_method: lambda { |i| i.downcase }
assert_select 'input[type=radio][value=jose]'
assert_select 'input[type=radio][value=carlos]'
assert_select 'label.collection_radio_buttons', 'JOSE'
assert_select 'label.collection_radio_buttons', 'CARLOS'
end
test 'collection input with radio type should generate required html attribute' do
2013-04-13 18:50:14 -04:00
with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']
assert_select 'input[type=radio].required'
assert_select 'input[type=radio][required]'
end
test 'collection input with radio type should generate aria-required html attribute' do
2013-04-13 18:50:14 -04:00
with_input_for @user, :name, :radio_buttons, collection: ['Jose', 'Carlos']
assert_select 'input[type=radio].required'
assert_select 'input[type=radio][aria-required=true]'
end
test 'input radio does not wrap the collection by default' do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form input[type=radio]', count: 2
assert_no_select 'form ul'
end
test 'input radio wraps the collection in the configured collection wrapper tag' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form ul input[type=radio]', count: 2
end
end
test 'input radio does not wrap the collection when configured with falsy values' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: false do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form input[type=radio]', count: 2
assert_no_select 'form ul'
end
end
test 'input radio allows overriding the collection wrapper tag at input level' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul do
with_input_for @user, :active, :radio_buttons, collection_wrapper_tag: :section
2013-01-28 16:02:59 -05:00
assert_select 'form section input[type=radio]', count: 2
assert_no_select 'form ul'
end
end
test 'input radio allows disabling the collection wrapper tag at input level' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul do
with_input_for @user, :active, :radio_buttons, collection_wrapper_tag: false
2013-01-28 16:02:59 -05:00
assert_select 'form input[type=radio]', count: 2
assert_no_select 'form ul'
end
end
test 'input radio renders the wrapper tag with the configured wrapper class' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul, collection_wrapper_class: 'inputs-list' do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form ul.inputs-list input[type=radio]', count: 2
end
end
test 'input radio allows giving wrapper class at input level only' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul do
with_input_for @user, :active, :radio_buttons, collection_wrapper_class: 'items-list'
2013-01-28 16:02:59 -05:00
assert_select 'form ul.items-list input[type=radio]', count: 2
end
end
test 'input radio uses both configured and given wrapper classes for wrapper tag' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, collection_wrapper_tag: :ul, collection_wrapper_class: 'inputs-list' do
with_input_for @user, :active, :radio_buttons, collection_wrapper_class: 'items-list'
2013-01-28 16:02:59 -05:00
assert_select 'form ul.inputs-list.items-list input[type=radio]', count: 2
end
end
test 'input radio wraps each item in the configured item wrapper tag' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :li do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form li input[type=radio]', count: 2
end
end
test 'input radio does not wrap items when configured with falsy values' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: false do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form input[type=radio]', count: 2
assert_no_select 'form li'
end
end
test 'input radio allows overriding the item wrapper tag at input level' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :li do
with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :dl
2013-01-28 16:02:59 -05:00
assert_select 'form dl input[type=radio]', count: 2
assert_no_select 'form li'
end
end
test 'input radio allows disabling the item wrapper tag at input level' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :ul do
with_input_for @user, :active, :radio_buttons, item_wrapper_tag: false
2013-01-28 16:02:59 -05:00
assert_select 'form input[type=radio]', count: 2
assert_no_select 'form li'
end
end
test 'input radio wraps items in a span tag by default' do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form span input[type=radio]', count: 2
end
test 'input radio renders the item wrapper tag with a default class "radio"' do
2013-01-28 16:02:59 -05:00
with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :li
2013-01-28 16:02:59 -05:00
assert_select 'form li.radio input[type=radio]', count: 2
end
test 'input radio renders the item wrapper tag with the configured item wrapper class' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :li, item_wrapper_class: 'item' do
with_input_for @user, :active, :radio_buttons
2013-01-28 16:02:59 -05:00
assert_select 'form li.radio.item input[type=radio]', count: 2
end
end
test 'input radio allows giving item wrapper class at input level only' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :li do
with_input_for @user, :active, :radio_buttons, item_wrapper_class: 'item'
2013-01-28 16:02:59 -05:00
assert_select 'form li.radio.item input[type=radio]', count: 2
end
end
test 'input radio uses both configured and given item wrapper classes for item wrapper tag' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, item_wrapper_tag: :li, item_wrapper_class: 'item' do
with_input_for @user, :active, :radio_buttons, item_wrapper_class: 'inline'
2013-01-28 16:02:59 -05:00
assert_select 'form li.radio.item.inline input[type=radio]', count: 2
end
end
test 'input radio respects the nested boolean style config, generating nested label > input' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :radio_buttons
assert_select 'label.radio > input#user_active_true[type=radio]'
assert_select 'label.radio', 'Yes'
assert_select 'label.radio > input#user_active_false[type=radio]'
assert_select 'label.radio', 'No'
assert_no_select 'label.collection_radio_buttons'
end
end
test 'input radio with nested style overrides configured item wrapper tag, forcing the :label' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, boolean_style: :nested, item_wrapper_tag: :li do
with_input_for @user, :active, :radio_buttons
assert_select 'label.radio > input'
assert_no_select 'li'
end
end
test 'input radio with nested style overrides given item wrapper tag, forcing the :label' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :li
assert_select 'label.radio > input'
assert_no_select 'li'
end
end
test 'input radio with nested style accepts giving extra wrapper classes' do
2013-01-28 16:02:59 -05:00
swap SimpleForm, boolean_style: :nested do
with_input_for @user, :active, :radio_buttons, item_wrapper_class: "inline"
assert_select 'label.radio.inline > input'
end
end
end