2011-09-03 12:49:54 -04:00
|
|
|
# encoding: UTF-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class BooleanInputTest < ActionView::TestCase
|
|
|
|
test 'input should generate a checkbox by default for boolean attributes' do
|
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
assert_select 'input[type=checkbox].boolean#user_active'
|
2012-01-26 08:30:08 -05:00
|
|
|
assert_select 'label.boolean.optional', 'Active'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input does not generate the label with the checkbox when label option is false' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_input_for @user, :active, :boolean, label: false
|
2012-01-26 08:30:08 -05:00
|
|
|
assert_select 'input[type=checkbox].boolean#user_active'
|
|
|
|
assert_no_select 'label'
|
|
|
|
end
|
|
|
|
|
2012-08-26 08:06:19 -04:00
|
|
|
test 'input uses custom checked value' do
|
|
|
|
@user.action = 'on'
|
2013-01-28 16:02:59 -05:00
|
|
|
with_input_for @user, :action, :boolean, checked_value: 'on', unchecked_value: 'off'
|
2012-08-26 08:06:19 -04:00
|
|
|
assert_select 'input[type=checkbox][value=on][checked=checked]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input uses custom unchecked value' do
|
|
|
|
@user.action = 'off'
|
2013-01-28 16:02:59 -05:00
|
|
|
with_input_for @user, :action, :boolean, checked_value: 'on', unchecked_value: 'off'
|
2012-08-26 08:06:19 -04:00
|
|
|
assert_select 'input[type=checkbox][value=on]'
|
|
|
|
assert_no_select 'input[checked=checked][value=on]'
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input generates hidden input with custom unchecked value' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_input_for @user, :action, :boolean, checked_value: 'on', unchecked_value: 'off'
|
2012-08-26 08:06:19 -04:00
|
|
|
assert_select 'input[type=hidden][value=off]'
|
|
|
|
end
|
|
|
|
|
2012-01-26 12:06:33 -05:00
|
|
|
test 'input uses inline boolean style by default' do
|
2012-01-26 08:30:08 -05:00
|
|
|
with_input_for @user, :active, :boolean
|
2011-09-03 12:49:54 -04:00
|
|
|
assert_select 'input.boolean + label.boolean.optional'
|
2012-01-26 08:30:08 -05:00
|
|
|
assert_no_select 'label > input'
|
|
|
|
end
|
|
|
|
|
2012-01-30 11:31:07 -05:00
|
|
|
test 'input allows changing default boolean style config to nested, generating a default label and a manual hidden field for checkbox' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
2012-01-26 08:30:08 -05:00
|
|
|
with_input_for @user, :active, :boolean
|
2012-01-27 06:32:19 -05:00
|
|
|
assert_select 'label[for=user_active]', 'Active'
|
|
|
|
assert_select 'label.boolean > input.boolean'
|
2012-01-30 11:31:07 -05:00
|
|
|
assert_no_select 'input[type=checkbox] + label'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-01 10:35:36 -05:00
|
|
|
test 'input boolean with nested allows :inline_label' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, label: false, inline_label: 'I am so inline.'
|
|
|
|
assert_select 'label.checkbox', text: 'I am so inline.'
|
2012-03-01 10:35:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-30 01:34:40 -04:00
|
|
|
test 'input boolean with nested style creates an inline label using the default label text when inline_label option set to true' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, label: false, inline_label: true
|
|
|
|
assert_select 'label.checkbox', text: 'Active'
|
2012-04-30 01:34:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-30 13:38:28 -05:00
|
|
|
test 'input boolean with nested generates a manual hidden field for checkbox outside the label, to recreate Rails functionality with valid html5' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
2012-01-30 11:31:07 -05:00
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
|
|
|
|
assert_select "input[type=hidden][name='user[active]'] + label.boolean > input.boolean"
|
|
|
|
assert_no_select 'input[type=checkbox] + label'
|
2012-01-26 08:30:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-30 13:38:28 -05:00
|
|
|
test 'input boolean with nested generates a disabled hidden field for checkbox outside the label, if the field is disabled' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, disabled: true
|
2012-01-30 13:38:28 -05:00
|
|
|
|
|
|
|
assert_select "input[type=hidden][name='user[active]'][disabled] + label.boolean > input.boolean[disabled]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-26 12:44:13 -05:00
|
|
|
test 'input accepts changing boolean style to nested through given options' do
|
2013-01-28 16:02:59 -05:00
|
|
|
with_input_for @user, :active, :boolean, boolean_style: :nested
|
2012-01-27 06:32:19 -05:00
|
|
|
assert_select 'label[for=user_active]', 'Active'
|
|
|
|
assert_select 'label.boolean > input.boolean'
|
2012-01-30 11:31:07 -05:00
|
|
|
assert_no_select 'input[type=checkbox] + label'
|
2012-01-26 08:30:08 -05:00
|
|
|
end
|
|
|
|
|
2012-01-26 12:44:13 -05:00
|
|
|
test 'input accepts changing boolean style to inline through given options, when default is nested' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, boolean_style: :inline
|
2012-01-27 06:32:19 -05:00
|
|
|
assert_select 'label[for=user_active]', 'Active'
|
2012-01-26 12:44:13 -05:00
|
|
|
assert_select 'input.boolean + label.boolean'
|
|
|
|
assert_no_select 'label > input'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-26 08:30:08 -05:00
|
|
|
test 'input with nested style allows disabling label' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, label: false
|
2012-01-26 08:30:08 -05:00
|
|
|
assert_select 'input.boolean'
|
2012-01-30 08:15:05 -05:00
|
|
|
assert_no_select 'label.boolean'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-12 18:27:59 -04:00
|
|
|
test 'input with nested style allows customizing input_html' do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
|
|
|
with_input_for @user, :active, :boolean, input_html: { name: 'active_user' }
|
2012-07-12 18:27:59 -04:00
|
|
|
assert_select "input[type=hidden][name=active_user] + label.boolean > input.boolean[name=active_user]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-30 08:15:05 -05:00
|
|
|
test 'input boolean works using :input only in wrapper config (no label_input)' do
|
|
|
|
swap_wrapper do
|
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
|
|
|
|
assert_select 'label.boolean + input[type=hidden] + input.boolean'
|
|
|
|
assert_no_select 'label.checkbox'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'input boolean with nested style works using :input only in wrapper config (no label_input), adding the extra "checkbox" label wrapper' do
|
|
|
|
swap_wrapper do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
2012-01-30 08:15:05 -05:00
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
|
2012-01-30 11:31:07 -05:00
|
|
|
assert_select 'label.boolean + input[type=hidden] + label.checkbox > input.boolean'
|
2012-01-30 08:15:05 -05:00
|
|
|
end
|
2012-01-26 08:30:08 -05:00
|
|
|
end
|
2011-09-03 12:49:54 -04:00
|
|
|
end
|
2012-02-03 16:58:06 -05:00
|
|
|
|
|
|
|
test 'input boolean with nested style works using :label_input in wrapper config, adding "checkbox" class to label' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_without_top_level do
|
2013-01-28 16:02:59 -05:00
|
|
|
swap SimpleForm, boolean_style: :nested do
|
2012-02-03 16:58:06 -05:00
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
|
|
|
|
assert_select 'input[type=hidden] + label.boolean.checkbox > input.boolean'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-05-28 16:12:18 -04:00
|
|
|
|
|
|
|
test 'input boolean without additional classes should add "checkbox" class to label' do
|
|
|
|
swap_wrapper :default, self.custom_wrapper_without_top_level do
|
|
|
|
swap SimpleForm, boolean_style: :nested, generate_additional_classes_for: [:input] do
|
|
|
|
with_input_for @user, :active, :boolean
|
|
|
|
|
|
|
|
assert_select 'label'
|
|
|
|
assert_select 'label.checkbox'
|
|
|
|
assert_no_select 'label.boolean'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-01-26 07:51:12 -05:00
|
|
|
end
|