Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In tests, move forms inline to cut down on verbosity #2240

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/olive-students-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@primer/view-components': minor
---

Allow anonymous forms, mostly useful for tests

<!-- Changed components: _none_ -->
10 changes: 10 additions & 0 deletions app/helpers/primer/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@ def primer_fields_for(record_name, record_object = nil, options = {}, &block)
&block
)
end

def inline_form(*args, &block)
Primer::Forms.inline_form(*args, &block)
end

def render_inline_form(*args, &block)
# rubocop:disable GitHub/RailsViewRenderLiteral
render(inline_form(*args, &block))
# rubocop:enable GitHub/RailsViewRenderLiteral
end
end
end
16 changes: 16 additions & 0 deletions lib/primer/forms.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Primer
# :nodoc:
module Forms
def self.inline_form(builder, base = nil, &block)
base ||= defined?(ApplicationForm) ? ApplicationForm : Primer::Forms::Base

klass = Class.new(base) do
form(&block)
end

klass.new(builder)
end
end
end
2 changes: 2 additions & 0 deletions lib/primer/forms/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module Utils
# conventions, so it should work ok. Zeitwerk also has this information but lacks a
# public API to map constants to source files.
def const_source_location(class_name)
return nil unless class_name

# NOTE: underscore respects namespacing, i.e. will convert Foo::Bar to foo/bar.
class_path = "#{class_name.underscore}.rb"

Expand Down
42 changes: 15 additions & 27 deletions test/lib/primer/forms/checkbox_group_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,43 @@
class Primer::Forms::CheckboxGroupInputTest < Minitest::Test
include Primer::ComponentTestHelpers

class HiddenCheckboxGroupForm < ApplicationForm
form do |check_form|
check_form.check_box_group(label: "Foobar", hidden: true) do |check_group|
check_group.check_box(name: :foo, label: "Foo")
end
end
end

