Skip to content

Commit

Permalink
Added test case for belongs_to association error displaying.
Browse files Browse the repository at this point in the history
  • Loading branch information
dannytip committed Aug 8, 2023
1 parent 3792cc9 commit 0531dce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions demo/app/controllers/addresses_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddressesController < ApplicationController
def create
redirect_to root_path
end

def update
redirect_to root_path
end
end
1 change: 1 addition & 0 deletions demo/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Dummy::Application.routes.draw do
get "fragment" => "bootstrap#fragment", as: :fragment
resources :addresses
resources :users

root to: "bootstrap#form"
Expand Down
16 changes: 16 additions & 0 deletions test/bootstrap_fields_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ class BootstrapFieldsTest < ActionView::TestCase
assert_equivalent_html expected, bootstrap_form_for(@user) { |f| f.file_field(:misc) }
end

test "errors are correctly displayed for belongs_to assoication fields" do
@address.valid?

expected = <<~HTML
<form accept-charset="UTF-8" action="/addresses" class="new_address" id="new_address" method="post">
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
<div class="mb-3">
<label class="form-label required" for="address_user_id">User</label>
<input aria-required="true" class="form-control is-invalid" id="address_user_id" name="address[user_id]" required="required" type="text"/>
<div class="invalid-feedback">must exist</div>
</div>
</form>
HTML
assert_equivalent_html expected, bootstrap_form_for(@address) { |f| f.text_field(:user_id) }
end

test "hidden fields are supported" do
expected = <<~HTML
<input #{autocomplete_attr} id="user_misc" name="user[misc]" type="hidden" />
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class ActionView::TestCase
def setup_test_fixture
@address = Address.new(street: "Foo")
@user = User.new(email: "[email protected]", password: "secret", comments: "my comment")
@builder = BootstrapForm::FormBuilder.new(:user, @user, self, {})
@horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self,
Expand Down

0 comments on commit 0531dce

Please sign in to comment.