Skip to content

Commit

Permalink
Make Hanami::View::Rendered more specs friendly (#238)
Browse files Browse the repository at this point in the history
* Make `Hanami::View::Rendered` more specs friendly

* CHANGELOG
  • Loading branch information
jodosha authored Sep 19, 2023
1 parent 083f832 commit 5532152
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

View layer for Hanami

## v2.1.0.beta2 (unreleased)

### Added
- [Luca Guidi] Add `Hanami::View::Rendered#match?`, `#match`, and `#include?` to make it more specs friendly

## v2.1.0.beta1 - 2023-06-29

### Added
Expand Down
25 changes: 25 additions & 0 deletions lib/hanami/view/rendered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ def to_s
output
end
alias_method :to_str, :to_s

# Matches given input with the rendered view
#
# @param matcher [String, Regexp] matcher
#
# @return [TrueClass,FalseClass]
#
# @api public
# @since 2.1.0
def match?(matcher)
output.match?(matcher)
end
alias_method :match, :match?

# Checks if given string is included in the rendered view
#
# @param string [String] string
#
# @return [TrueClass,FalseClass]
#
# @api public
# @since 2.1.0
def include?(string)
output.include?(string)
end
end
end
end
17 changes: 17 additions & 0 deletions spec/unit/rendered_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
end
end

describe "#match?" do
it "matches rendered output" do
expect(rendered).to match("rendered template output")
end

it "is aliased as #match" do
expect(rendered.match?("rendered template output")).to be(true)
expect(rendered.match("rendered template output")).to be(true)
end
end

describe "#include?" do
it "matches rendered output" do
expect(rendered).to include("rendered template output")
end
end

describe "#locals" do
it "returns the locals hash" do
expect(rendered.locals).to eql(user: {name: "Jane"})
Expand Down

0 comments on commit 5532152

Please sign in to comment.