komposable / komponent

An opinionated way of organizing front-end code in Ruby on Rails, based on components
http://komponent.io
MIT License
427 stars 31 forks source link

How to test a component inside a Rails application? #107

Open nicolas-brousse opened 6 years ago

nicolas-brousse commented 6 years ago

Those days I'm working on testing some part of a Rails application. And I was asking me, how to test a component? But I haven't a answer. So I open this issue to ask you if you already have a way, or if not to discuss about it.

Also if there is a way, may be it could be nice to implement a template for mini test and rspec during a component generation.

florentferry commented 6 years ago

No right answers here. It depends on what you want to test.

If you are using components as a structural tool, you can only check behaviors with functional tests. You can use unit tests for rendering (smoke test), and test edge cases handled in component modules. So what kind of tests do you want ? What tests would you see for components ?

Maybe choosing a component, and thinking about how you would test it and what you want to test should be the right way to discuss about it.

nicolas-brousse commented 6 years ago

Thanks for your answer @florentferry for now I'm not really sure of how to and what I would like to test exactly.

I mainly open this issue to know if it's something you already did.

I'll do some try later and let you know. But for now I have no answer.

aldhsu commented 5 years ago

If you just want to assert some HTML eg. testing your logic in the component.rb is correct, a method to do this is using the render anywhere methods in Rails 5. This only tests the erb and the component.rb file. This doesn't have a real browser backing it so you can't test user interaction or JS.

So a rendered example:

  it "displays time zone if user time zone and visit time zone are different" do
   ...
    html = ActionController::Base.render(
      assigns: {event: event, user: user},
      inline: '<%= c "event", event: @event, current_user: @user %>',
    )
    expect(html).to match "2:00am - 3:00am"
    expect(html).to match "AEST"
  end
Spone commented 5 years ago

It could be interesting to do some unit testing on the methods of the EventComponent module as well.

In this case, where do we put the test file? If we follow the current architecture, it would make sense to have it in the component folder.

nicolas-brousse commented 4 years ago

I was thinking a bit of this subject recently, and I'm not sure we really needs to test the ruby component file.

All tests could be done on view directly. All code of component should be use in view. Otherwise it sounds like old code.

I think we could have a module/class to extends or include, like for ActiveJob. It should be base on https://guides.rubyonrails.org/testing.html#testing-views and adds some helpers, like for rendering. We could have the same for RSpec.
And we may update the component generator to include test file too (in a components folder btw).