diff --git a/app/presenters/hotwire_combobox/component.rb b/app/presenters/hotwire_combobox/component.rb index 8a86814..8a53a98 100644 --- a/app/presenters/hotwire_combobox/component.rb +++ b/app/presenters/hotwire_combobox/component.rb @@ -204,7 +204,7 @@ def hidden_field_data end def hidden_field_value - form&.object&.public_send(name) || value + form&.object&.try(name) || value end diff --git a/test/application_view_test_case.rb b/test/application_view_test_case.rb index 25a726a..106fb0c 100644 --- a/test/application_view_test_case.rb +++ b/test/application_view_test_case.rb @@ -10,6 +10,21 @@ def assert_attrs(tag, tag_name: :input, **attrs) assert_match /<#{tag_name}.* #{attrs}/, tag end + def mock_form(form_object: OpenStruct.new) + form = OpenStruct.new + form.define_singleton_method(:field_id) { |name| "#{name}_id" } + form.define_singleton_method(:field_name) { |name| name } + form.define_singleton_method(:object) { form_object } + form_object.define_singleton_method(:public_send) do |method_name, *args| + if form_object.respond_to?(method_name) + super(method_name, *args) + else + raise NoMethodError, "undefined method `#{method_name}` for #{form_object.inspect}" + end + end + form + end + private def escape_specials(value) special_characters = Regexp.union "[]".chars diff --git a/test/presenters/hotwire_combobox/component_test.rb b/test/presenters/hotwire_combobox/component_test.rb new file mode 100644 index 0000000..ebe4ad4 --- /dev/null +++ b/test/presenters/hotwire_combobox/component_test.rb @@ -0,0 +1,49 @@ +require "test_helper" + +class HotwireCombobox::ComponentTest < ApplicationViewTestCase + setup do + @view = ActionView::Base.new(ActionController::Base.view_paths, {}, ActionController::Base.new) + end + + test "hidden_field_attrs returns value from form object if available" do + existent_field_on_model = "Peter" + field_name = :name + component = HotwireCombobox::Component.new @view, field_name, + form: mock_form(form_object: OpenStruct.new("#{field_name}": existent_field_on_model)) + + assert_equal component.hidden_field_attrs, { id: "#{field_name}_id-hw-hidden-field", + name: :name, + data: { hw_combobox_target: "hiddenField" }, + value: existent_field_on_model } + end + + test "hidden_field_attrs returns component value if not available in form object" do + field_name = "options[name]" + specified_value = "Peter" + component = HotwireCombobox::Component.new( + @view, field_name, + form: mock_form, + value: specified_value + ) + + assert_equal component.hidden_field_attrs, { id: "#{field_name}_id-hw-hidden-field", + name: field_name, + data: { hw_combobox_target: "hiddenField" }, + value: specified_value } + + end + + test "hidden_field_attrs returns nil for value if neither form object nor component value is available" do + field_name = "options[name]" + component = HotwireCombobox::Component.new( + @view, field_name, + form: mock_form, + ) + + assert_equal component.hidden_field_attrs, { id: "#{field_name}_id-hw-hidden-field", + name: field_name, + data: { hw_combobox_target: "hiddenField" }, + value: nil } + + end +end \ No newline at end of file