From c0a8adc8a03b9fa71b7bce8e7d19e222aefa3e6e Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sat, 6 May 2023 10:45:15 -0400 Subject: [PATCH] Document the `assert_turbo_stream` helpers 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. --- README.md | 7 ++ lib/turbo/engine.rb | 9 ++- lib/turbo/test_assertions.rb | 62 ++++++++++++++- .../integration_test_assertions.rb | 76 +++++++++++++++++++ test/streams/streams_controller_test.rb | 2 +- 5 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 lib/turbo/test_assertions/integration_test_assertions.rb diff --git a/README.md b/README.md index 6d671aef..1df479d2 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,13 @@ You can watch [the video introduction to Hotwire](https://hotwired.dev/#screenca Turbo can coexist with Rails UJS, but you need to take a series of upgrade steps to make it happen. See [the upgrading guide](https://github.com/hotwired/turbo-rails/blob/main/UPGRADING.md). +## Testing + + +The [`Turbo::TestAssertions`](./lib/turbo/test_assertions.rb) concern provides Turbo Stream test helpers that assert the presence or absence of `` elements in a rendered fragment of HTML. `Turbo::TestAssertions` are automatically included in [`ActiveSupport::TestCase`](https://edgeapi.rubyonrails.org/classes/ActiveSupport/TestCase.html) and depend on the presence of [`rails-dom-testing`](https://github.com/rails/rails-dom-testing/) assertions. + +The [`Turbo::TestAssertions::IntegrationTestAssertions`](./lib/turbo/test_assertions/integration_test_assertions.rb) are built on top of `Turbo::TestAssertions`, and add support for passing a `status:` keyword. They are automatically included in [`ActionDispatch::IntegrationTest`](https://edgeguides.rubyonrails.org/testing.html#integration-testing). + ## Development diff --git a/lib/turbo/engine.rb b/lib/turbo/engine.rb index 9234f563..77c5a775 100644 --- a/lib/turbo/engine.rb +++ b/lib/turbo/engine.rb @@ -1,5 +1,4 @@ require "rails/engine" -require "turbo/test_assertions" module Turbo class Engine < Rails::Engine @@ -69,8 +68,16 @@ class Engine < Rails::Engine initializer "turbo.test_assertions" do ActiveSupport.on_load(:active_support_test_case) do + require "turbo/test_assertions" + include Turbo::TestAssertions end + + ActiveSupport.on_load(:action_dispatch_integration_test) do + require "turbo/test_assertions/integration_test_assertions" + + include Turbo::TestAssertions::IntegrationTestAssertions + end end initializer "turbo.integration_test_request_encoding" do diff --git a/lib/turbo/test_assertions.rb b/lib/turbo/test_assertions.rb index ee756c9c..84b67b2a 100644 --- a/lib/turbo/test_assertions.rb +++ b/lib/turbo/test_assertions.rb @@ -7,17 +7,71 @@ module TestAssertions delegate :dom_id, :dom_class, to: ActionView::RecordIdentifier end - def assert_turbo_stream(action:, target: nil, targets: nil, status: :ok, &block) - assert_response status - assert_equal Mime[:turbo_stream], response.media_type + # Assert that the rendered fragment of HTML contains a `` + # element. + # + # === Options + # + # * :action [String] matches the element's [action] + # attribute + # * :target [String, #to_key] matches the element's + # [target] attribute. If the value responds to #to_key, + # the value will be transformed by calling dom_id + # * :targets [String] matches the element's [targets] + # attribute + # + # Given the following HTML fragment: + # + # + # + # 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 fragment: + # + # + #