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

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

Open
nicolas-brousse opened this issue Jul 28, 2018 · 5 comments
Open

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

nicolas-brousse opened this issue Jul 28, 2018 · 5 comments
Labels

Comments

@nicolas-brousse
Copy link
Member

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.

@Spone Spone added the question label Jul 28, 2018
@florentferry
Copy link
Member

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
Copy link
Member Author

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
Copy link

aldhsu commented Feb 18, 2019

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
Copy link
Collaborator

Spone commented Feb 18, 2019

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
Copy link
Member Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants