Skip to content

Commit

Permalink
assert_turbo_stream: accept a count (#452)
Browse files Browse the repository at this point in the history
This PR allows you to provide a `count` keyword argument to `assert_turbo_stream`. The argument is just passed on to `assert_select`.

For example, if you expect a controller to remove 4 elements, you could do:

```ruby
assert_turbo_stream action: :remove, count: 4
```

Co-authored-by: David Heinemeier Hansson <dhh@hey.com>
  • Loading branch information
ghiculescu and dhh committed Jun 21, 2023
1 parent 92ad1f4 commit 59ba919
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/turbo/test_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module TestAssertions
# the value will be transformed by calling <tt>dom_id</tt>
# * <tt>:targets</tt> [String] matches the element's <tt>[targets]</tt>
# attribute
# * <tt>:count</tt> [Integer] indicates how many turbo streams are expected.
# Defaults to <tt>1</tt>.
#
# Given the following HTML fragment:
#
Expand All @@ -43,11 +45,11 @@ module TestAssertions
# assert_select "template p", text: "Hello!"
# end
#
def assert_turbo_stream(action:, target: nil, targets: nil, &block)
def assert_turbo_stream(action:, target: nil, targets: nil, count: 1, &block)
selector = %(turbo-stream[action="#{action}"])
selector << %([target="#{target.respond_to?(:to_key) ? dom_id(target) : target}"]) if target
selector << %([targets="#{targets}"]) if targets
assert_select selector, count: 1, &block
assert_select selector, count: count, &block
end

# Assert that the rendered fragment of HTML does not contain a `<turbo-stream>`
Expand Down
1 change: 1 addition & 0 deletions test/streams/streams_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Turbo::StreamsControllerTest < ActionDispatch::IntegrationTest

patch message_path(id: 1), as: :turbo_stream

assert_turbo_stream action: :replace, count: 4
assert_turbo_stream action: :replace, targets: "#message_4" do
assert_select 'template', 'Something fourth'
end
Expand Down

0 comments on commit 59ba919

Please sign in to comment.