# frozen_string_literal: true require 'test_helper' require_relative '../../app/helpers/shoelace/form_helper' class FormHelperTest < ActionView::TestCase include Shoelace::FormHelper AUTOCOMPLETE_ATTRIBUTE = ActionView::VERSION::STRING >= '6.1.0' ? 'autocomplete="off"' : '' test "#sl_text_field_tag with name and value" do assert_dom_equal <<~HTML, sl_text_field_tag('name', 'Your name') HTML end test "#sl_text_field_tag with class string" do assert_dom_equal <<~HTML, sl_text_field_tag('name', 'Your name', class: "admin") HTML end test "#sl_text_field_tag with params" do assert_raises ActionController::UnfilteredParameters do sl_text_field_tag('name', 'Your name', **ActionController::Parameters.new(key: "value")) end end test "#sl_text_field_tag with disabled: true" do assert_dom_equal <<~HTML, sl_text_field_tag('name', 'Your name', disabled: true) HTML end test "#sl_submit_tag" do assert_dom_equal <<~HTML, sl_submit_tag("Save") Save HTML end test "#sl_submit_tag with onclick" do assert_dom_equal <<~HTML, sl_submit_tag("Save", onclick: "alert('hello!')", data: { disable_with: "Saving..." }) Save HTML end test "#sl_radio_button" do assert_dom_equal(<<~HTML, sl_radio_button(:user, :name, 'userid-314', checked: true) { "Yuki Nishijima" }) Yuki Nishijima HTML end test "#sl_form_with" do assert_dom_equal(<<~HTML, sl_form_with(url: "/") {})
HTML end test "#sl_form_for" do assert_dom_equal(<<~HTML, sl_form_for(User.new, url: "/") { }) HTML end test "#text_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name) HTML end end test "#text_field with a default value" do sl_form_for(User.new(name: "Yuki"), url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name) HTML end end test "#email_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.email_field(:name) HTML end end test "#number_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.number_field(:name) HTML end end test "#password_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.password_field(:name) HTML end end test "#search_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.search_field(:name) HTML end end test "#telephone_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.telephone_field(:name) HTML end end test "#phone_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.phone_field(:name) HTML end end test "#text_field with a block" do sl_form_for(User.new(name: "Yuki"), url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name) { 'slot' } slot HTML end end test "#url_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.url_field(:name) HTML end end test "#color_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.color_field(:name) HTML end end test "#range_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.range_field(:name) HTML end end test "#range_field without a label" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.range_field(:name, label: nil) HTML end end test "#switch_field" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.switch_field(:name) Name HTML end end test "#text_area" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name) HTML end end test "#text_area with a value" do sl_form_for(User.new(name: "Yuki Nishijima"), url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name) HTML end end test "#text_area with an one-off value" do sl_form_for(User.new(name: "Yuki Nishijima"), url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name, value: "Yuki") HTML end end test "#text_area without a label" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name, label: nil) HTML end end test "#text_area with a size" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name, size: "small") HTML end end test "#text_area with a block" do sl_form_for(User.new, url: "/") do |form| expected = <<~HTML
Name can not be blank.
HTML assert_dom_equal(expected, form.text_area(:name) { content_tag(:div, "Name can not be blank.", slot: "help-text") }) end end test "#check_box" do sl_form_for(User.new, url: "/") do |form| if ActionView::VERSION::STRING >= '6.1.0' assert_dom_equal <<~HTML, form.check_box(:name) Name HTML else assert_dom_equal <<~HTML, form.check_box(:name) Name HTML end end end test "#check_box with a block" do sl_form_for(User.new, url: "/") do |form| if ActionView::VERSION::STRING >= '6.1.0' assert_dom_equal <<~HTML, form.check_box(:name) { "Maintainer Name" } Maintainer Name HTML else assert_dom_equal <<~HTML, form.check_box(:name) { "Maintainer Name" } Maintainer Name HTML end end end test "#check_box without a hidden input" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.check_box(:name, include_hidden: false) { "Maintainer Name" } Maintainer Name HTML end end test "#select" do users = { "Yuki Nishijima" => 1, "Matz" => 2, "Koichi Sasada" => 3 } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users) Yuki Nishijima Matz Koichi Sasada HTML end end test "#select with a custom value" do users = { "Yuki Nishijima" => 1, "Matz" => 2, "Koichi Sasada" => 3 } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users, {}, { value: 3 }) Yuki Nishijima Matz Koichi Sasada HTML end end test "#select with custom selected and disabled values" do users = { "Yuki Nishijima" => 1, "Matz" => 2, "Koichi Sasada" => 3 } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users, selected: 3, disabled: 1) Yuki Nishijima Matz Koichi Sasada HTML end end test "#select with multiple" do users = { "Yuki Nishijima" => 1, "Matz" => 2, "Koichi Sasada" => 3 } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users, {}, { multiple: true }) Yuki Nishijima Matz Koichi Sasada HTML end end test "#select with grouped options" do users = { "Main maintainers" => [ ["Matz", 2], ["Koichi Sasada", 3] ], "Default gem maintainers" => [ ["Yuki Nishijima", 1], ] } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users) Main maintainers Matz Koichi Sasada Default gem maintainers Yuki Nishijima HTML end end test "#select with grouped options with a default value" do users = { "Main maintainers" => [ ["Matz", 2], ["Koichi Sasada", 3] ], "Default gem maintainers" => [ ["Yuki Nishijima", 1], ] } sl_form_for(User.new(name: 2), url: "/") do |form| assert_dom_equal <<~HTML, form.select(:name, users) Main maintainers Matz Koichi Sasada Default gem maintainers Yuki Nishijima HTML end end test "#collection_select" do users = { 1 => "Yuki Nishijima", 2 => "Matz", 3 => "Koichi Sasada", } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.collection_select(:name, users, :first, :last) Yuki Nishijima Matz Koichi Sasada HTML end end test "#collection_select with a defalut value" do users = { 1 => "Yuki Nishijima", 2 => "Matz", 3 => "Koichi Sasada", } sl_form_for(User.new(name: "2"), url: "/") do |form| assert_dom_equal <<~HTML, form.collection_select(:name, users, :first, :last) Yuki Nishijima Matz Koichi Sasada HTML end end test "#collection_radio_buttons" do users = { 1 => "Yuki Nishijima", 2 => "Matz", 3 => "Koichi Sasada", } sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.collection_radio_buttons(:name, users, :first, :last) Yuki Nishijima Matz Koichi Sasada HTML end end test "#collection_radio_buttons with a default value" do users = { 1 => "Yuki Nishijima", 2 => "Matz", 3 => "Koichi Sasada", } sl_form_for(User.new(name: 1), url: "/") do |form| assert_dom_equal <<~HTML, form.collection_radio_buttons(:name, users, :first, :last) Yuki Nishijima Matz Koichi Sasada HTML end end test "#submit" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.submit("Save") Save HTML end end end