# frozen_string_literal: true require "abstract_unit" class ActiveModelHelperTest < ActionView::TestCase tests ActionView::Helpers::ActiveModelHelper silence_warnings do Post = Struct.new(:author_name, :body, :category, :published, :updated_at) do include ActiveModel::Conversion include ActiveModel::Validations def persisted? false end end end def setup super @post = Post.new @post.errors[:author_name] << "can't be empty" @post.errors[:body] << "foo" @post.errors[:category] << "must exist" @post.errors[:published] << "must be accepted" @post.errors[:updated_at] << "bar" @post.author_name = "" @post.body = "Back to the hill and over it again!" @post.category = "rails" @post.published = false @post.updated_at = Date.new(2004, 6, 15) end def test_text_area_with_errors assert_dom_equal( %(
), text_area("post", "body") ) end def test_text_field_with_errors assert_dom_equal( %(
), text_field("post", "author_name") ) end def test_select_with_errors assert_dom_equal( %(
), select("post", "author_name", [:a, :b]) ) end def test_select_with_errors_and_blank_option expected_dom = %(
) assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], include_blank: "Choose one...")) assert_dom_equal(expected_dom, select("post", "author_name", [:a, :b], prompt: "Choose one...")) end def test_select_grouped_options_with_errors grouped_options = [ ["A", [["A1"], ["A2"]]], ["B", [["B1"], ["B2"]]], ] assert_dom_equal( %(
), select("post", "category", grouped_options) ) end def test_collection_select_with_errors assert_dom_equal( %(
), collection_select("post", "author_name", [:a, :b], :to_s, :to_s) ) end def test_date_select_with_errors assert_dom_equal( %(
\n\n\n
), date_select("post", "updated_at", discard_month: true, discard_day: true, start_year: 2004, end_year: 2005) ) end def test_datetime_select_with_errors assert_dom_equal( %(
\n\n\n\n : \n
), datetime_select("post", "updated_at", discard_year: true, discard_month: true, discard_day: true, minute_step: 60) ) end def test_time_select_with_errors assert_dom_equal( %(
\n\n\n\n : \n
), time_select("post", "updated_at", minute_step: 60) ) end def test_label_with_errors assert_dom_equal( %(
), label("post", "body") ) end def test_check_box_with_errors assert_dom_equal( %(
), check_box("post", "published") ) end def test_check_boxes_with_errors assert_dom_equal( %(
), check_box("post", "published") + check_box("post", "published") ) end def test_radio_button_with_errors assert_dom_equal( %(
), radio_button("post", "category", "rails") ) end def test_radio_buttons_with_errors assert_dom_equal( %(
), radio_button("post", "category", "rails") + radio_button("post", "category", "java") ) end def test_collection_check_boxes_with_errors assert_dom_equal( %(
), collection_check_boxes("post", "category", [:ruby, :java], :to_s, :to_s) ) end def test_collection_radio_buttons_with_errors assert_dom_equal( %(
), collection_radio_buttons("post", "category", [:ruby, :java], :to_s, :to_s) ) end def test_hidden_field_does_not_render_errors assert_dom_equal( %(), hidden_field("post", "author_name") ) end def test_field_error_proc old_proc = ActionView::Base.field_error_proc ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| raw(%(
#{html_tag} #{[instance.error_message].join(', ')}
)) end assert_dom_equal( %(
can't be empty
), text_field("post", "author_name") ) ensure ActionView::Base.field_error_proc = old_proc if old_proc end end