How to test View Component routes

121 views Asked by At

I have a card component that when clicked, takes you to a different route in the application. The component is built using Rails ViewComponents. What is the best way to write tests that ensure when a user clicks on the component, they are taken to the correct route?

Here is the .rb file of the View Component:

class LandingCardComponent < ViewComponent::Base

  def initialize(title: "", description: "", label: "", path: "")
    @title = title
    @description = description
    @label = label
    @path = path
  end
end
1

There are 1 answers

0
spickermann On BEST ANSWER

I suggest testing if there is a link in the rendered view looks like expected.

require "test_helper"

class LandingCardComponentTest < ViewComponent::TestCase
  def test_render_component
    render_inline(LandingCardComponent.new(title:       "Title", 
                                           description: "Description", 
                                           label:       "Label", 
                                           path:        "/foo/bar"))

    assert_select("a[href=?]", "/foo/bar", text: "Label")
  end
end