Rails URL Generation in ViewComponent Previews

178 Views Asked by At

I have a component that uses slots, which I managed to figure out how to render in a preview. I know that in a test environment, it's possible to use with_request_url to specify a particular request URL for the test, but this does not seem possible for a preview.

The context here is that I am using previews to do some system testing on an isolated basis (inspired by this post: https://wrapbook.engineering/system-testing-view-components.html) of a cart system. The reason is that I am looking to do some in-browser testing.

The cart belongs to our main resource, and has some subresources (cart items) that we manipulate. The Cart component contains a Subcomponent that works with this subresource and accesses resource_cart_subresource_index_path which assumes there is a :resource_id present in the path, so I am getting a ActiveController::UrlGenerationError.

I'd love to be able to preview this component so I can do some in-browser testing. Any leads would be appreciated.

# spec/components/cart_component_preview.rb
class CartComponentPreview < ViewComponent::Preview
  def default
    user = FactoryBot.create(:user, :with_organizations, org_count: 1)
    @resource = FactoryBot.create(:resource, user:)
    FactoryBot.create(:cart, resource: @resource)
    render(Cart::MainComponent.new(resource: @resource)) do |component|
      component.with_subcomponent do
        Cart::SubComponent.new(resource: @resource).render_in(component)
      end
      component.with_info do
        Cart::InfoComponent.new(resource: @resource).render_in(component)
      end
    end
  end
end

I tried rendering via template as well, but ran into the same problem, and not sure of next steps.

1

There are 1 best solutions below

0
On

I was not able to figure out an answer, but I ended up moving these tests into an integration test using a browser based runner.