2017-07-23 11:36:41 -04:00
# frozen_string_literal: true
2016-08-06 12:50:17 -04:00
require " abstract_unit "
2012-01-31 12:45:59 -05:00
2017-01-13 01:09:56 -05:00
Category = Struct . new ( :id , :name )
2012-01-31 13:31:17 -05:00
2012-01-31 12:45:59 -05:00
class FormCollectionsHelperTest < ActionView :: TestCase
def assert_no_select ( selector , value = nil )
2016-08-06 13:36:34 -04:00
assert_select ( selector , text : value , count : 0 )
2012-01-31 12:45:59 -05:00
end
def with_collection_radio_buttons ( * args , & block )
2012-01-31 15:00:42 -05:00
@output_buffer = collection_radio_buttons ( * args , & block )
2012-01-31 12:45:59 -05:00
end
2012-01-31 13:31:17 -05:00
def with_collection_check_boxes ( * args , & block )
2012-01-31 15:00:42 -05:00
@output_buffer = collection_check_boxes ( * args , & block )
2012-01-31 13:31:17 -05:00
end
# COLLECTION RADIO BUTTONS
2016-08-06 12:50:17 -04:00
test " collection radio accepts a collection and generates inputs from value method " do
2012-01-31 12:45:59 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true] # user_active_true "
assert_select " input[type=radio][value=false] # user_active_false "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts a collection and generates inputs from label method " do
2012-01-31 12:45:59 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true] " , " true "
assert_select " label[for=user_active_false] " , " false "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio handles camelized collection values for labels correctly " do
with_collection_radio_buttons :user , :active , [ " Yes " , " No " ] , :to_s , :to_s
2012-01-31 12:45:59 -05:00
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_yes] " , " Yes "
assert_select " label[for=user_active_no] " , " No "
2012-01-31 12:45:59 -05:00
end
2017-07-28 10:18:10 -04:00
test " collection radio generates labels for non-English values correctly " do
with_collection_radio_buttons :user , :title , [ " Господин " , " Госпожа " ] , :to_s , :to_s
assert_select " input[type=radio] # user_title_г о с по дин "
assert_select " label[for=user_title_г о с по дин] " , " Господин "
end
2016-08-06 12:50:17 -04:00
test " collection radio should sanitize collection values for labels correctly " do
with_collection_radio_buttons :user , :name , [ " $0.99 " , " $1.99 " ] , :to_s , :to_s
2019-01-18 18:56:12 -05:00
assert_select " label[for=user_name_0_99] " , " $0.99 "
assert_select " label[for=user_name_1_99] " , " $1.99 "
end
test " collection radio correctly builds unique DOM IDs for float values " do
with_collection_radio_buttons :user , :name , [ 1 . 0 , 10 ] , :to_s , :to_s
assert_select " label[for=user_name_1_0] " , " 1.0 "
assert_select " label[for=user_name_10] " , " 10 "
assert_select 'input#user_name_1_0[type=radio][value="1.0"]'
assert_select 'input#user_name_10[type=radio][value="10"]'
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts checked item " do
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , [ [ 1 , true ] , [ 0 , false ] ] , :last , :first , checked : true
2012-01-31 12:45:59 -05:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true][checked=checked] "
assert_no_select " input[type=radio][value=false][checked=checked] "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts multiple disabled items " do
collection = [ [ 1 , true ] , [ 0 , false ] , [ 2 , " other " ] ]
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , collection , :last , :first , disabled : [ true , false ]
2012-01-31 12:45:59 -05:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true][disabled=disabled] "
assert_select " input[type=radio][value=false][disabled=disabled] "
assert_no_select " input[type=radio][value=other][disabled=disabled] "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts single disabled item " do
2012-01-31 12:45:59 -05:00
collection = [ [ 1 , true ] , [ 0 , false ] ]
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , collection , :last , :first , disabled : true
2012-01-31 12:45:59 -05:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true][disabled=disabled] "
assert_no_select " input[type=radio][value=false][disabled=disabled] "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts multiple readonly items " do
collection = [ [ 1 , true ] , [ 0 , false ] , [ 2 , " other " ] ]
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , collection , :last , :first , readonly : [ true , false ]
2014-04-12 14:14:35 -04:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true][readonly=readonly] "
assert_select " input[type=radio][value=false][readonly=readonly] "
assert_no_select " input[type=radio][value=other][readonly=readonly] "
2014-04-12 14:14:35 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts single readonly item " do
2014-04-12 14:14:35 -04:00
collection = [ [ 1 , true ] , [ 0 , false ] ]
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , collection , :last , :first , readonly : true
2014-04-12 14:14:35 -04:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true][readonly=readonly] "
assert_no_select " input[type=radio][value=false][readonly=readonly] "
2014-04-12 14:14:35 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts html options as input " do
2012-01-31 12:45:59 -05:00
collection = [ [ 1 , true ] , [ 0 , false ] ]
2017-08-12 07:31:46 -04:00
with_collection_radio_buttons :user , :active , collection , :last , :first , { } , { class : " special-radio " }
2012-01-31 12:45:59 -05:00
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true].special-radio # user_active_true "
assert_select " input[type=radio][value=false].special-radio # user_active_false "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts html options as the last element of array " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , true , { class : " foo " } ] , [ 0 , false , { class : " bar " } ] ]
2013-06-16 10:39:03 -04:00
with_collection_radio_buttons :user , :active , collection , :second , :first
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio][value=true].foo # user_active_true "
assert_select " input[type=radio][value=false].bar # user_active_false "
2013-06-16 10:39:03 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection radio sets the label class defined inside the block " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , true , { class : " foo " } ] , [ 0 , false , { class : " bar " } ] ]
2013-12-01 01:50:21 -05:00
with_collection_radio_buttons :user , :active , collection , :second , :first do | b |
b . label ( class : " collection_radio_buttons " )
end
2016-08-06 12:50:17 -04:00
assert_select " label.collection_radio_buttons[for=user_active_true] "
assert_select " label.collection_radio_buttons[for=user_active_false] "
2013-12-01 01:50:21 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio does not include the input class in the respective label " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , true , { class : " foo " } ] , [ 0 , false , { class : " bar " } ] ]
2013-12-01 01:50:21 -05:00
with_collection_radio_buttons :user , :active , collection , :second , :first
2016-08-06 12:50:17 -04:00
assert_no_select " label.foo[for=user_active_true] "
assert_no_select " label.bar[for=user_active_false] "
2013-12-01 01:50:21 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio does not wrap input inside the label " do
2012-01-31 12:45:59 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s
2016-08-06 12:50:17 -04:00
assert_select " input[type=radio] + label "
assert_no_select " label input "
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts a block to render the label as radio button wrapper " do
2012-02-01 13:14:24 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label { b . radio_button }
2012-01-31 12:45:59 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true] > input # user_active_true[type=radio] "
assert_select " label[for=user_active_false] > input # user_active_false[type=radio] "
2012-01-31 12:45:59 -05:00
end
2012-01-31 13:31:17 -05:00
2016-08-06 12:50:17 -04:00
test " collection radio accepts a block to change the order of label and radio button " do
2012-02-01 16:16:58 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label + b . radio_button
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true] + input # user_active_true[type=radio] "
assert_select " label[for=user_active_false] + input # user_active_false[type=radio] "
2012-02-01 16:16:58 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio with block helpers accept extra html options " do
2012-02-01 16:46:48 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( class : " radio_button " ) + b . radio_button ( class : " radio_button " )
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.radio_button[for=user_active_true] + input # user_active_true.radio_button[type=radio] "
assert_select " label.radio_button[for=user_active_false] + input # user_active_false.radio_button[type=radio] "
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio with block helpers allows access to current text and value " do
2012-02-01 16:46:48 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( " data-value " : b . value ) { b . radio_button + b . text }
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true][data-value=true] " , " true " do
assert_select " input # user_active_true[type=radio] "
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_false][data-value=false] " , " false " do
assert_select " input # user_active_false[type=radio] "
2012-02-01 16:46:48 -05:00
end
end
2016-08-06 12:50:17 -04:00
test " collection radio with block helpers allows access to the current object item in the collection to access extra properties " do
2012-02-13 19:48:47 -05:00
with_collection_radio_buttons :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( class : b . object ) { b . radio_button + b . text }
2012-02-13 19:48:47 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.true[for=user_active_true] " , " true " do
assert_select " input # user_active_true[type=radio] "
2012-02-13 19:48:47 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.false[for=user_active_false] " , " false " do
assert_select " input # user_active_false[type=radio] "
2012-02-13 19:48:47 -05:00
end
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons with fields for " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2012-01-31 15:00:42 -05:00
@output_buffer = fields_for ( :post ) do | p |
2012-01-31 14:46:31 -05:00
p . collection_radio_buttons :category_id , collection , :id , :name
2012-01-31 15:00:42 -05:00
end
2012-01-31 14:46:31 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input#post_category_id_1[type=radio][value="1"]'
assert_select 'input#post_category_id_2[type=radio][value="2"]'
2012-01-31 14:46:31 -05:00
2016-08-06 12:50:17 -04:00
assert_select " label[for=post_category_id_1] " , " Category 1 "
assert_select " label[for=post_category_id_2] " , " Category 2 "
2012-01-31 14:46:31 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio accepts checked item which has a value of false " do
2016-08-06 13:36:34 -04:00
with_collection_radio_buttons :user , :active , [ [ 1 , true ] , [ 0 , false ] ] , :last , :first , checked : false
2016-08-06 12:50:17 -04:00
assert_no_select " input[type=radio][value=true][checked=checked] "
assert_select " input[type=radio][value=false][checked=checked] "
2012-12-26 21:00:31 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons generates only one hidden field for the entire collection, to ensure something will be sent back to the server when posting an empty collection " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2015-01-02 13:53:38 -05:00
with_collection_radio_buttons :user , :category_ids , collection , :id , :name
2015-12-30 22:40:33 -05:00
assert_select " input[type=hidden][name='user[category_ids]'][value=''] " , count : 1
2015-01-02 13:53:38 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons generates a hidden field using the given :name in :html_options " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2017-08-12 07:31:46 -04:00
with_collection_radio_buttons :user , :category_ids , collection , :id , :name , { } , { name : " user[other_category_ids] " }
2015-01-02 13:53:38 -05:00
2015-12-30 22:40:33 -05:00
assert_select " input[type=hidden][name='user[other_category_ids]'][value=''] " , count : 1
2015-01-02 13:53:38 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons generates a hidden field with index if it was provided " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2016-08-06 13:44:11 -04:00
with_collection_radio_buttons :user , :category_ids , collection , :id , :name , index : 322
2015-01-02 13:53:38 -05:00
2015-12-30 22:40:33 -05:00
assert_select " input[type=hidden][name='user[322][category_ids]'][value=''] " , count : 1
2015-01-02 13:53:38 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons does not generate a hidden field if include_hidden option is false " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2015-01-02 13:53:38 -05:00
with_collection_radio_buttons :user , :category_ids , collection , :id , :name , include_hidden : false
2015-12-30 22:40:33 -05:00
assert_select " input[type=hidden][name='user[category_ids]'][value=''] " , count : 0
2015-01-02 13:53:38 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection radio buttons does not generate a hidden field if include_hidden option is false with key as string " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
with_collection_radio_buttons :user , :category_ids , collection , :id , :name , " include_hidden " = > false
2015-01-02 13:53:38 -05:00
2015-12-30 22:40:33 -05:00
assert_select " input[type=hidden][name='user[category_ids]'][value=''] " , count : 0
2015-01-02 13:53:38 -05:00
end
2012-01-31 13:31:17 -05:00
# COLLECTION CHECK BOXES
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a collection and generate a series of checkboxes for value method " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2012-01-31 14:25:42 -05:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input#user_category_ids_1[type=checkbox][value="1"]'
assert_select 'input#user_category_ids_2[type=checkbox][value="2"]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes generates only one hidden field for the entire collection, to ensure something will be sent back to the server when posting an empty collection " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2012-01-31 14:25:42 -05:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name
2012-01-31 13:31:17 -05:00
2016-08-06 13:36:34 -04:00
assert_select " input[type=hidden][name='user[category_ids][]'][value=''] " , count : 1
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes generates a hidden field using the given :name in :html_options " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2017-08-12 07:31:46 -04:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name , { } , { name : " user[other_category_ids][] " }
2013-09-24 14:42:06 -04:00
2016-08-06 13:36:34 -04:00
assert_select " input[type=hidden][name='user[other_category_ids][]'][value=''] " , count : 1
2013-09-24 14:42:06 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes generates a hidden field with index if it was provided " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2016-08-06 13:44:11 -04:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name , index : 322
2014-04-13 14:58:42 -04:00
2014-05-27 10:27:52 -04:00
assert_select " input[type=hidden][name='user[322][category_ids][]'][value=''] " , count : 1
2014-04-13 14:58:42 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes does not generate a hidden field if include_hidden option is false " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2013-10-27 11:14:16 -04:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name , include_hidden : false
2016-08-06 13:36:34 -04:00
assert_select " input[type=hidden][name='user[category_ids][]'][value=''] " , count : 0
2013-10-27 11:14:16 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes does not generate a hidden field if include_hidden option is false with key as string " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
with_collection_check_boxes :user , :category_ids , collection , :id , :name , " include_hidden " = > false
2015-01-05 17:32:33 -05:00
assert_select " input[type=hidden][name='user[category_ids][]'][value=''] " , count : 0
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a collection and generate a series of checkboxes with labels for label method " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2012-01-31 14:25:42 -05:00
with_collection_check_boxes :user , :category_ids , collection , :id , :name
2012-01-31 13:31:17 -05:00
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_category_ids_1] " , " Category 1 "
assert_select " label[for=user_category_ids_2] " , " Category 2 "
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes handles camelized collection values for labels correctly " do
with_collection_check_boxes :user , :active , [ " Yes " , " No " ] , :to_s , :to_s
2012-01-31 13:31:17 -05:00
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_yes] " , " Yes "
assert_select " label[for=user_active_no] " , " No "
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check box should sanitize collection values for labels correctly " do
with_collection_check_boxes :user , :name , [ " $0.99 " , " $1.99 " ] , :to_s , :to_s
2019-01-18 18:56:12 -05:00
assert_select " label[for=user_name_0_99] " , " $0.99 "
assert_select " label[for=user_name_1_99] " , " $1.99 "
end
test " collection check boxes correctly builds unique DOM IDs for float values " do
with_collection_check_boxes :user , :name , [ 1 . 0 , 10 ] , :to_s , :to_s
assert_select " label[for=user_name_1_0] " , " 1.0 "
assert_select " label[for=user_name_10] " , " 10 "
assert_select 'input#user_name_1_0[type=checkbox][value="1.0"]'
assert_select 'input#user_name_10[type=checkbox][value="10"]'
2012-01-31 13:31:17 -05:00
end
2017-07-28 10:18:10 -04:00
test " collection check boxes generates labels for non-English values correctly " do
with_collection_check_boxes :user , :title , [ " Господин " , " Госпожа " ] , :to_s , :to_s
assert_select " input[type=checkbox] # user_title_г о с по дин "
assert_select " label[for=user_title_г о с по дин] " , " Господин "
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts html options as the last element of array " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , " Category 1 " , { class : " foo " } ] , [ 2 , " Category 2 " , { class : " bar " } ] ]
2013-06-16 10:39:03 -04:00
with_collection_check_boxes :user , :active , collection , :first , :second
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"].foo'
assert_select 'input[type=checkbox][value="2"].bar'
2013-06-16 10:39:03 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes propagates input id to the label for attribute " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , " Category 1 " , { id : " foo " } ] , [ 2 , " Category 2 " , { id : " bar " } ] ]
2015-06-08 16:55:10 -04:00
with_collection_check_boxes :user , :active , collection , :first , :second
assert_select 'input[type=checkbox][value="1"]#foo'
assert_select 'input[type=checkbox][value="2"]#bar'
2016-08-06 12:50:17 -04:00
assert_select " label[for=foo] "
assert_select " label[for=bar] "
2015-06-08 16:55:10 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes sets the label class defined inside the block " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , " Category 1 " , { class : " foo " } ] , [ 2 , " Category 2 " , { class : " bar " } ] ]
2013-12-01 01:50:21 -05:00
with_collection_check_boxes :user , :active , collection , :second , :first do | b |
2016-08-06 12:50:17 -04:00
b . label ( class : " collection_check_boxes " )
2013-12-01 01:50:21 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.collection_check_boxes[for=user_active_category_1] "
assert_select " label.collection_check_boxes[for=user_active_category_2] "
2013-12-01 01:50:21 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes does not include the input class in the respective label " do
2016-08-16 03:30:11 -04:00
collection = [ [ 1 , " Category 1 " , { class : " foo " } ] , [ 2 , " Category 2 " , { class : " bar " } ] ]
2013-12-01 01:50:21 -05:00
with_collection_check_boxes :user , :active , collection , :second , :first
2016-08-06 12:50:17 -04:00
assert_no_select " label.foo[for=user_active_category_1] "
assert_no_select " label.bar[for=user_active_category_2] "
2013-12-01 01:50:21 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts selected values as :checked option " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , checked : [ 1 , 3 ]
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][checked=checked]'
assert_select 'input[type=checkbox][value="3"][checked=checked]'
assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts selected string values as :checked option " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , checked : [ " 1 " , " 3 " ]
2012-05-05 13:34:06 -04:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][checked=checked]'
assert_select 'input[type=checkbox][value="3"][checked=checked]'
assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
2012-05-05 13:34:06 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a single checked value " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , checked : 3
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="3"][checked=checked]'
assert_no_select 'input[type=checkbox][value="1"][checked=checked]'
assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts selected values as :checked option and override the model values " do
2012-01-31 14:53:43 -05:00
user = Struct . new ( :category_ids ) . new ( 2 )
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2012-01-31 14:53:43 -05:00
2012-01-31 15:00:42 -05:00
@output_buffer = fields_for ( :user , user ) do | p |
2016-08-06 13:36:34 -04:00
p . collection_check_boxes :category_ids , collection , :first , :last , checked : [ 1 , 3 ]
2012-01-31 15:00:42 -05:00
end
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][checked=checked]'
assert_select 'input[type=checkbox][value="3"][checked=checked]'
assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts multiple disabled items " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , disabled : [ 1 , 3 ]
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][disabled=disabled]'
assert_select 'input[type=checkbox][value="3"][disabled=disabled]'
assert_no_select 'input[type=checkbox][value="2"][disabled=disabled]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts single disabled item " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , disabled : 1
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][disabled=disabled]'
assert_no_select 'input[type=checkbox][value="3"][disabled=disabled]'
assert_no_select 'input[type=checkbox][value="2"][disabled=disabled]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a proc to disabled items " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , disabled : proc { | i | i . first == 1 }
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input[type=checkbox][value="1"][disabled=disabled]'
assert_no_select 'input[type=checkbox][value="3"][disabled=disabled]'
assert_no_select 'input[type=checkbox][value="2"][disabled=disabled]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts multiple readonly items " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , readonly : [ 1 , 3 ]
2014-04-12 14:14:35 -04:00
2014-05-27 10:27:52 -04:00
assert_select 'input[type=checkbox][value="1"][readonly=readonly]'
assert_select 'input[type=checkbox][value="3"][readonly=readonly]'
assert_no_select 'input[type=checkbox][value="2"][readonly=readonly]'
2014-04-12 14:14:35 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts single readonly item " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , readonly : 1
2014-04-12 14:14:35 -04:00
2014-05-27 10:27:52 -04:00
assert_select 'input[type=checkbox][value="1"][readonly=readonly]'
assert_no_select 'input[type=checkbox][value="3"][readonly=readonly]'
assert_no_select 'input[type=checkbox][value="2"][readonly=readonly]'
2014-04-12 14:14:35 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a proc to readonly items " do
2016-08-16 03:30:11 -04:00
collection = ( 1 .. 3 ) . map { | i | [ i , " Category #{ i } " ] }
2016-08-06 13:36:34 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , readonly : proc { | i | i . first == 1 }
2014-04-12 14:14:35 -04:00
2014-05-27 10:27:52 -04:00
assert_select 'input[type=checkbox][value="1"][readonly=readonly]'
assert_no_select 'input[type=checkbox][value="3"][readonly=readonly]'
assert_no_select 'input[type=checkbox][value="2"][readonly=readonly]'
2014-04-12 14:14:35 -04:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts html options " do
collection = [ [ 1 , " Category 1 " ] , [ 2 , " Category 2 " ] ]
2017-08-12 07:31:46 -04:00
with_collection_check_boxes :user , :category_ids , collection , :first , :last , { } , { class : " check " }
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input.check[type=checkbox][value="1"]'
assert_select 'input.check[type=checkbox][value="2"]'
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes with fields for " do
collection = [ Category . new ( 1 , " Category 1 " ) , Category . new ( 2 , " Category 2 " ) ]
2012-01-31 15:00:42 -05:00
@output_buffer = fields_for ( :post ) do | p |
2012-01-31 14:46:31 -05:00
p . collection_check_boxes :category_ids , collection , :id , :name
2012-01-31 15:00:42 -05:00
end
2012-01-31 13:31:17 -05:00
2013-07-23 10:35:07 -04:00
assert_select 'input#post_category_ids_1[type=checkbox][value="1"]'
assert_select 'input#post_category_ids_2[type=checkbox][value="2"]'
2012-01-31 13:31:17 -05:00
2016-08-06 12:50:17 -04:00
assert_select " label[for=post_category_ids_1] " , " Category 1 "
assert_select " label[for=post_category_ids_2] " , " Category 2 "
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes does not wrap input inside the label " do
2012-01-31 13:31:17 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s
2016-08-06 12:50:17 -04:00
assert_select " input[type=checkbox] + label "
assert_no_select " label input "
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a block to render the label as check box wrapper " do
2012-02-01 13:14:24 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label { b . check_box }
2012-01-31 13:31:17 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true] > input # user_active_true[type=checkbox] "
assert_select " label[for=user_active_false] > input # user_active_false[type=checkbox] "
2012-01-31 13:31:17 -05:00
end
2012-02-01 16:16:58 -05:00
2016-08-06 12:50:17 -04:00
test " collection check boxes accepts a block to change the order of label and check box " do
2012-02-01 16:16:58 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
b . label + b . check_box
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true] + input # user_active_true[type=checkbox] "
assert_select " label[for=user_active_false] + input # user_active_false[type=checkbox] "
2012-02-01 16:16:58 -05:00
end
2012-02-01 16:46:48 -05:00
2016-08-06 12:50:17 -04:00
test " collection check boxes with block helpers accept extra html options " do
2012-02-01 16:46:48 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( class : " check_box " ) + b . check_box ( class : " check_box " )
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.check_box[for=user_active_true] + input # user_active_true.check_box[type=checkbox] "
assert_select " label.check_box[for=user_active_false] + input # user_active_false.check_box[type=checkbox] "
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
test " collection check boxes with block helpers allows access to current text and value " do
2012-02-01 16:46:48 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( " data-value " : b . value ) { b . check_box + b . text }
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_true][data-value=true] " , " true " do
assert_select " input # user_active_true[type=checkbox] "
2012-02-01 16:46:48 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label[for=user_active_false][data-value=false] " , " false " do
assert_select " input # user_active_false[type=checkbox] "
2012-02-01 16:46:48 -05:00
end
end
2012-02-13 19:48:47 -05:00
2016-08-06 12:50:17 -04:00
test " collection check boxes with block helpers allows access to the current object item in the collection to access extra properties " do
2012-02-13 19:48:47 -05:00
with_collection_check_boxes :user , :active , [ true , false ] , :to_s , :to_s do | b |
2016-08-06 13:36:34 -04:00
b . label ( class : b . object ) { b . check_box + b . text }
2012-02-13 19:48:47 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.true[for=user_active_true] " , " true " do
assert_select " input # user_active_true[type=checkbox] "
2012-02-13 19:48:47 -05:00
end
2016-08-06 12:50:17 -04:00
assert_select " label.false[for=user_active_false] " , " false " do
assert_select " input # user_active_false[type=checkbox] "
2012-02-13 19:48:47 -05:00
end
end
2012-01-31 12:45:59 -05:00
end