Giter Club home page Giter Club logo

rspec-viewcomponent's Introduction

RSpec::ViewComponent

RSpec::ViewComponent provides contexts that help you write clean one-line specs for your components. This enables you to test that they respond as expected and specify their function without the cruft of boilerplate.

Installation

Add this line to your application's Gemfile, ideally in the test group:

gem 'rspec-viewcomponent'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rspec-viewcomponent

Usage

Require the gem in your spec_helper.rb:

require 'rspec-viewcomponent'

Then, make the shared context available to component examples:

RSpec.configure do |config|
  config.include RSpec::ViewComponent::Context, type: :component
end

It's suggested that you follow the patterns laid out in spec/viewcomponent/rspec/context_spec.rb when testing your component. Generally, you'll only need to test the rendered component unless you're reliant on methods you call upon it. RSpec::ViewComponent::Context makes this available as the subject of any tests by default, allowing you to write one-line tests with matchers like have_css and be_empty:

describe ExampleComponent do
  let(:args) { ['red'] }
  let(:content) { ->(_view_context = nil) { 'Hello World' } }

  describe 'rendered component' do
    it { is_expected.to have_css('p.red', text: 'Hello World') }

    context 'with nil content' do
      let(:content) { ->(view_context = nil) {} }

      it { is_expected.to be_empty }
    end
  end
end

As you can see there, the context also makes use of two variables defined with let:

  • args, passed to the component upon initialize, e.g. 'red' in the case of ExampleComponent.new('red'). kwargs is also available for keyword arguments.

  • content, passed to the component in a block when it's rendered, e.g. ExampleComponent.new.with_content('Hello World') or

    <%= render ExampleComponent do %>
      Hello World
    <% end %>

component_instance is also available, should you wish to test against the component instance itself.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/viewcomponent/rspec-viewcomponent. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the RSpec::ViewComponent project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

rspec-viewcomponent's People

Contributors

boardfish avatar dependabot[bot] avatar

Stargazers

Anatoly Chernov avatar Serhii Ponomarov avatar Juri Hahn avatar Stefan Vermaas avatar Fabrizio Azzarri avatar Matheus Poli avatar hirabayashi naoki avatar Pedro Paiva avatar Hans Lemuet avatar

Watchers

 avatar  avatar

Forkers

petergoldstein

rspec-viewcomponent's Issues

Introduce named subject for rendered component

@andapopovici and @pythonandchips suggested they'd like to see this feature. Right now, the rendered component is an anonymous subject.

Potentially regardless of whether folks prefer one-liner specs or not, a named subject would be useful for the odd case where you do see yourself making a full it .. do block, but optimally, I'd expect the vast majority of component specs to be able to take this one-liner style.

Raise error on deprecation warnings

We might be able to achieve this with ActiveSupport if ViewComponent uses ActiveSupport::Deprecation internally. Otherwise, I've had good experience with DeprecationToolkit in the past.

Gem naming

I think the current convention for naming RSpec extensions is rspec-* (rspec-rails, rspec-sidekiq, etc.)

Is there a reason for choosing viewcomponent-rspec over rspec-viewcomponent?

NoMethodError: undefined method `matcher' for class RSpec::ExampleGroups...

require 'rails_helper'

RSpec.describe Projects::NewProjectLinkComponent, type: :component do
  let(:company) { create(:company) }

  let(:kwargs) { { company: company } }

  it { is_expected.to inherit_from ApplicationComponent }

end

Running this raises:

NoMethodError:
  undefined method `matcher' for class RSpec::ExampleGroups::ProjectsNewProjectLinkComponent
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/example_group.rb:752:in `method_missing'
# /Users/sergiobayona/.rbenv/versions/3.3.2/lib/ruby/gems/3.3.0/gems/rspec-viewcomponent-0.1.0/lib/rspec/viewcomponent/context.rb:26:in `block (2 levels) in <module:Context>'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-support-3.12.1/lib/rspec/support/with_keywords_when_needed.rb:22:in `class_exec'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-support-3.12.1/lib/rspec/support/with_keywords_when_needed.rb:22:in `class_exec'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/shared_example_group.rb:38:in `block in include_in'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/example_group.rb:822:in `with_frame'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/shared_example_group.rb:37:in `include_in'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/example_group.rb:386:in `find_and_eval_shared'
# /Users/sergiobayona/.gem/ruby/3.3.0/gems/rspec-core-3.12.2/lib/rspec/core/example_group.rb:344:in `include_context'
# /Users/sergiobayona/.rbenv/versions/3.3.2/lib/ruby/gems/3.3.0/gems/rspec-viewcomponent-0.1.0/lib/rspec/viewcomponent/context.rb:33:in `block in <module:Context>'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.