-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from stephannv/feat/test_helpers
Add `render_view` test helper
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |