From ad306ed48c5e9864738af6f96d8ad2011ac12843 Mon Sep 17 00:00:00 2001 From: Larry Reid Date: Tue, 9 Dec 2025 20:16:35 +0000 Subject: [PATCH] Add tests for namespaces --- test/bootstrap_form_test.rb | 180 +++++++++++++++++++++++++++++++++ test/bootstrap_selects_test.rb | 35 +++++++ test/namespaced_form_helper.rb | 13 +++ 3 files changed, 228 insertions(+) create mode 100644 test/namespaced_form_helper.rb diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 718f881f..423a490b 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true require_relative "test_helper" +require "namespaced_form_helper" class BootstrapFormTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper + include NamespacedFormHelper setup do setup_test_fixture @@ -881,6 +883,184 @@ def warn(message, ...) assert_equivalent_html expected, @builder.errors_on(:email, custom_class: "custom-error-class") end + + test "namespaced form adds namespace to id and label" do + expected = <<~HTML +
+ + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.text_field(:email) + assert_equivalent_html expected, namespaced_form_with.text_field(:email) + end + + test "namespaced form adds namespace to id and label with specified id:" do + skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0" + expected = <<~HTML +
+ + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.text_field(:email, id: "custom_id") + assert_equivalent_html expected, namespaced_form_with.text_field(:email, id: "custom_id") + end + + test "namespaced form adds namespace to id and label for checkboxes" do + expected = <<~HTML +
+ + + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.check_box(:terms, label: "I agree to the terms", extra: "extra arg") + assert_equivalent_html expected, namespaced_form_with.check_box(:terms, label: "I agree to the terms", extra: "extra arg") + end + + test "namespaced form adds namespace to id and label for checkboxes with specified id:" do + skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0" + expected = <<~HTML +
+ + + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.check_box( + :terms, label: "I agree to the terms", extra: "extra arg", id: "custom_id" + ) + assert_equivalent_html expected, namespaced_form_with.check_box( + :terms, label: "I agree to the terms", extra: "extra arg", id: "custom_id" + ) + end + + test "namespaced form adds namespace to id and label for radio buttons" do + expected = <<~HTML +
+ + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.radio_button( + :misc, "1", label: "This is a radio button", extra: "extra arg" + ) + assert_equivalent_html expected, namespaced_form_with.radio_button( + :misc, "1", label: "This is a radio button", extra: "extra arg" + ) + end + + test "namespaced form adds namespace to id and label for radio buttons with specified id:" do + skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0" + expected = <<~HTML +
+ + +
+ HTML + assert_equivalent_html expected, namespaced_form_for.radio_button( + :misc, "1", label: "This is a radio button", extra: "extra arg", id: "custom_id" + ) + assert_equivalent_html expected, namespaced_form_with.radio_button( + :misc, "1", label: "This is a radio button", extra: "extra arg", id: "custom_id" + ) + end + + test "namespaced form adds namespace to id and label and group for collection_checkboxes" do + collection = [Address.new(id: 1, street: "Foobar")] + expected = <<~HTML + +
+
This is a checkbox collection
+
+ + +
+ With a help! +
+ HTML + + assert_equivalent_html expected, namespaced_form_for.collection_check_boxes( + :misc, collection, :id, :street, label: "This is a checkbox collection", help: "With a help!" + ) + assert_equivalent_html expected, namespaced_form_with.collection_check_boxes( + :misc, collection, :id, :street, label: "This is a checkbox collection", help: "With a help!" + ) + end + + test "namespaced form adds namespace to id and label and group for collection_radio_buttons" do + collection = [Address.new(id: 1, street: "Foobar")] + expected = <<~HTML +
+
This is a radio button collection
+
+ + +
+ With a help! +
+ HTML + + assert_equivalent_html expected, namespaced_form_for.collection_radio_buttons( + :misc, collection, :id, :street, label: "This is a radio button collection", help: "With a help!" + ) + assert_equivalent_html expected, namespaced_form_with.collection_radio_buttons( + :misc, collection, :id, :street, label: "This is a radio button collection", help: "With a help!" + ) + end + + test "namespaced form adds namespace to inline errors" do + @user.errors.add(:misc, "error for test") + expected = <<~HTML +
+ + +
error for test
+
+ HTML + with_bootstrap_form_field_error_proc do + assert_equivalent_html(expected, namespaced_form_for.file_field(:misc)) + assert_equivalent_html(expected, namespaced_form_with.file_field(:misc)) + end + end + + test "namespaced form adds namespace to label errors" do + @user.email = nil + assert @user.invalid? + + expected = <<~HTML +
+ + +
+ HTML + with_bootstrap_form_field_error_proc do + assert_equivalent_html expected, namespaced_form_for(label_errors: true).text_field(:email) + assert_equivalent_html expected, namespaced_form_with(label_errors: true).text_field(:email) + end + end + + test "namespaced form adds namespace to errors_on" do + @user.email = nil + assert @user.invalid? + + expected = <<~HTML +
Email can't be blank, Email is too short (minimum is 5 characters)
+ HTML + assert_equivalent_html expected, namespaced_form_for.errors_on(:email) + assert_equivalent_html expected, namespaced_form_with.errors_on(:email) + end end class LegacyBootstrapFormTest < ActionView::TestCase diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 565539a6..c8a37e7e 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -1,9 +1,11 @@ # frozen_string_literal: true require_relative "test_helper" +require "namespaced_form_helper" class BootstrapSelectsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper + include NamespacedFormHelper setup :setup_test_fixture @@ -843,6 +845,39 @@ def options_range(start: 1, stop: 31, selected: nil, months: false) assert_equivalent_html expected, @builder.select(:misc, [["Apple", 1], ["Grape", 2]], floating: true) end + test "namespaced form adds namespace to id and label for selects" do + travel_to(Time.utc(2012, 2, 3, 12, 0, 0)) do + expected = <<~HTML +
+ +
+ + + + — + + : + +
+
+ HTML + assert_equivalent_html expected, namespaced_form_for.datetime_select(:misc) + assert_equivalent_html expected, namespaced_form_with.datetime_select(:misc) + end + end + + private + def blank_option '' end diff --git a/test/namespaced_form_helper.rb b/test/namespaced_form_helper.rb new file mode 100644 index 00000000..a0ebe12a --- /dev/null +++ b/test/namespaced_form_helper.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module NamespacedFormHelper + def namespaced_form_for(**args) + bootstrap_form_for(@user, namespace: "name_space", **args) { |f| @namespaced_form_for = f } + @namespaced_form_for + end + + def namespaced_form_with(**args) + bootstrap_form_with(model: @user, namespace: "name_space", **args) { |f| @namespaced_form_with = f } + @namespaced_form_with + end +end