Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rails-intest-views

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rails-intest-views

  • 0.2.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Gem Version Build

Rails Intest Views

Generate test views (templates) right from test files. Useful to test UI components (view components, partials, JS, etc.) in isolation.

A quick example of using an inline template to test view components via Rails system tests:

class ButtonComponentSystemTest < ApplicationSystemTestCase
  def test_submit_button
    visit_template <<~ERB
      <form id="myForm" onsubmit="event.preventDefault(); this.innerHTML = '';">
        <h2>Self-destructing form</h2>
        <%= render ButtonComponent.new(type: :submit) do %>
          Save me!
        <% end %>
      </form>
    ERB

    assert_text("Self-destructing form")

    click_button("Save me!")

    assert_no_text("Self-destructing form")
  end
end

Installation

Adding gem to your Gemfile:

# Gemfile
gem "rails-intest-views", group: :test

Supported Ruby/Rails versions

  • Ruby (MRI) >= 2.7.0
  • Rails 6+

Usage

To use intest templates with Capybara, you must first include the helper module:

# Minitest
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
 include RailsIntestViews::CapybaraHelpers

  # ...
end

# RSpec
RSpec.configure do |config|
  config.include RailsIntestViews::CapybaraHelpers, type: :system
end

Then, you can use the #visit_template helper in your tests:

visit_template <<~ERB
  <a href="#">Clicko!</a>
ERB

# some assertions/expectations

You can also specify the layout to use:

source = <<~ERB
  <a href="#">Clicko!</a>
ERB

visit_template source, layout: "custom"

View components are supported through the #render_component helper:

button = ButtonComponent.new(type: :submit) do
  "Save me!"
end

render_component button

View component-based layouts are supported as well:

button = ButtonComponent.new(type: :submit) do
  "Save me!"
end

render_component button, layout: PreviewLayout
# or
render_component button, layout: -> { PreviewLayout }

Configuration

The following configuration parameters are available (the default values are shown):

# a controller class used to render templates
config.rails_intest_views.templates_controller = "RailsIntestViews::TemplatesController"
# default layout to use; no layout by default (but you're likely to specify it to load scripts, styles, etc.)
config.rails_intest_views.default_layout = nil
# the mount path for the endpoint (the "/templates/:id" is added to the final URL)
config.rails_intest_views.mount_path = "/rails/intest"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/palkan/rails-intest-views.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 12 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc