require 'test_helper' module ExpressAdmin class SmartFormTest < ActiveSupport::TestCase def resource_assigns {resource: Widget.new, collection: Widget.all} end def helpers view = mock_action_view(resource_assigns) class << view def widget_path(widget_id) "/widgets/#{widget_id.to_param}" end alias resource_path widget_path def widgets_path "/widgets" end alias collection_path widgets_path def admin_widgets_path "/admin/widgets" end end view end def widget_form(*args) arbre { smart_form(:widget, *args) } end test "renders a form correct id" do assert_match /form.*id="widget"/, widget_form end test "uses inherited_resources path helpers to set correct action" do action_attrib = 'action="/widgets"' assert_match action_attrib, widget_form end test "action path can be overridden" do custom_action = '/something_custom' assert_match /action="#{custom_action}"/, widget_form(action: custom_action) end test "category field is a select" do assert_match // assert 7, matches.length assert_match '', widget_form end test "fields are wrapped in a div" do assert_match '
', widget_form end test 'path prefix is provided' do action_attrib = 'action="/admin/widgets"' assert_match action_attrib, widget_form(path_prefix: 'admin') end test "options for select come from the related collection" do assert_match /Toys<\/option>/, widget_form assert_match /Tools<\/option>/, widget_form end test "options for select work when a namespace is specified" do assert_match /Toys<\/option>/, widget_form(namespace: "example_engine") assert_match /Tools<\/option>/, widget_form(namespace: "example_engine") end test "multiple select is used for has_many_through associations" do assert_match /select.*multiple="multiple".*name="widget\[tag_ids\]\[\]"/, widget_form end test "excluded fields are excluded" do refute_match 'column3', widget_form(exclude: [:column3]) end test "virtual attributes may be included" do assert_match 'password', widget_form(virtual: [:password]) end end end