Skip to content

Commit

Permalink
Refactor Builder spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Spone committed May 1, 2023
1 parent 22a31a7 commit 3aa5285
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
5 changes: 5 additions & 0 deletions spec/internal/app/models/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Message < ActiveRecord::Base
has_rich_text :content
end
9 changes: 9 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
ActiveRecord::Schema.define do
# Set up any tables you need to exist for your test suite that don't belong
# in migrations.
if ENV.fetch("VIEW_COMPONENT_FORM_USE_ACTIONTEXT", "false") == "true"
create_table(:action_text_rich_texts, force: true) do |t|
t.string :name
t.text :body
t.references :record, null: false, polymorphic: true, index: false
t.timestamps
end
end

create_table(:authors, force: true) do |t|
t.string :name_with_initial
t.timestamps
Expand Down
39 changes: 16 additions & 23 deletions spec/view_component/form/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
require_relative "../../fixtures/test_model"

RSpec.describe ViewComponent::Form::Builder, type: :builder do
let(:object) { OpenStruct.new }
let(:form) { form_with(object) }
let(:options) { {} }

shared_examples "the default form builder" do |method_name, *args, rspec_around: lambda { |example|
example.run
}, **kwargs, &block|
around(&rspec_around)

let(:object) { OpenStruct.new(args.first ? {args.first => nil} : {}) } unless method_defined?(:object)

subject { form.public_send(method_name, *args, **kwargs, &block) }

let(:default_form_builder) { form_with(object, builder: ActionView::Helpers::FormBuilder) }
Expand All @@ -21,9 +23,10 @@
end
end

it_behaves_like "the default form builder", :check_box, "validated"
it_behaves_like "the default form builder", :check_box, "gooddog", {}, "yes", "no"
it_behaves_like "the default form builder", :check_box, "accepted", { class: "eula_check" }, "yes", "no"
it_behaves_like "the default form builder", :check_box, :validated
it_behaves_like "the default form builder", :check_box, :gooddog, {}, "yes", "no"
it_behaves_like "the default form builder", :check_box, :accepted, { class: "eula_check" }, "yes", "no"

context "with model-dependent fields" do
before do
Author.create(name_with_initial: "Touma K.")
Expand Down Expand Up @@ -133,35 +136,25 @@

it_behaves_like "the default form builder", :submit
it_behaves_like "the default form builder", :text_area, :detail
if defined?(ActionView::Helpers::Tags::ActionText)
let(:object_klass) do
Class.new(ActiveRecord::Base) do
has_rich_text :content

class << self
def name
"Message"
end
end
end
end

let(:object) { object_klass.new }

context "with a model with rich text" do
it_behaves_like "the default form builder", :rich_text_area, :content
end
end
it_behaves_like "the default form builder", :text_field, :name
it_behaves_like "the default form builder", :time_field, :born_at
it_behaves_like "the default form builder", :time_select, :average_lap
it_behaves_like "the default form builder", :time_zone_select, :time_zone, nil, { include_blank: true }
it_behaves_like "the default form builder", :url_field, :homepage
it_behaves_like "the default form builder", :week_field, :birthday_week

if Rails::VERSION::MAJOR >= 7
it_behaves_like "the default form builder", :weekday_select, :weekday, { include_blank: true }
end

if defined?(ActionView::Helpers::Tags::ActionText)
let(:object) { Message.new }

context "with a model with rich text" do
it_behaves_like "the default form builder", :rich_text_area, :content
end
end

describe "#component_klass" do
context "with gem Builder" do
let(:builder) { described_class.new(object_name, object, template, options) }
Expand Down

0 comments on commit 3aa5285

Please sign in to comment.