Skip to content

Commit

Permalink
Merge pull request #275 from stephannv/feat/test_helpers
Browse files Browse the repository at this point in the history
Add `render_view` test helper
  • Loading branch information
joeldrapper authored Oct 22, 2022
2 parents e54bb5b + 7b0261b commit b8ceea2
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/components/layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def template(&block)
render Nav::Item.new("Views", to: Pages::Views, active_page: @_parent)
render Nav::Item.new("Templates", to: Pages::Templates, active_page: @_parent)
render Nav::Item.new("Helpers", to: Pages::Helpers, active_page: @_parent)
render Nav::Item.new("Testing", to: Pages::Testing, active_page: @_parent)
end

h2(class: "text-lg font-semibold pt-5") { "Rails" }
Expand Down
94 changes: 94 additions & 0 deletions docs/pages/testing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: true

module Pages
class Testing < ApplicationPage
def template
render Layout.new(title: "Testing") do
render Markdown.new(<<~MD)
# Testing
## RSpec
Add test helpers to your suite:
```ruby
# spec/support/phlex.rb
require "phlex/test_helpers"
RSpec.configure do |config|
config.include Phlex::TestHelpers, type: :view
end
```
Remember to require this file in `rails_helper.rb` uncommenting the following line:
```ruby
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
```
### Asserting without capybara
```ruby
# menu.rb
class Menu < Phlex::View
def template
ul do
li do
a(href: "/") { "Home" }
a(href: "/sign_in") { "Sign In" }
end
end
end
end
# menu_spec.rb
it "renders Home menu item" do
result = render_view(Menu.new)
selector = result.css('ul li a[href="/"]:contains("Home")')
expect(selector).to be_present
end
```
### Asserting with capybara
Setup capybara:
```ruby
# spec/support/capybara.rb
require "capybara/rspec"
RSpec.configure do |config|
config.include Capybara::RSpecMatchers, type: :view
end
```
You are now able to use capybara matchers:
```ruby
# menu_spec.rb
it "renders Home menu item" do
result = render_view(Menu.new)
expect(result).to have_link("Home", href: "/")
end
```
### Passing blocks
You can pass blocks to `render_view`:
```ruby
it "renders items" do
result = render_view(Nav.new) do |nav|
nav.item(href: "/products/new") { "New Product" }
nav.item(href: "/categories/new") { "New Category" }
end
expect(result).to have_link("New", href: "/products/new")
end
```
MD
end
end
end
end
1 change: 1 addition & 0 deletions lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
loader.ignore("#{__dir__}/generators")
loader.ignore("#{__dir__}/install")
loader.ignore("#{__dir__}/phlex/test_helpers.rb")
loader.inflector.inflect("html" => "HTML")
loader.inflector.inflect("vcall" => "VCall")
loader.inflector.inflect("fcall" => "FCall")
Expand Down
14 changes: 14 additions & 0 deletions lib/phlex/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Phlex
module TestHelpers
def render_view(view, &block)
rendered = controller.view_context.render(view, &block)
Nokogiri::HTML.fragment(rendered)
end

def controller
ActionView::TestCase::TestController.new
end
end
end
41 changes: 41 additions & 0 deletions test/phlex/test_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require "test_helper"
require "phlex/test_helpers"

describe Phlex::TestHelpers do
include Phlex::TestHelpers

describe "#render_view" do
it "parses rendered string in to a nokogiri nodeset" do
view = Class.new(Phlex::View) do
def template
h2 { "Some title" }
end
end

output = render_view(view.new)

expect(output.class).to be == Nokogiri::HTML::DocumentFragment
end

it "accepts content block" do
nav_view = Class.new(Phlex::View) do
def template(&content)
ul(&content)
end

def item(&content)
li(&content)
end
end

output = render_view(nav_view.new) do |nav|
nav.item { "Menu A" }
nav.item { "Menu B" }
end

expect(output.to_s).to be == %(<ul>\n<li>Menu A</li>\n<li>Menu B</li>\n</ul>)
end
end
end

0 comments on commit b8ceea2

Please sign in to comment.