-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document the
assert_turbo_stream
helpers (#462)
First, mention the `assert_turbo_stream` test helpers in the [README.md](./README.md) under a new "Testing" heading. Next, add method-level documentation to describe each helper's interface. Finally, separate the `ActionDispatch::IntegrationTest` portion of the test into a new `Turbo::TestAssertions::IntegrationTestAssertions` module that's automatically included in `ActionDispatch::IntegrationTest` when it's loaded. By separating the two, the `assert_turbo_stream` helpers can be invoked outside of an HTTP request test, while still providing a baseline implementation for when an HTTP response is available.
- Loading branch information
1 parent
0661376
commit ea00f37
Showing
5 changed files
with
150 additions
and
6 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
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,76 @@ | ||
module Turbo | ||
module TestAssertions | ||
module IntegrationTestAssertions | ||
# Assert that the Turbo Stream request's response body's HTML contains a | ||
# `<turbo-stream>` element. | ||
# | ||
# === Options | ||
# | ||
# * <tt>:status</tt> [Integer, Symbol] the HTTP response status | ||
# * <tt>:action</tt> [String] matches the element's <tt>[action]</tt> | ||
# attribute | ||
# * <tt>:target</tt> [String, #to_key] matches the element's | ||
# <tt>[target]</tt> attribute. If the value responds to <tt>#to_key</tt>, | ||
# the value will be transformed by calling <tt>dom_id</tt> | ||
# * <tt>:targets</tt> [String] matches the element's <tt>[targets]</tt> | ||
# attribute | ||
# | ||
# Given the following HTML response body: | ||
# | ||
# <turbo-stream action="remove" target="message_1"></turbo-stream> | ||
# | ||
# The following assertion would pass: | ||
# | ||
# assert_turbo_stream action: "remove", target: "message_1" | ||
# | ||
# You can also pass a block make assertions about the contents of the | ||
# element. Given the following HTML response body: | ||
# | ||
# <turbo-stream action="replace" target="message_1"> | ||
# <template> | ||
# <p>Hello!</p> | ||
# <template> | ||
# </turbo-stream> | ||
# | ||
# The following assertion would pass: | ||
# | ||
# assert_turbo_stream action: "replace", target: "message_1" do | ||
# assert_select "template p", text: "Hello!" | ||
# end | ||
# | ||
def assert_turbo_stream(status: :ok, **attributes, &block) | ||
assert_response status | ||
assert_equal Mime[:turbo_stream], response.media_type | ||
super(**attributes, &block) | ||
end | ||
|
||
# Assert that the Turbo Stream request's response body's HTML does not | ||
# contain a `<turbo-stream>` element. | ||
# | ||
# === Options | ||
# | ||
# * <tt>:status</tt> [Integer, Symbol] the HTTP response status | ||
# * <tt>:action</tt> [String] matches the element's <tt>[action]</tt> | ||
# attribute | ||
# * <tt>:target</tt> [String, #to_key] matches the element's | ||
# <tt>[target]</tt> attribute. If the value responds to <tt>#to_key</tt>, | ||
# the value will be transformed by calling <tt>dom_id</tt> | ||
# * <tt>:targets</tt> [String] matches the element's <tt>[targets]</tt> | ||
# attribute | ||
# | ||
# Given the following HTML response body: | ||
# | ||
# <turbo-stream action="remove" target="message_1"></turbo-stream> | ||
# | ||
# The following assertion would fail: | ||
# | ||
# assert_no_turbo_stream action: "remove", target: "message_1" | ||
# | ||
def assert_no_turbo_stream(status: :ok, **attributes) | ||
assert_response status | ||
assert_equal Mime[:turbo_stream], response.media_type | ||
super(**attributes) | ||
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