ViewComponent / view_component

A framework for building reusable, testable & encapsulated view components in Ruby on Rails.
https://viewcomponent.org
MIT License
3.24k stars 416 forks source link

Allow `test_controller` to be customizable per component #440

Closed agrobbin closed 3 years ago

agrobbin commented 4 years ago

Feature request

Right now, there is no way to customize the test_controller on a per-component-basis, which has made it difficult for us to test different groups of components.

Base.test_controller is a mattr_accessor, which doesn't allow customizing on a per-component basis. Additionally, TestHelpers#controller always uses Base.test_controller, rather than using the test_controller of the component currently under test.

If Base.test_controller was changed to a class_attribute, and TestHelpers#controller was changed to use the more specific one, the default scenario of everyone using ApplicationController would continue to work, while those who want to isolated different namespaces of an application's components would be able to do so!

Motivation

We have 3 different namespaces within our application, each with its own set of view components (and each with their own base controller class, i.e. Foo::BaseController and Bar::BaseController). We'd love to be able to mirror that hierarchy in our component classes, having Foo::BaseComponent that all of our Fooey components inherit from, and then be able to inherit from something other than ApplicationController in those component's tests.

joelhawksley commented 4 years ago

@agrobbin sounds like a good idea to me! Would you like to take a crack at implementing this?

cc @juanmanuelramallo

agrobbin commented 4 years ago

@joelhawksley sure! The class_attribute switch should be relatively easy. I'll have to think about the best way to extract the right test_controller. A couple sanity check questions if you don't mind:

Is controller something that should remain part of the public TestHelpers interface? Or is it something that can be made private to the render_inline helper method (this would make extracting the test_controller very easy, but breaks the current contract of controller being something that's theoretically usable by tests).

joelhawksley commented 4 years ago

@agrobbin Sounds good!

controller does need to remain public.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

agrobbin commented 4 years ago

Sorry Stalebot, I was clearly too slow here! I've issued #465 to add this functionality.