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: + # + # + #