|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require_relative "test_helper" |
| 4 | +require "namespaced_form_helper" |
4 | 5 |
|
5 | 6 | class BootstrapFormTest < ActionView::TestCase |
6 | 7 | include BootstrapForm::ActionViewExtensions::FormHelper |
| 8 | + include NamespacedFormHelper |
7 | 9 |
|
8 | 10 | setup do |
9 | 11 | setup_test_fixture |
@@ -881,6 +883,184 @@ def warn(message, ...) |
881 | 883 |
|
882 | 884 | assert_equivalent_html expected, @builder.errors_on(:email, custom_class: "custom-error-class") |
883 | 885 | 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 |
884 | 1064 | end |
885 | 1065 |
|
886 | 1066 | class LegacyBootstrapFormTest < ActionView::TestCase |
|
0 commit comments