diff --git a/app/helpers/polaris/form_builder.rb b/app/helpers/polaris/form_builder.rb index 49ef3798..89b3ab2e 100644 --- a/app/helpers/polaris/form_builder.rb +++ b/app/helpers/polaris/form_builder.rb @@ -1,5 +1,7 @@ module Polaris class FormBuilder < ActionView::Helpers::FormBuilder + include ActionView::Helpers::OutputSafetyHelper + attr_reader :template delegate :render, :pluralize, to: :template diff --git a/test/helpers/polaris/form_builder_test.rb b/test/helpers/polaris/form_builder_test.rb new file mode 100644 index 00000000..1e627f13 --- /dev/null +++ b/test/helpers/polaris/form_builder_test.rb @@ -0,0 +1,92 @@ +require "test_helper" + +class Polaris::ViewHelperTest < ActionView::TestCase + include Polaris::ComponentTestHelpers + + class Product + include ActiveModel::Model + attr_accessor :title, :status, :accept, :access + end + + setup do + @product = Product.new + @builder = Polaris::FormBuilder.new(:product, @product, self, {}) + end + + test "#errors_summary" do + @product.errors.add(:base, "Base Error") + + @rendered_component = @builder.errors_summary + + assert_selector ".Polaris-Banner--statusCritical" do + assert_text "1 error with this product" + assert_selector ".Polaris-Banner__Content" do + assert_selector "li", text: "Base Error" + end + end + end + + test "#error_for" do + @product.errors.add(:title, "Error") + + assert_equal "Title Error", @builder.error_for(:title) + end + + test "#polaris_inline_error_for" do + @product.errors.add(:title, "Error") + + @rendered_component = @builder.polaris_inline_error_for(:title) + + assert_selector ".Polaris-InlineError" do + assert_selector ".Polaris-InlineError__Icon" + assert_text "Title Error" + end + end + + test "#polaris_text_field" do + @rendered_component = @builder.polaris_text_field(:title, help_text: "Help Text") + + assert_selector ".Polaris-Label" do + assert_selector "label", text: "Title" + end + assert_selector ".Polaris-TextField" do + assert_selector %(input[name="product[title]"]) + assert_selector ".Polaris-Labelled__HelpText", text: "Help Text" + end + end + + test "#polaris_select" do + @rendered_component = @builder.polaris_select(:status, options: {"Active" => "active", "Draft" => "draft"}) + + assert_selector ".Polaris-Label" do + assert_selector "label", text: "Status" + end + assert_selector ".Polaris-Select" do + assert_selector %(select[name="product[status]"]) + assert_selector "option[value=active]" + assert_selector "option[value=draft]" + end + end + + test "#polaris_check_box" do + @rendered_component = @builder.polaris_check_box(:accept, label: "Checkbox Label") + + assert_selector "label.Polaris-Choice" do + assert_selector ".Polaris-Choice__Label", text: "Checkbox Label" + assert_selector ".Polaris-Checkbox" do + assert_selector %(input[name="product[accept]"][type="checkbox"]) + end + end + end + + test "#polaris_radio_button" do + @rendered_component = @builder.polaris_radio_button(:access, value: :allow, label: "Radio Label") + + assert_selector "label.Polaris-Choice" do + assert_selector ".Polaris-Choice__Label", text: "Radio Label" + assert_selector ".Polaris-RadioButton" do + assert_selector %(input[name="product[access]"][value="allow"][type="radio"]) + end + end + end +end