Skip to content

Commit

Permalink
Fix Base and Field component errors methods (#71)
Browse files Browse the repository at this point in the history
* Add bin/rspec binstub
* fix: make Base and Field component errors methods works
* test: exclude spec folder of code coverage
  • Loading branch information
nicolas-brousse authored Sep 27, 2021
1 parent f9cad39 commit 4a09c1b
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 3 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ AllCops:
TargetRubyVersion: 2.7
NewCops: enable
Exclude:
- bin/**/*
- lib/generators/**/templates/**/*
- gemfiles/**/*
- vendor/**/*
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Setup rspec-html-matchers and use it for complex components specs (#65)

### Fixed
- Fix errors methods in `BaseComponent` and `FieldComponent` (#71)

## [0.1.0] - 2021-09-16

### Added
Expand All @@ -22,4 +25,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[Unreleased]: https://github.com/pantographe/view_component-form/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/pantographe/view_component-form/releases/tag/v0.1.0

2 changes: 1 addition & 1 deletion app/components/view_component/form/field_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def value

def object_method_names
@object_method_names ||= begin
object_method_names = [method_name]
object_method_names = [method_name.to_sym]
if method_name.end_with?("_id") && object.respond_to?(singular_association_method_name)
object_method_names << singular_association_method_name
elsif method_name.end_with?("_ids") && object.respond_to?(collection_association_method_name)
Expand Down
29 changes: 29 additions & 0 deletions bin/rspec
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")
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
if ENV.fetch("COVERAGE", false)
require "simplecov"
SimpleCov.start do
minimum_coverage 90
add_filter "/spec"

minimum_coverage 89
maximum_coverage_drop 2
end
end
Expand Down
41 changes: 41 additions & 0 deletions spec/view_component/form/base_component_spec.rb
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
70 changes: 70 additions & 0 deletions spec/view_component/form/field_component_spec.rb
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

0 comments on commit 4a09c1b

Please sign in to comment.