Skip to content

Commit 0677c79

Browse files
authored
Merge pull request #784 from lcreid/533-namespaced-id-for
Test That `namespace` Works
2 parents b5e127a + ad306ed commit 0677c79

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

test/bootstrap_form_test.rb

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# frozen_string_literal: true
22

33
require_relative "test_helper"
4+
require "namespaced_form_helper"
45

56
class BootstrapFormTest < ActionView::TestCase
67
include BootstrapForm::ActionViewExtensions::FormHelper
8+
include NamespacedFormHelper
79

810
setup do
911
setup_test_fixture
@@ -881,6 +883,184 @@ def warn(message, ...)
881883

882884
assert_equivalent_html expected, @builder.errors_on(:email, custom_class: "custom-error-class")
883885
end
886+
887+
test "namespaced form adds namespace to id and label" do
888+
expected = <<~HTML
889+
<div class="mb-3">
890+
<label class="form-label required" for="name_space_user_email">Email</label>
891+
<input required="required" class="form-control" id="name_space_user_email" name="user[email]" type="text" value="steve@example.com" />
892+
</div>
893+
HTML
894+
assert_equivalent_html expected, namespaced_form_for.text_field(:email)
895+
assert_equivalent_html expected, namespaced_form_with.text_field(:email)
896+
end
897+
898+
test "namespaced form adds namespace to id and label with specified id:" do
899+
skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0"
900+
expected = <<~HTML
901+
<div class="mb-3">
902+
<label class="form-label required" for="name_space_custom_id">Email</label>
903+
<input required="required" class="form-control" id="name_space_custom_id" name="user[email]" type="text" value="steve@example.com" />
904+
</div>
905+
HTML
906+
assert_equivalent_html expected, namespaced_form_for.text_field(:email, id: "custom_id")
907+
assert_equivalent_html expected, namespaced_form_with.text_field(:email, id: "custom_id")
908+
end
909+
910+
test "namespaced form adds namespace to id and label for checkboxes" do
911+
expected = <<~HTML
912+
<div class="form-check mb-3">
913+
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
914+
<input class="form-check-input" extra="extra arg" id="name_space_user_terms" name="user[terms]" type="checkbox" value="1" />
915+
<label class="form-check-label" for="name_space_user_terms">
916+
I agree to the terms
917+
</label>
918+
</div>
919+
HTML
920+
assert_equivalent_html expected, namespaced_form_for.check_box(:terms, label: "I agree to the terms", extra: "extra arg")
921+
assert_equivalent_html expected, namespaced_form_with.check_box(:terms, label: "I agree to the terms", extra: "extra arg")
922+
end
923+
924+
test "namespaced form adds namespace to id and label for checkboxes with specified id:" do
925+
skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0"
926+
expected = <<~HTML
927+
<div class="form-check mb-3">
928+
<input #{autocomplete_attr} name="user[terms]" type="hidden" value="0" />
929+
<input class="form-check-input" extra="extra arg" id="name_space_custom_id" name="user[terms]" type="checkbox" value="1" />
930+
<label class="form-check-label" for="name_space_custom_id">
931+
I agree to the terms
932+
</label>
933+
</div>
934+
HTML
935+
assert_equivalent_html expected, namespaced_form_for.check_box(
936+
:terms, label: "I agree to the terms", extra: "extra arg", id: "custom_id"
937+
)
938+
assert_equivalent_html expected, namespaced_form_with.check_box(
939+
:terms, label: "I agree to the terms", extra: "extra arg", id: "custom_id"
940+
)
941+
end
942+
943+
test "namespaced form adds namespace to id and label for radio buttons" do
944+
expected = <<~HTML
945+
<div class="form-check">
946+
<input class="form-check-input" extra="extra arg" id="name_space_user_misc_1" name="user[misc]" type="radio" value="1" />
947+
<label class="form-check-label" for="name_space_user_misc_1">
948+
This is a radio button
949+
</label>
950+
</div>
951+
HTML
952+
assert_equivalent_html expected, namespaced_form_for.radio_button(
953+
:misc, "1", label: "This is a radio button", extra: "extra arg"
954+
)
955+
assert_equivalent_html expected, namespaced_form_with.radio_button(
956+
:misc, "1", label: "This is a radio button", extra: "extra arg"
957+
)
958+
end
959+
960+
test "namespaced form adds namespace to id and label for radio buttons with specified id:" do
961+
skip "Ignore Rails 7 bug" if Rails::VERSION::STRING < "8.0.0"
962+
expected = <<~HTML
963+
<div class="form-check">
964+
<input class="form-check-input" extra="extra arg" id="name_space_custom_id" name="user[misc]" type="radio" value="1" />
965+
<label class="form-check-label" for="name_space_custom_id">
966+
This is a radio button
967+
</label>
968+
</div>
969+
HTML
970+
assert_equivalent_html expected, namespaced_form_for.radio_button(
971+
:misc, "1", label: "This is a radio button", extra: "extra arg", id: "custom_id"
972+
)
973+
assert_equivalent_html expected, namespaced_form_with.radio_button(
974+
:misc, "1", label: "This is a radio button", extra: "extra arg", id: "custom_id"
975+
)
976+
end
977+
978+
test "namespaced form adds namespace to id and label and group for collection_checkboxes" do
979+
collection = [Address.new(id: 1, street: "Foobar")]
980+
expected = <<~HTML
981+
<input #{autocomplete_attr_55336} id="name_space_user_misc" name="user[misc][]" type="hidden" value="" />
982+
<div role="group" aria-labelledby="name_space_user_misc" class="mb-3">
983+
<div id="name_space_user_misc" class="form-label">This is a checkbox collection</div>
984+
<div class="form-check">
985+
<input class="form-check-input" id="name_space_user_misc_1" name="user[misc][]" type="checkbox" value="1" />
986+
<label class="form-check-label" for="name_space_user_misc_1">Foobar</label>
987+
</div>
988+
<small class="form-text text-muted">With a help!</small>
989+
</div>
990+
HTML
991+
992+
assert_equivalent_html expected, namespaced_form_for.collection_check_boxes(
993+
:misc, collection, :id, :street, label: "This is a checkbox collection", help: "With a help!"
994+
)
995+
assert_equivalent_html expected, namespaced_form_with.collection_check_boxes(
996+
:misc, collection, :id, :street, label: "This is a checkbox collection", help: "With a help!"
997+
)
998+
end
999+
1000+
test "namespaced form adds namespace to id and label and group for collection_radio_buttons" do
1001+
collection = [Address.new(id: 1, street: "Foobar")]
1002+
expected = <<~HTML
1003+
<div role="group" aria-labelledby="name_space_user_misc" class="mb-3">
1004+
<div id="name_space_user_misc" class="form-label">This is a radio button collection</div>
1005+
<div class="form-check">
1006+
<input class="form-check-input" id="name_space_user_misc_1" name="user[misc]" type="radio" value="1" />
1007+
<label class="form-check-label" for="name_space_user_misc_1">
1008+
Foobar
1009+
</label>
1010+
</div>
1011+
<small class="form-text text-muted">With a help!</small>
1012+
</div>
1013+
HTML
1014+
1015+
assert_equivalent_html expected, namespaced_form_for.collection_radio_buttons(
1016+
:misc, collection, :id, :street, label: "This is a radio button collection", help: "With a help!"
1017+
)
1018+
assert_equivalent_html expected, namespaced_form_with.collection_radio_buttons(
1019+
:misc, collection, :id, :street, label: "This is a radio button collection", help: "With a help!"
1020+
)
1021+
end
1022+
1023+
test "namespaced form adds namespace to inline errors" do
1024+
@user.errors.add(:misc, "error for test")
1025+
expected = <<~HTML
1026+
<div class="mb-3">
1027+
<label class="form-label" for="name_space_user_misc">Misc</label>
1028+
<input class="form-control is-invalid" id="name_space_user_misc" aria-describedby="name_space_user_misc_feedback" name="user[misc]" type="file"/>
1029+
<div class="invalid-feedback" id="name_space_user_misc_feedback">error for test</div>
1030+
</div>
1031+
HTML
1032+
with_bootstrap_form_field_error_proc do
1033+
assert_equivalent_html(expected, namespaced_form_for.file_field(:misc))
1034+
assert_equivalent_html(expected, namespaced_form_with.file_field(:misc))
1035+
end
1036+
end
1037+
1038+
test "namespaced form adds namespace to label errors" do
1039+
@user.email = nil
1040+
assert @user.invalid?
1041+
1042+
expected = <<~HTML
1043+
<div class="mb-3">
1044+
<label class="form-label required text-danger" for="name_space_user_email" id="name_space_user_email_feedback">Email can't be blank, is too short (minimum is 5 characters)</label>
1045+
<input required="required" class="form-control is-invalid" id="name_space_user_email" aria-describedby="name_space_user_email_feedback" name="user[email]" type="text" />
1046+
</div>
1047+
HTML
1048+
with_bootstrap_form_field_error_proc do
1049+
assert_equivalent_html expected, namespaced_form_for(label_errors: true).text_field(:email)
1050+
assert_equivalent_html expected, namespaced_form_with(label_errors: true).text_field(:email)
1051+
end
1052+
end
1053+
1054+
test "namespaced form adds namespace to errors_on" do
1055+
@user.email = nil
1056+
assert @user.invalid?
1057+
1058+
expected = <<~HTML
1059+
<div class="invalid-feedback" id="name_space_user_email_feedback">Email can't be blank, Email is too short (minimum is 5 characters)</div>
1060+
HTML
1061+
assert_equivalent_html expected, namespaced_form_for.errors_on(:email)
1062+
assert_equivalent_html expected, namespaced_form_with.errors_on(:email)
1063+
end
8841064
end
8851065

8861066
class LegacyBootstrapFormTest < ActionView::TestCase

test/bootstrap_selects_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# frozen_string_literal: true
22

33
require_relative "test_helper"
4+
require "namespaced_form_helper"
45

56
class BootstrapSelectsTest < ActionView::TestCase
67
include BootstrapForm::ActionViewExtensions::FormHelper
8+
include NamespacedFormHelper
79

810
setup :setup_test_fixture
911

@@ -843,6 +845,39 @@ def options_range(start: 1, stop: 31, selected: nil, months: false)
843845
assert_equivalent_html expected, @builder.select(:misc, [["Apple", 1], ["Grape", 2]], floating: true)
844846
end
845847

848+
test "namespaced form adds namespace to id and label for selects" do
849+
travel_to(Time.utc(2012, 2, 3, 12, 0, 0)) do
850+
expected = <<~HTML
851+
<div class="mb-3">
852+
<label class="form-label" for="name_space_user_misc">Misc</label>
853+
<div class="rails-bootstrap-forms-datetime-select">
854+
<select class="form-select" id="name_space_user_misc_1i" name="user[misc(1i)]">
855+
#{options_range(start: 2007, stop: 2017, selected: 2012)}
856+
</select>
857+
<select class="form-select" id="name_space_user_misc_2i" name="user[misc(2i)]">
858+
#{options_range(start: 1, stop: 12, selected: 2, months: true)}
859+
</select>
860+
<select class="form-select" id="name_space_user_misc_3i" name="user[misc(3i)]">
861+
#{options_range(start: 1, stop: 31, selected: 3)}
862+
</select>
863+
&mdash;
864+
<select class="form-select" id="name_space_user_misc_4i" name="user[misc(4i)]">
865+
#{options_range(start: '00', stop: '23', selected: '12')}
866+
</select>
867+
:
868+
<select class="form-select" id="name_space_user_misc_5i" name="user[misc(5i)]">
869+
#{options_range(start: '00', stop: '59', selected: '00')}
870+
</select>
871+
</div>
872+
</div>
873+
HTML
874+
assert_equivalent_html expected, namespaced_form_for.datetime_select(:misc)
875+
assert_equivalent_html expected, namespaced_form_with.datetime_select(:misc)
876+
end
877+
end
878+
879+
private
880+
846881
def blank_option
847882
'<option label=" " value=""></option>'
848883
end

test/namespaced_form_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module NamespacedFormHelper
4+
def namespaced_form_for(**args)
5+
bootstrap_form_for(@user, namespace: "name_space", **args) { |f| @namespaced_form_for = f }
6+
@namespaced_form_for
7+
end
8+
9+
def namespaced_form_with(**args)
10+
bootstrap_form_with(model: @user, namespace: "name_space", **args) { |f| @namespaced_form_with = f }
11+
@namespaced_form_with
12+
end
13+
end

0 commit comments

Comments
 (0)