def test_hidden_checkbox_group
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(HiddenCheckboxGroupForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box_group(label: "Foobar", hidden: true) do |check_group|
check_group.check_box(name: :foo, label: "Foo")
end
end
end
end

assert_selector "fieldset", visible: :hidden
assert_selector ".FormControl-checkbox-wrap", visible: :hidden
end

class DisabledCheckboxGroupForm < ApplicationForm
form do |check_form|
check_form.check_box_group(label: "Foobar", disabled: true) do |check_group|
check_group.check_box(name: :foo, label: "Foo")
end
end
end

def test_disabled_checkbox_group_disables_constituent_checkboxes
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(DisabledCheckboxGroupForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box_group(label: "Foobar", disabled: true) do |check_group|
check_group.check_box(name: :foo, label: "Foo")
end
end
end
end

assert_selector ".FormControl-checkbox-wrap input[disabled]"
end

class DisabledCheckboxInGroupForm < ApplicationForm
form do |check_form|
check_form.check_box_group(label: "Foobar") do |check_group|
check_group.check_box(name: :foo, label: "Foo", disabled: true)
end
end
end

def test_checkbox_can_be_individually_disabled_in_group
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(DisabledCheckboxInGroupForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box_group(label: "Foobar") do |check_group|
check_group.check_box(name: :foo, label: "Foo", disabled: true)
end
end
end
end

Expand Down
46 changes: 17 additions & 29 deletions test/lib/primer/forms/checkbox_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
class Primer::Forms::CheckboxInputTest < Minitest::Test
include Primer::ComponentTestHelpers

class BadCheckboxesForm < ApplicationForm
form do |check_form|
check_form.check_box(name: :alpha, label: "Alpha", scheme: :array)
end
end

def test_array_checkboxes_require_values
error = assert_raises(ArgumentError) do
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(BadCheckboxesForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box(name: :alpha, label: "Alpha", scheme: :array)
end
end
end
end
Expand All @@ -32,41 +28,33 @@ class NestedForm < ApplicationForm
end
end

class HiddenCheckboxForm < ApplicationForm
form do |check_form|
check_form.check_box(name: :foo, label: "Foo", hidden: true) do |foo_check|
foo_check.nested_form do |builder|
NestedForm.new(builder)
end
end
end
end

def test_hidden_checkbox
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(HiddenCheckboxForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box(name: :foo, label: "Foo", hidden: true) do |foo_check|
foo_check.nested_form do |builder|
NestedForm.new(builder)
end
end
end
end
end

assert_selector "input[name=foo]", visible: :hidden
assert_selector "input[name=bar]", visible: :hidden
end

class CheckboxWithHiddenNestedForm < ApplicationForm
form do |check_form|
check_form.check_box(name: :foo, label: "Foo") do |foo_check|
foo_check.nested_form(hidden: true) do |builder|
NestedForm.new(builder)
end
end
end
end

def test_nested_form_can_be_hidden_independently
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(CheckboxWithHiddenNestedForm.new(f))
render_inline_form(f) do |check_form|
check_form.check_box(name: :foo, label: "Foo") do |foo_check|
foo_check.nested_form(hidden: true) do |builder|
NestedForm.new(builder)
end
end
end
end
end

Expand Down
22 changes: 7 additions & 15 deletions test/lib/primer/forms/form_control_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@
class Primer::Forms::FormControlTest < Minitest::Test
include Primer::ComponentTestHelpers

class PlainTextFieldForm < ApplicationForm
form do |test_form|
test_form.text_field(name: :ultimate_answer, label: "Ultimate answer")
end
end

class AutoCheckTextFieldForm < ApplicationForm
form do |test_form|
test_form.text_field(name: :ultimate_answer, label: "Ultimate answer", auto_check_src: "/foo")
end
end

def test_plain_supports_server_errors
def test_supports_model_errors
model = DeepThought.new(41)
model.valid? # perform validations

render_in_view_context do
primer_form_with(url: "/foo", model: model) do |f|
render(PlainTextFieldForm.new(f))
render_inline_form(f) do |test_form|
test_form.text_field(name: :ultimate_answer, label: "Ultimate answer")
end
end
end

Expand All @@ -42,7 +32,9 @@ def test_auto_check_generates_validation_elements

render_in_view_context do
primer_form_with(url: "/foo", model: model) do |f|
render(AutoCheckTextFieldForm.new(f))
render_inline_form(f) do |test_form|
test_form.text_field(name: :ultimate_answer, label: "Ultimate answer", auto_check_src: "/foo")
end
end
end

Expand Down
48 changes: 18 additions & 30 deletions test/lib/primer/forms/multi_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,16 @@
class Primer::Forms::MultiInputTest < Minitest::Test
include Primer::ComponentTestHelpers

class MultipleFieldsVisibleForm < ApplicationForm
form do |test_form|
test_form.multi(name: :foo, label: "Thing") do |multi|
multi.text_field(name: :bar, label: "Text field")
multi.text_field(name: :baz, label: "Text field")
end
end
end

class FieldsWithDifferentNamesForm < ApplicationForm
form do |test_form|
test_form.multi(name: :foo, label: "Thing") do |multi|
multi.text_field(name: :bar, label: "Text field")
multi.text_field(name: :baz, label: "Text field", hidden: true)
end
end
end

def test_disallows_two_visible_inputs
error = assert_raises(ArgumentError) do
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(MultipleFieldsVisibleForm.new(f))
render_inline_form(f) do |test_form|
test_form.multi(name: :foo, label: "Thing") do |multi|
multi.text_field(name: :bar, label: "Text field")
multi.text_field(name: :baz, label: "Text field")
end
end
end
end
end
Expand All @@ -39,30 +26,31 @@ def test_disallows_two_visible_inputs
def test_inputs_have_data_name
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(FieldsWithDifferentNamesForm.new(f))
render_inline_form(f) do |test_form|
test_form.multi(name: :foo, label: "Thing") do |multi|
multi.text_field(name: :bar, label: "Text field")
multi.text_field(name: :baz, label: "Text field", hidden: true)
end
end
end
end

assert_selector "input[data-name=bar]"
assert_selector "input[data-name=baz]", visible: :hidden
end

class MultiDeepThoughtForm < ApplicationForm
form do |text_area_form|
text_area_form.multi(name: :ultimate_answer, label: "Ultimate answer") do |ultimate_answer_multi|
ultimate_answer_multi.text_field(name: :foo)
ultimate_answer_multi.text_field(name: :bar, hidden: true)
end
end
end

def test_no_error_markup
model = DeepThought.new(41)
model.valid? # populate validation error messages

render_in_view_context do
primer_form_with(model: model, url: "/foo") do |f|
render(MultiDeepThoughtForm.new(f))
render_inline_form(f) do |test_form|
test_form.multi(name: :ultimate_answer, label: "Ultimate answer") do |ultimate_answer_multi|
ultimate_answer_multi.text_field(name: :foo)
ultimate_answer_multi.text_field(name: :bar, hidden: true)
end
end
end
end

Expand Down
42 changes: 15 additions & 27 deletions test/lib/primer/forms/radio_button_group_input_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,43 @@
class Primer::Forms::RadioButtonGroupInputTest < Minitest::Test
include Primer::ComponentTestHelpers

class HiddenRadioGroupForm < ApplicationForm
form do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar", hidden: true) do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo")
end
end
end

def test_hidden_radio_button_group
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(HiddenRadioGroupForm.new(f))
render_inline_form(f) do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar", hidden: true) do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo")
end
end
end
end

assert_selector "fieldset", visible: :hidden
assert_selector ".FormControl-radio-wrap", visible: :hidden
end

class DisabledRadioGroupForm < ApplicationForm
form do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar", disabled: true) do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo")
end
end
end

def test_disabled_radio_group_disables_constituent_radios
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(DisabledRadioGroupForm.new(f))
render_inline_form(f) do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar", disabled: true) do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo")
end
end
end
end

assert_selector ".FormControl-radio-wrap input[disabled]"
end

class DisabledRadioInGroupForm < ApplicationForm
form do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar") do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo", disabled: true)
end
end
end

def test_radio_can_be_individually_disabled_in_group
render_in_view_context do
primer_form_with(url: "/foo") do |f|
render(DisabledRadioInGroupForm.new(f))
render_inline_form(f) do |radio_form|
radio_form.radio_button_group(name: :foobar, label: "Foobar") do |radio_group|
radio_group.radio_button(name: :foo, value: "Foo", label: "Foo", disabled: true)
end
end
end
end

Expand Down
Loading
Loading