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

rich_text_area spec #110

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ AllCops:
- 'gemfiles/**/*'
- 'lib/generators/**/templates/**/*'
- 'node_modules/**/*'
- 'spec/internal/db/schema.rb'
- 'tmp/**/*'
- 'vendor/**/*'

Expand All @@ -34,4 +35,3 @@ RSpec/MultipleMemoizedHelpers:
Metrics/BlockLength:
Exclude:
- 'spec/view_component/**/*'
- 'spec/internal/db/schema.rb'
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
13 changes: 13 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 Expand Up @@ -35,4 +44,8 @@
t.belongs_to :country
t.timestamps
end

create_table(:messages, force: true) do |t|
t.timestamps
end
end
20 changes: 16 additions & 4 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 @@ -139,10 +142,19 @@
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