-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Base and Field component errors methods (#71)
* Add bin/rspec binstub * fix: make Base and Field component errors methods works * test: exclude spec folder of code coverage
- Loading branch information
1 parent
f9cad39
commit 4a09c1b
Showing
7 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
# | ||
# This file was generated by Bundler. | ||
# | ||
# The application 'rspec' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require "pathname" | ||
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", | ||
Pathname.new(__FILE__).realpath) | ||
|
||
bundle_binstub = File.expand_path("../bundle", __FILE__) | ||
|
||
if File.file?(bundle_binstub) | ||
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ | ||
load(bundle_binstub) | ||
else | ||
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. | ||
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") | ||
end | ||
end | ||
|
||
require "rubygems" | ||
require "bundler/setup" | ||
|
||
load Gem.bin_path("rspec-core", "rspec") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ViewComponent::Form::BaseComponent, type: :component do | ||
let(:object_klass) do | ||
Class.new do | ||
include ActiveModel::Model | ||
|
||
attr_accessor :first_name | ||
|
||
validates :first_name, presence: true, length: { minimum: 2 } | ||
|
||
class << self | ||
def name | ||
"User" | ||
end | ||
end | ||
end | ||
end | ||
|
||
let(:object) { object_klass.new } | ||
let(:form) { form_with(object) } | ||
let(:options) { {} } | ||
|
||
let(:component) { described_class.new(form, object_name, options) } | ||
|
||
describe "#object_errors?" do | ||
before { object.validate } | ||
|
||
context "with valid object" do | ||
let(:object) { object_klass.new(first_name: "John") } | ||
|
||
it { expect(component.object_errors?).to eq(false) } | ||
end | ||
|
||
context "with invalid object" do | ||
let(:object) { object_klass.new(first_name: "") } | ||
|
||
it { expect(component.object_errors?).to eq(true) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe ViewComponent::Form::FieldComponent, type: :component do | ||
let(:object_klass) do | ||
Class.new do | ||
include ActiveModel::Model | ||
|
||
attr_accessor :first_name | ||
|
||
validates :first_name, presence: true, length: { minimum: 2 } | ||
|
||
class << self | ||
def name | ||
"User" | ||
end | ||
end | ||
end | ||
end | ||
|
||
let(:object) { object_klass.new } | ||
let(:form) { form_with(object) } | ||
let(:options) { {} } | ||
|
||
let(:component) { described_class.new(form, object_name, :first_name, options) } | ||
|
||
describe "#method_errors" do | ||
before { object.validate } | ||
|
||
context "with valid object" do | ||
let(:object) { object_klass.new(first_name: "John") } | ||
|
||
it { expect(component.method_errors).to eq([]) } | ||
end | ||
|
||
context "with invalid object" do | ||
let(:object) { object_klass.new(first_name: "") } | ||
|
||
it { expect(component.method_errors).to eq(["Can't be blank", "Is too short (minimum is 2 characters)"]) } | ||
end | ||
end | ||
|
||
describe "#method_errors?" do | ||
before { object.validate } | ||
|
||
context "with valid object" do | ||
let(:object) { object_klass.new(first_name: "John") } | ||
|
||
it { expect(component.method_errors?).to eq(false) } | ||
end | ||
|
||
context "with invalid object" do | ||
let(:object) { object_klass.new(first_name: "") } | ||
|
||
it { expect(component.method_errors?).to eq(true) } | ||
end | ||
end | ||
|
||
describe "#value" do | ||
let(:object) { object_klass.new(first_name: "John") } | ||
|
||
it { expect(component.value).to eq("John") } | ||
end | ||
|
||
describe "#object_method_names" do | ||
it { expect(component.object_method_names).to eq(%i[first_name]) } | ||
|
||
pending "test with belongs_to for _id" | ||
pending "test with has_many for _ids" | ||
end | ||
end |