require "abstract_unit" class Map < Hash def category "" end end class FormOptionsHelperTest < ActionView::TestCase tests ActionView::Helpers::FormOptionsHelper silence_warnings do Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :category, :origin, :allow_comments) Continent = Struct.new("Continent", :continent_name, :countries) Country = Struct.new("Country", :country_id, :country_name) Firm = Struct.new("Firm", :time_zone) Album = Struct.new("Album", :id, :title, :genre) end module FakeZones FakeZone = Struct.new(:name) do def to_s; name; end end module ClassMethods def [](id); fake_zones ? fake_zones[id] : super; end def all; fake_zones ? fake_zones.values : super; end def dummy; :test; end end def self.prepended(base) class << base mattr_accessor(:fake_zones) prepend ClassMethods end end end ActiveSupport::TimeZone.prepend FakeZones setup do ActiveSupport::TimeZone.fake_zones = %w(A B C D E).map do |id| [ id, FakeZones::FakeZone.new(id) ] end.to_h @fake_timezones = ActiveSupport::TimeZone.all end teardown do ActiveSupport::TimeZone.fake_zones = nil end def test_collection_options assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title") ) end def test_collection_options_with_preselected_value assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", "Babe") ) end def test_collection_options_with_preselected_value_array assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", [ "Babe", "Cabe" ]) ) end def test_collection_options_with_proc_for_selected assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", lambda { |p| p.author_name == "Babe" }) ) end def test_collection_options_with_disabled_value assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: "Babe") ) end def test_collection_options_with_disabled_array assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: [ "Babe", "Cabe" ]) ) end def test_collection_options_with_preselected_and_disabled_value assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", selected: "Cabe", disabled: "Babe") ) end def test_collection_options_with_proc_for_disabled assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: lambda { |p| %w(Babe Cabe).include?(p.author_name) }) ) end def test_collection_options_with_proc_for_value_method assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, lambda { |p| p.author_name }, "title") ) end def test_collection_options_with_proc_for_text_method assert_dom_equal( "\n\n", options_from_collection_for_select(dummy_posts, "author_name", lambda { |p| p.title }) ) end def test_collection_options_with_element_attributes assert_dom_equal( "", options_from_collection_for_select([[ "USA", "USA", { class: "bold" } ]], :first, :second) ) end def test_string_options_for_select options = "" assert_dom_equal( options, options_for_select(options) ) end def test_array_options_for_select assert_dom_equal( "\n\n", options_for_select([ "", "USA", "Sweden" ]) ) end def test_array_options_for_select_with_custom_defined_selected assert_dom_equal( "\n", options_for_select([ ["Richard Bandler", 1, { type: "Coach", selected: "selected" }], ["Richard Bandler", 1, { type: "Coachee" }] ]) ) end def test_array_options_for_select_with_custom_defined_disabled assert_dom_equal( "\n", options_for_select([ ["Richard Bandler", 1, { type: "Coach", disabled: "disabled" }], ["Richard Bandler", 1, { type: "Coachee" }] ]) ) end def test_array_options_for_select_with_selection assert_dom_equal( "\n\n", options_for_select([ "Denmark", "", "Sweden" ], "") ) end def test_array_options_for_select_with_selection_array assert_dom_equal( "\n\n", options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) ) end def test_array_options_for_select_with_disabled_value assert_dom_equal( "\n\n", options_for_select([ "Denmark", "", "Sweden" ], disabled: "") ) end def test_array_options_for_select_with_disabled_array assert_dom_equal( "\n\n", options_for_select([ "Denmark", "", "Sweden" ], disabled: ["", "Sweden"]) ) end def test_array_options_for_select_with_selection_and_disabled_value assert_dom_equal( "\n\n", options_for_select([ "Denmark", "", "Sweden" ], selected: "Denmark", disabled: "") ) end def test_boolean_array_options_for_select_with_selection_and_disabled_value assert_dom_equal( "\n", options_for_select([ true, false ], selected: false, disabled: nil) ) end def test_range_options_for_select assert_dom_equal( "\n\n", options_for_select(1..3) ) end def test_array_options_for_string_include_in_other_string_bug_fix assert_dom_equal( "\n", options_for_select([ "ruby", "rubyonrails" ], "rubyonrails") ) assert_dom_equal( "\n", options_for_select([ "ruby", "rubyonrails" ], "ruby") ) assert_dom_equal( %(\n\n), options_for_select([ "ruby", "rubyonrails", nil ], "ruby") ) end def test_hash_options_for_select assert_dom_equal( "\n", options_for_select("$" => "Dollar", "" => "").split("\n").join("\n") ) assert_dom_equal( "\n", options_for_select({ "$" => "Dollar", "" => "" }, "Dollar").split("\n").join("\n") ) assert_dom_equal( "\n", options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]).split("\n").join("\n") ) end def test_ducktyped_options_for_select quack = Struct.new(:first, :last) assert_dom_equal( "\n", options_for_select([quack.new("", ""), quack.new("$", "Dollar")]) ) assert_dom_equal( "\n", options_for_select([quack.new("", ""), quack.new("$", "Dollar")], "Dollar") ) assert_dom_equal( "\n", options_for_select([quack.new("", ""), quack.new("$", "Dollar")], ["Dollar", ""]) ) end def test_collection_options_with_preselected_value_as_string_and_option_value_is_integer albums = [ Album.new(1, "first", "rap"), Album.new(2, "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", selected: "1") ) end def test_collection_options_with_preselected_value_as_integer_and_option_value_is_string albums = [ Album.new("1", "first", "rap"), Album.new("2", "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", selected: 1) ) end def test_collection_options_with_preselected_value_as_string_and_option_value_is_float albums = [ Album.new(1.0, "first", "rap"), Album.new(2.0, "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", selected: "2.0") ) end def test_collection_options_with_preselected_value_as_nil albums = [ Album.new(1.0, "first", "rap"), Album.new(2.0, "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", selected: nil) ) end def test_collection_options_with_disabled_value_as_nil albums = [ Album.new(1.0, "first", "rap"), Album.new(2.0, "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", disabled: nil) ) end def test_collection_options_with_disabled_value_as_array albums = [ Album.new(1.0, "first", "rap"), Album.new(2.0, "second", "pop")] assert_dom_equal( %(\n), options_from_collection_for_select(albums, "id", "genre", disabled: ["1.0", 2.0]) ) end def test_collection_options_with_preselected_values_as_string_array_and_option_value_is_float albums = [ Album.new(1.0, "first", "rap"), Album.new(2.0, "second", "pop"), Album.new(3.0, "third", "country") ] assert_dom_equal( %(\n\n), options_from_collection_for_select(albums, "id", "genre", ["1.0", "3.0"]) ) end def test_option_groups_from_collection_for_select assert_dom_equal( "\n\n", option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk") ) end def test_option_groups_from_collection_for_select_returns_html_safe_string assert option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk").html_safe? end def test_grouped_options_for_select_with_array assert_dom_equal( "\n\n", grouped_options_for_select([ ["North America", [["United States", "US"], "Canada"]], ["Europe", [["Great Britain", "GB"], "Germany"]] ]) ) end def test_grouped_options_for_select_with_array_and_html_attributes assert_dom_equal( "\n\n", grouped_options_for_select([ ["North America", [["United States", "US"], "Canada"], data: { foo: "bar" }], ["Europe", [["Great Britain", "GB"], "Germany"], disabled: "disabled"] ]) ) end def test_grouped_options_for_select_with_optional_divider assert_dom_equal( "\n\n", grouped_options_for_select([["US", "Canada"] , ["GB", "Germany"]], nil, divider: "----------") ) end def test_grouped_options_for_select_with_selected_and_prompt assert_dom_equal( "\n", grouped_options_for_select([["Hats", ["Baseball Cap", "Cowboy Hat"]]], "Cowboy Hat", prompt: "Choose a product...") ) end def test_grouped_options_for_select_with_selected_and_prompt_true assert_dom_equal( "\n", grouped_options_for_select([["Hats", ["Baseball Cap", "Cowboy Hat"]]], "Cowboy Hat", prompt: true) ) end def test_grouped_options_for_select_returns_html_safe_string assert grouped_options_for_select([["Hats", ["Baseball Cap", "Cowboy Hat"]]]).html_safe? end def test_grouped_options_for_select_with_prompt_returns_html_escaped_string assert_dom_equal( "\n", grouped_options_for_select([["Hats", ["Baseball Cap", "Cowboy Hat"]]], nil, prompt: "")) end def test_optgroups_with_with_options_with_hash assert_dom_equal( "\n\n", grouped_options_for_select("North America" => ["United States", "Canada"], "Europe" => ["Denmark", "Germany"]) ) end def test_time_zone_options_no_params opts = time_zone_options_for_select assert_dom_equal "\n" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_selected opts = time_zone_options_for_select("D") assert_dom_equal "\n" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_unknown_selected opts = time_zone_options_for_select("K") assert_dom_equal "\n" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_priority_zones zones = [ ActiveSupport::TimeZone.new("B"), ActiveSupport::TimeZone.new("E") ] opts = time_zone_options_for_select(nil, zones) assert_dom_equal "\n" + "" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_selected_priority_zones zones = [ ActiveSupport::TimeZone.new("B"), ActiveSupport::TimeZone.new("E") ] opts = time_zone_options_for_select("E", zones) assert_dom_equal "\n" + "" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_unselected_priority_zones zones = [ ActiveSupport::TimeZone.new("B"), ActiveSupport::TimeZone.new("E") ] opts = time_zone_options_for_select("C", zones) assert_dom_equal "\n" + "" + "\n" + "\n" + "\n" + "", opts end def test_time_zone_options_with_priority_zones_does_not_mutate_time_zones original_zones = ActiveSupport::TimeZone.all.dup zones = [ ActiveSupport::TimeZone.new("B"), ActiveSupport::TimeZone.new("E") ] time_zone_options_for_select(nil, zones) assert_equal original_zones, ActiveSupport::TimeZone.all end def test_time_zone_options_returns_html_safe_string assert time_zone_options_for_select.html_safe? end def test_select @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest)) ) end def test_select_without_multiple assert_dom_equal( "", select(:post, :category, "", {}, multiple: false) ) end def test_select_with_grouped_collection_as_nested_array @post = Post.new countries_by_continent = [ ["", [["", ""], ["Somalia", "so"]]], ["Europe", [["Denmark", "dk"], ["Ireland", "ie"]]], ] assert_dom_equal( [ '', ].join("\n"), select("post", "origin", countries_by_continent) ) end def test_select_with_grouped_collection_as_hash @post = Post.new countries_by_continent = { "" => [["", ""], ["Somalia", "so"]], "Europe" => [["Denmark", "dk"], ["Ireland", "ie"]], } assert_dom_equal( [ '', ].join("\n"), select("post", "origin", countries_by_continent) ) end def test_select_with_boolean_method @post = Post.new @post.allow_comments = false assert_dom_equal( "", select("post", "allow_comments", %w( true false )) ) end def test_select_under_fields_for @post = Post.new @post.category = "" output_buffer = fields_for :post, @post do |f| concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", output_buffer ) end def test_fields_for_with_record_inherited_from_hash map = Map.new output_buffer = fields_for :map, map do |f| concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", output_buffer ) end def test_select_under_fields_for_with_index @post = Post.new @post.category = "" output_buffer = fields_for :post, @post, index: 108 do |f| concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", output_buffer ) end def test_select_under_fields_for_with_auto_index @post = Post.new @post.category = "" def @post.to_param; 108; end output_buffer = fields_for "post[]", @post do |f| concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", output_buffer ) end def test_select_under_fields_for_with_string_and_given_prompt @post = Post.new options = raw("") output_buffer = fields_for :post, @post do |f| concat f.select(:category, options, prompt: "The prompt") end assert_dom_equal( "", output_buffer ) end def test_select_under_fields_for_with_block @post = Post.new output_buffer = fields_for :post, @post do |f| concat(f.select(:category) do concat content_tag(:option, "hello world") end) end assert_dom_equal( "", output_buffer ) end def test_select_under_fields_for_with_block_without_options @post = Post.new output_buffer = fields_for :post, @post do |f| concat(f.select(:category) {}) end assert_dom_equal( "", output_buffer ) end def test_select_with_multiple_to_add_hidden_input output_buffer = select(:post, :category, "", {}, multiple: true) assert_dom_equal( "", output_buffer ) end def test_select_with_multiple_and_without_hidden_input output_buffer = select(:post, :category, "", { include_hidden: false }, multiple: true) assert_dom_equal( "", output_buffer ) end def test_select_with_multiple_and_with_explicit_name_ending_with_brackets output_buffer = select(:post, :category, [], { include_hidden: false }, multiple: true, name: "post[category][]") assert_dom_equal( "", output_buffer ) end def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input output_buffer = select(:post, :category, "", {}, multiple: true, disabled: true) assert_dom_equal( "", output_buffer ) end def test_select_with_blank @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), include_blank: true) ) end def test_select_with_include_blank_false_and_required @post = Post.new @post.category = "" e = assert_raises(ArgumentError) { select("post", "category", %w( abe hest), { include_blank: false }, required: "required") } assert_match(/include_blank cannot be false for a required field./, e.message) end def test_select_with_blank_as_string @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), include_blank: "None") ) end def test_select_with_blank_as_string_escaped @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), include_blank: "") ) end def test_select_with_default_prompt @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), prompt: true) ) end def test_select_no_prompt_when_select_has_value @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), prompt: true) ) end def test_select_with_given_prompt @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), prompt: "The prompt") ) end def test_select_with_given_prompt_escaped @post = Post.new assert_dom_equal( "", select("post", "category", %w( abe hest), prompt: "") ) end def test_select_with_prompt_and_blank @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest), prompt: true, include_blank: true) ) end def test_select_with_empty @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", [], prompt: true, include_blank: true) ) end def test_select_with_html_options @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", [], { prompt: true, include_blank: true }, class: "disabled", disabled: true) ) end def test_select_with_nil @post = Post.new @post.category = "othervalue" assert_dom_equal( "", select("post", "category", [nil, "othervalue"]) ) end def test_required_select assert_dom_equal( %(), select("post", "category", %w(abe mus hest), {}, required: true) ) end def test_required_select_with_include_blank_prompt assert_dom_equal( %(), select("post", "category", %w(abe mus hest), { include_blank: "Select one" }, required: true) ) end def test_required_select_with_prompt assert_dom_equal( %(), select("post", "category", %w(abe mus hest), { prompt: "Select one" }, required: true) ) end def test_required_select_display_size_equals_to_one assert_dom_equal( %(), select("post", "category", %w(abe mus hest), {}, required: true, size: 1) ) end def test_required_select_with_display_size_bigger_than_one assert_dom_equal( %(), select("post", "category", %w(abe mus hest), {}, required: true, size: 2) ) end def test_required_select_with_multiple_option assert_dom_equal( %(), select("post", "category", %w(abe mus hest), {}, required: true, multiple: true) ) end def test_select_with_integer @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", [1], prompt: true, include_blank: true) ) end def test_list_of_lists @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", [["Number", "number"], ["Text", "text"], ["Yes/No", "boolean"]], prompt: true, include_blank: true) ) end def test_select_with_selected_value @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest ), selected: "abe") ) end def test_select_with_index_option @album = Album.new @album.id = 1 expected = "" assert_dom_equal( expected, select("album[]", "genre", %w[rap rock country], {}, index: nil) ) end def test_select_escapes_options assert_dom_equal( '', select("post", "title", "") ) end def test_select_with_selected_nil @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest ), selected: nil) ) end def test_select_with_disabled_value @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest ), disabled: "hest") ) end def test_select_not_existing_method_with_selected_value @post = Post.new assert_dom_equal( "", select("post", "locale", %w( en ru ), selected: "ru") ) end def test_select_with_prompt_and_selected_value @post = Post.new assert_dom_equal( "", select("post", "category", %w( one two ), selected: "two", prompt: true) ) end def test_select_with_disabled_array @post = Post.new @post.category = "" assert_dom_equal( "", select("post", "category", %w( abe hest ), disabled: ["hest", "abe"]) ) end def test_select_with_range @post = Post.new @post.category = 0 assert_dom_equal( "", select("post", "category", 1..3) ) end def test_collection_select @post = Post.new @post.author_name = "Babe" assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, "author_name", "author_name") ) end def test_collection_select_under_fields_for @post = Post.new @post.author_name = "Babe" output_buffer = fields_for :post, @post do |f| concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name) end assert_dom_equal( "", output_buffer ) end def test_collection_select_under_fields_for_with_index @post = Post.new @post.author_name = "Babe" output_buffer = fields_for :post, @post, index: 815 do |f| concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name) end assert_dom_equal( "", output_buffer ) end def test_collection_select_under_fields_for_with_auto_index @post = Post.new @post.author_name = "Babe" def @post.to_param; 815; end output_buffer = fields_for "post[]", @post do |f| concat f.collection_select(:author_name, dummy_posts, :author_name, :author_name) end assert_dom_equal( "", output_buffer ) end def test_collection_select_with_blank_and_style @post = Post.new @post.author_name = "Babe" assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: true }, "style" => "width: 200px") ) end def test_collection_select_with_blank_as_string_and_style @post = Post.new @post.author_name = "Babe" assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: "No Selection" }, "style" => "width: 200px") ) end def test_collection_select_with_multiple_option_appends_array_brackets_and_hidden_input @post = Post.new @post.author_name = "Babe" expected = "" # Should suffix default name with []. assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: true }, multiple: true) # Shouldn't suffix custom name with []. assert_dom_equal expected, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", { include_blank: true, name: "post[author_name][]" }, multiple: true) end def test_collection_select_with_blank_and_selected @post = Post.new @post.author_name = "Babe" assert_dom_equal( %{}, collection_select("post", "author_name", dummy_posts, "author_name", "author_name", include_blank: true, selected: "") ) end def test_collection_select_with_disabled @post = Post.new @post.author_name = "Babe" assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, "author_name", "author_name", disabled: "Cabe") ) end def test_collection_select_with_proc_for_value_method @post = Post.new assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, lambda { |p| p.author_name }, "title") ) end def test_collection_select_with_proc_for_text_method @post = Post.new assert_dom_equal( "", collection_select("post", "author_name", dummy_posts, "author_name", lambda { |p| p.title }) ) end def test_time_zone_select @firm = Firm.new("D") html = time_zone_select("firm", "time_zone") assert_dom_equal "", html end def test_time_zone_select_under_fields_for @firm = Firm.new("D") output_buffer = fields_for :firm, @firm do |f| concat f.time_zone_select(:time_zone) end assert_dom_equal( "", output_buffer ) end def test_time_zone_select_under_fields_for_with_index @firm = Firm.new("D") output_buffer = fields_for :firm, @firm, index: 305 do |f| concat f.time_zone_select(:time_zone) end assert_dom_equal( "", output_buffer ) end def test_time_zone_select_under_fields_for_with_auto_index @firm = Firm.new("D") def @firm.to_param; 305; end output_buffer = fields_for "firm[]", @firm do |f| concat f.time_zone_select(:time_zone) end assert_dom_equal( "", output_buffer ) end def test_time_zone_select_with_blank @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, include_blank: true) assert_dom_equal "", html end def test_time_zone_select_with_blank_as_string @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, include_blank: "No Zone") assert_dom_equal "", html end def test_time_zone_select_with_style @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, {}, "style" => "color: red") assert_dom_equal "", html assert_dom_equal html, time_zone_select("firm", "time_zone", nil, {}, style: "color: red") end def test_time_zone_select_with_blank_and_style @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, { include_blank: true }, "style" => "color: red") assert_dom_equal "", html assert_dom_equal html, time_zone_select("firm", "time_zone", nil, { include_blank: true }, style: "color: red") end def test_time_zone_select_with_blank_as_string_and_style @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, { include_blank: "No Zone" }, "style" => "color: red") assert_dom_equal "", html assert_dom_equal html, time_zone_select("firm", "time_zone", nil, { include_blank: "No Zone" }, style: "color: red") end def test_time_zone_select_with_priority_zones @firm = Firm.new("D") zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] html = time_zone_select("firm", "time_zone", zones) assert_dom_equal "", html end def test_time_zone_select_with_priority_zones_as_regexp @firm = Firm.new("D") @fake_timezones.each do |tz| def tz.=~(re); %(A D).include?(name) end end html = time_zone_select("firm", "time_zone", /A|D/) assert_dom_equal "", html end def test_time_zone_select_with_priority_zones_is_not_implemented_with_grep @firm = Firm.new("D") # `time_zone_select` can't be written with `grep` because Active Support # time zones don't support implicit string coercion with `to_str`. @fake_timezones.each do |tz| def tz.===(zone); raise Exception; end end html = time_zone_select("firm", "time_zone", /A|D/) assert_dom_equal "", html end def test_time_zone_select_with_default_time_zone_and_nil_value @firm = Firm.new() @firm.time_zone = nil html = time_zone_select("firm", "time_zone", nil, default: "B") assert_dom_equal "", html end def test_time_zone_select_with_default_time_zone_and_value @firm = Firm.new("D") html = time_zone_select("firm", "time_zone", nil, default: "B") assert_dom_equal "", html end def test_options_for_select_with_element_attributes assert_dom_equal( "\n\n\n", options_for_select([ [ "", { class: "bold" } ], [ "USA", { onclick: "alert('Hello World');" } ], [ "Sweden" ], "Germany" ]) ) end def test_options_for_select_with_data_element assert_dom_equal( "", options_for_select([ [ "", { data: { test: "bold" } } ] ]) ) end def test_options_for_select_with_data_element_with_special_characters assert_dom_equal( "", options_for_select([ [ "", { data: { test: "" } } ] ]) ) end def test_options_for_select_with_element_attributes_and_selection assert_dom_equal( "\n\n", options_for_select([ "", [ "USA", { class: "bold" } ], "Sweden" ], "USA") ) end def test_options_for_select_with_element_attributes_and_selection_array assert_dom_equal( "\n\n", options_for_select([ "", [ "USA", { class: "bold" } ], "Sweden" ], [ "USA", "Sweden" ]) ) end def test_options_for_select_with_special_characters assert_dom_equal( "", options_for_select([ [ "", { onclick: %(alert("")) } ] ]) ) end def test_option_html_attributes_with_no_array_element assert_equal({}, option_html_attributes("foo")) end def test_option_html_attributes_without_hash assert_equal({}, option_html_attributes([ "foo", "bar" ])) end def test_option_html_attributes_with_single_element_hash assert_equal( { class: "fancy" }, option_html_attributes([ "foo", "bar", { class: "fancy" } ]) ) end def test_option_html_attributes_with_multiple_element_hash assert_equal( { :class => "fancy", "onclick" => "alert('Hello World');" }, option_html_attributes([ "foo", "bar", { :class => "fancy", "onclick" => "alert('Hello World');" } ]) ) end def test_option_html_attributes_with_multiple_hashes assert_equal( { :class => "fancy", "onclick" => "alert('Hello World');" }, option_html_attributes([ "foo", "bar", { class: "fancy" }, { "onclick" => "alert('Hello World');" } ]) ) end def test_option_html_attributes_with_multiple_hashes_does_not_modify_them options1 = { class: "fancy" } options2 = { onclick: "alert('Hello World');" } option_html_attributes([ "foo", "bar", options1, options2 ]) assert_equal({ class: "fancy" }, options1) assert_equal({ onclick: "alert('Hello World');" }, options2) end def test_grouped_collection_select @post = Post.new @post.origin = "dk" assert_dom_equal( %Q{}, grouped_collection_select("post", "origin", dummy_continents, :countries, :continent_name, :country_id, :country_name) ) end def test_grouped_collection_select_with_selected @post = Post.new assert_dom_equal( %Q{}, grouped_collection_select("post", "origin", dummy_continents, :countries, :continent_name, :country_id, :country_name, selected: "dk") ) end def test_grouped_collection_select_with_disabled_value @post = Post.new assert_dom_equal( %Q{}, grouped_collection_select("post", "origin", dummy_continents, :countries, :continent_name, :country_id, :country_name, disabled: "dk") ) end def test_grouped_collection_select_under_fields_for @post = Post.new @post.origin = "dk" output_buffer = fields_for :post, @post do |f| concat f.grouped_collection_select("origin", dummy_continents, :countries, :continent_name, :country_id, :country_name) end assert_dom_equal( %Q{}, output_buffer ) end private def dummy_posts [ Post.new(" went home", "", "To a little house", "shh!"), Post.new("Babe went home", "Babe", "To a little house", "shh!"), Post.new("Cabe went home", "Cabe", "To a little house", "shh!") ] end def dummy_continents [ Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")]), Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")]) ] end end