Skip to content

Commit

Permalink
assert_turbo_stream: accept a count
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
```
  • Loading branch information
ghiculescu committed Apr 6, 2023
1 parent 728b963 commit e748dce
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/turbo/test_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ module TestAssertions
delegate :dom_id, :dom_class, to: ActionView::RecordIdentifier
end

def assert_turbo_stream(action:, target: nil, targets: nil, status: :ok, &block)
def assert_turbo_stream(action:, target: nil, targets: nil, status: :ok, count: 1, &block)
assert_response status
assert_equal Mime[:turbo_stream], response.media_type
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

def assert_no_turbo_stream(action:, target: nil, targets: nil)
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 e748dce

Please sign in to comment.