Skip to content

Commit

Permalink
Add tests for FormBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Nov 15, 2021
1 parent cb75e56 commit 816cdbd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/helpers/polaris/form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Polaris
class FormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::OutputSafetyHelper

attr_reader :template

delegate :render, :pluralize, to: :template
Expand Down
92 changes: 92 additions & 0 deletions test/helpers/polaris/form_builder_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 816cdbd

Please sign in to comment.