# frozen_string_literal: true require "abstract_unit" class ActiveModelHelperTest < ActionView::TestCase tests ActionView::Helpers::ActiveModelHelper silence_warnings do Post = Struct.new(:author_name, :body, :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[:updated_at] << "bar" @post.author_name = "" @post.body = "Back to the hill and over it again!" @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_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_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