|
| 1 | +require_relative "test_helper" |
| 2 | + |
| 3 | +class BootstrapFieldsForTest < ActionView::TestCase |
| 4 | + include BootstrapForm::ActionViewExtensions::FormHelper |
| 5 | + |
| 6 | + setup :setup_test_fixture |
| 7 | + |
| 8 | + test "bootstrap_fields_for helper works for associations" do |
| 9 | + @user.address = Address.new(street: "123 Main Street") |
| 10 | + |
| 11 | + output = bootstrap_form_for(@user) do |_f| |
| 12 | + bootstrap_fields_for @user.address do |af| |
| 13 | + af.text_field(:street) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + expected = <<~HTML |
| 18 | + <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"> |
| 19 | + #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} |
| 20 | + <div class="mb-3"> |
| 21 | + <label class="form-label" for="address_street">Street</label> |
| 22 | + <input class="form-control" id="address_street" name="address[street]" type="text" value="123 Main Street" /> |
| 23 | + </div> |
| 24 | + </form> |
| 25 | + HTML |
| 26 | + assert_equivalent_html expected, output |
| 27 | + end |
| 28 | + |
| 29 | + test "bootstrap_form_for helper works for serialized hash attributes" do |
| 30 | + @user.preferences = { "favorite_color" => "cerulean" } |
| 31 | + |
| 32 | + output = bootstrap_form_for(@user) do |_f| |
| 33 | + bootstrap_fields_for :preferences do |builder| |
| 34 | + builder.text_field :favorite_color, value: @user.preferences["favorite_color"] |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + expected = <<~HTML |
| 39 | + <form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post"> |
| 40 | + #{'<input name="utf8" type="hidden" value="✓"/>' unless ::Rails::VERSION::STRING >= '6'} |
| 41 | + <div class="mb-3"> |
| 42 | + <label class="form-label" for="preferences_favorite_color">Favorite color</label> |
| 43 | + <input class="form-control" id="preferences_favorite_color" name="preferences[favorite_color]" type="text" value="cerulean" /> |
| 44 | + </div> |
| 45 | + </form> |
| 46 | + HTML |
| 47 | + assert_equivalent_html expected, output |
| 48 | + end |
| 49 | +end |
0 commit comments