GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — bootstrap_form/test/bootstrap_fields_test.rb at bootstrap-4 · bootstrap-ruby/bootstrap_form Expand file tree/ Copy pathbootstrap_fields_test.rbMore file actions 442 lines (396 loc) · 16.6 KB / Copy pathbootstrap_fields_test.rbFile metadata and controls442 lines (396 loc) · 16.6 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 require_relative "./test_helper" class BootstrapFieldsTest < ActionView::TestCase include BootstrapForm::ActionViewExtensions::FormHelper setup :setup_test_fixture test "color fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="color" value="#000000" /> </div> HTML assert_equivalent_xml expected, @builder.color_field(:misc) end test "date fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="date" /> </div> HTML assert_equivalent_xml expected, @builder.date_field(:misc) end test "date time fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="datetime" /> </div> HTML assert_equivalent_xml expected, @builder.datetime_field(:misc) end test "date time local fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="datetime-local" /> </div> HTML assert_equivalent_xml expected, @builder.datetime_local_field(:misc) end test "email fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="email" /> </div> HTML assert_equivalent_xml expected, @builder.email_field(:misc) end test "file fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <div class="custom-file"> <input class="custom-file-input" id="user_misc" name="user[misc]" type="file" /> <label class="custom-file-label" for="user_misc">Choose file</label> </div> </div> HTML assert_equivalent_xml expected, @builder.file_field(:misc) end test "file field placeholder can be customized" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <div class="custom-file"> <input class="custom-file-input" id="user_misc" name="user[misc]" type="file" /> <label class="custom-file-label" for="user_misc">Pick a file</label> </div> </div> HTML assert_equivalent_xml expected, @builder.file_field(:misc, placeholder: "Pick a file") end if ::Rails::VERSION::STRING > "5.1" test "file field placeholder has appropriate `for` attribute when used in form_with" do expected = <<~HTML <div class="form-group"> <label for="custom-id">Misc</label> <div class="custom-file"> <input class="custom-file-input" id="custom-id" name="user[misc]" type="file" /> <label class="custom-file-label" for="custom-id">Choose file</label> </div> </div> HTML assert_equivalent_xml expected, form_with_builder.file_field(:misc, id: "custom-id") end end test "file fields are wrapped correctly with error" do @user.errors.add(:misc, "error for test") expected = <<~HTML <form accept-charset="UTF-8" action="/users" class="new_user" enctype="multipart/form-data" id="new_user" method="post" role="form"> #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} <div class="form-group"> <label for="user_misc">Misc</label> <div class="custom-file"> <input class="custom-file-input is-invalid" id="user_misc" name="user[misc]" type="file" /> <label class="custom-file-label" for="user_misc">Choose file</label> <div class="invalid-feedback">error for test</div> </div> </div> </form> HTML assert_equivalent_xml expected, bootstrap_form_for(@user) { |f| f.file_field(:misc) } end test "hidden fields are supported" do expected = '<input id="user_misc" name="user[misc]" type="hidden" />' assert_equivalent_xml expected, @builder.hidden_field(:misc) end test "month local fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="month" /> </div> HTML assert_equivalent_xml expected, @builder.month_field(:misc) end test "number fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="number" /> </div> HTML assert_equivalent_xml expected, @builder.number_field(:misc) end test "password fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_password">Password</label> <input class="form-control" id="user_password" name="user[password]" type="password" /> <small class="form-text text-muted">A good password should be at least six characters long</small> </div> HTML assert_equivalent_xml expected, @builder.password_field(:password) end test "phone/telephone fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="tel" /> </div> HTML assert_equivalent_xml expected, @builder.phone_field(:misc) assert_equivalent_xml expected, @builder.telephone_field(:misc) end test "range fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="range" /> </div> HTML assert_equivalent_xml expected, @builder.range_field(:misc) end test "search fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="search" /> </div> HTML assert_equivalent_xml expected, @builder.search_field(:misc) end test "text areas are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_comments">Comments</label> <textarea class="form-control" id="user_comments" name="user[comments]">\nmy comment</textarea> </div> HTML assert_equivalent_xml expected, @builder.text_area(:comments) end if ::Rails::VERSION::STRING > "5.1" && ::Rails::VERSION::STRING < "5.2" test "text areas are wrapped correctly form_with Rails 5.1" do expected = <<~HTML <div class="form-group"> <label for="user_comments">Comments</label> <textarea class="form-control" name="user[comments]">\nmy comment</textarea> </div> HTML assert_equivalent_xml expected, form_with_builder.text_area(:comments) end end if ::Rails::VERSION::STRING > "5.2" test "text areas are wrapped correctly form_with Rails 5.2+" do expected = <<~HTML <div class="form-group"> <label for="user_comments">Comments</label> <textarea class="form-control" id="user_comments" name="user[comments]">\nmy comment</textarea> </div> HTML assert_equivalent_xml expected, form_with_builder.text_area(:comments) end end test "text fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label class="required" for="user_email">Email</label> <input aria-required="true" class="form-control" id="user_email" name="user[email]" required="required" type="text" value="[email protected]" /> </div> HTML assert_equivalent_xml expected, @builder.text_field(:email) end test "text fields are wrapped correctly when horizontal and form-row given" do expected = <<~HTML <div class="form-group form-row"> <label class="col-form-label col-sm-2 required" for="user_email">Email</label> <div class="col-sm-10"> <input aria-required="true" class="form-control" id="user_email" name="user[email]" required="required" type="text" value="[email protected]" /> </div> </div> HTML assert_equivalent_xml expected, @horizontal_builder.text_field(:email, wrapper_class: "form-row") assert_equivalent_xml expected, @horizontal_builder.text_field(:email, wrapper: { class: "form-row" }) end test "field 'id' attribute is used to specify label 'for' attribute" do expected = <<~HTML <div class="form-group"> <label class="required" for="custom_id">Email</label> <input aria-required="true" class="form-control" id="custom_id" name="user[email]" required="required" type="text" value="[email protected]" /> </div> HTML assert_equivalent_xml expected, @builder.text_field(:email, id: :custom_id) end test "time fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="time" /> </div> HTML assert_equivalent_xml expected, @builder.time_field(:misc) end test "url fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="url" /> </div> HTML assert_equivalent_xml expected, @builder.url_field(:misc) end test "check_box fields are wrapped correctly" do expected = <<~HTML <div class="form-check"> <input name="user[misc]" type="hidden" value="0"/> <input class="form-check-input" id="user_misc" name="user[misc]" type="checkbox" value="1"/> <label class="form-check-label" for="user_misc">Misc</label> </div> HTML assert_equivalent_xml expected, @builder.check_box(:misc) end test "custom check_box fields are wrapped correctly" do expected = <<~HTML <div class="custom-control custom-checkbox"> <input name="user[misc]" type="hidden" value="0"/> <input class="custom-control-input" id="user_misc" name="user[misc]" type="checkbox" value="1"/> <label class="custom-control-label" for="user_misc">Misc</label> </div> HTML assert_equivalent_xml expected, @builder.check_box(:misc, custom: true) end test "switch-style check_box fields are wrapped correctly" do expected = <<~HTML <div class="custom-control custom-switch"> <input name="user[misc]" type="hidden" value="0"/> <input class="custom-control-input" id="user_misc" name="user[misc]" type="checkbox" value="1"/> <label class="custom-control-label" for="user_misc">Misc</label> </div> HTML assert_equivalent_xml expected, @builder.check_box(:misc, custom: :switch) end test "week fields are wrapped correctly" do expected = <<~HTML <div class="form-group"> <label for="user_misc">Misc</label> <input class="form-control" id="user_misc" name="user[misc]" type="week" /> </div> HTML assert_equivalent_xml expected, @builder.week_field(:misc) end test "bootstrap_form_for helper works for associations" do @user.address = Address.new(street: "123 Main Street") output = bootstrap_form_for(@user) do |f| f.fields_for :address do |af| af.text_field(:street) end end expected = <<~HTML <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form"> #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} <div class="form-group"> <label for="user_address_attributes_street">Street</label> <input class="form-control" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="123 Main Street" /> </div> </form> HTML assert_equivalent_xml expected, output end test "bootstrap_form_for helper works for serialized hash attributes" do @user.preferences = { favorite_color: "cerulean" } output = bootstrap_form_for(@user) do |f| f.fields_for :preferences do |builder| builder.text_field :favorite_color, value: @user.preferences[:favorite_color] end end expected = <<~HTML <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form"> #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} <div class="form-group"> <label for="user_preferences_favorite_color">Favorite color</label> <input class="form-control" id="user_preferences_favorite_color" name="user[preferences][favorite_color]" type="text" value="cerulean" /> </div> </form> HTML assert_equivalent_xml expected, output end test "fields_for correctly passes horizontal style from parent builder" do @user.address = Address.new(street: "123 Main Street") output = bootstrap_form_for(@user, layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10") do |f| f.fields_for :address do |af| af.text_field(:street) end end expected = <<~HTML <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post" role="form"> #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} <div class="form-group row"> <label class="col-form-label col-sm-2" for="user_address_attributes_street">Street</label> <div class="col-sm-10"> <input class="form-control" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="123 Main Street" /> </div> </div> </form> HTML assert_equivalent_xml expected, output end test "fields_for correctly passes inline style from parent builder" do @user.address = Address.new(street: "123 Main Street") # NOTE: This test works with even if you use `fields_for_without_bootstrap` output = bootstrap_form_for(@user, layout: :inline) do |f| f.fields_for :address do |af| af.text_field(:street) end end expected = <<~HTML <form accept-charset="UTF-8" action="/users" class="new_user form-inline" id="new_user" method="post" role="form"> #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} <div class="form-group"> <label class="mr-sm-2" for="user_address_attributes_street">Street</label> <input class="form-control" id="user_address_attributes_street" name="user[address_attributes][street]" type="text" value="123 Main Street" /> </div> </form> HTML assert_equivalent_xml expected, output end if ::Rails::VERSION::STRING >= "5.1" test "fields correctly uses options from parent builder" do @user.address = Address.new(street: "123 Main Street") bootstrap_form_with(model: @user, control_col: "control-style", inline_errors: false, label_col: "label-style", label_errors: true, layout: :inline) do |f| f.fields :address do |af| af.text_field(:street) assert_equal "control-style", af.control_col assert_equal false, af.inline_errors assert_equal "label-style", af.label_col assert_equal true, af.label_errors assert_equal :inline, af.layout end end end end test "fields_for_without_bootstrap does not use options from parent builder" do @user.address = Address.new(street: "123 Main Street") bootstrap_form_for(@user, control_col: "control-style", inline_errors: false, label_col: "label-style", label_errors: true, layout: :inline) do |f| f.fields_for_without_bootstrap :address do |af| af.text_field(:street) assert_not_equal "control-style", af.control_col assert_not_equal false, af.inline_errors assert_not_equal "label-style", af.label_col assert_not_equal true, af.label_errors assert_not_equal :inline, af.layout end end end end |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |