# frozen_string_literal: true require 'test_helper' require_relative '../../app/helpers/shoelace/form_helper' class TranslationTest < ActionView::TestCase include ActionView::Helpers::TranslationHelper include Shoelace::FormHelper AUTOCOMPLETE_ATTRIBUTE = ActionView::VERSION::STRING >= '6.1.0' ? 'autocomplete="off"' : '' setup do I18n.backend.store_translations :en, helpers: { label: { user: { name: "Full Name" } } } view_paths = ActionController::Base.view_paths view_paths.each(&:clear_cache) @view = ::ActionView::Base.with_empty_template_cache.with_view_paths(view_paths, {}) end teardown do I18n.backend.reload! end test "Form helpers should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name) HTML end end test "Form helpers should cast symbol object names to String" do sl_form_for(User.new, as: :user, url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name) HTML end end test "Form helpers should fall back to the humanize method when there is no matching translation" do I18n.backend.reload! sl_form_for(OpenStruct.new, as: :user, url: "/") do |form| assert_dom_equal <<~HTML, form.text_field(:name_eq) HTML end end test "#color_field should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.color_field(:name) HTML end end test "#range_field should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.range_field(:name) HTML end end test "#switch_field should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.switch_field(:name) Full Name HTML end end test "#text_area should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.text_area(:name) HTML end end test "#check_box should respect label translations" do sl_form_for(User.new, url: "/") do |form| assert_dom_equal <<~HTML, form.check_box(:name) Full Name HTML end end test "#selec should respect label translationst" 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 "#collection_select should respect label translations" 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 "#grouped_collection_select should respect label translations" do users = [ OpenStruct.new( group_name: "Main maintainers", members: [ OpenStruct.new(id: 1, name: "Matz"), OpenStruct.new(id: 2, name: "Koichi Sasada"), ] ), OpenStruct.new( group_name: "Default gem maintainers", members: [OpenStruct.new(id: 3, name: "Yuki Nishijima")] ), ] sl_form_for(User.new(name: "2"), url: "/") do |form| assert_dom_equal <<~HTML, form.grouped_collection_select(:name, users, :members, :group_name, :id, :name) Main maintainers Matz Koichi Sasada Default gem maintainers Yuki Nishijima HTML end end test "#collection_radio_buttons should respect label translations" 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 end