Module: Turbo::TestAssertions

Extended by:
ActiveSupport::Concern
Defined in:
lib/turbo/test_assertions.rb,
lib/turbo/test_assertions/integration_test_assertions.rb

Defined Under Namespace

Modules: IntegrationTestAssertions

Instance Method Summary collapse

Instance Method Details

#assert_no_turbo_stream(action:, target: nil, targets: nil) ⇒ Object

Assert that the rendered fragment of HTML does not contain a ‘<turbo-stream>` 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:

    <turbo-stream action="remove" target="message_1"></turbo-stream>
    

    The following assertion would fail:

    assert_no_turbo_stream action: "remove", target: "message_1"
    


76
77
78
79
80
81
# File 'lib/turbo/test_assertions.rb', line 76

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

#assert_turbo_stream(action:, target: nil, targets: nil, count: 1, &block) ⇒ Object

Assert that the rendered fragment of HTML contains a ‘<turbo-stream>` 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

  • :count [Integer] indicates how many turbo streams are expected. Defaults to 1.

    Given the following HTML fragment:

    <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 fragment:

  <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


48
49
50
51
52
53
# File 'lib/turbo/test_assertions.rb', line 48

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: count, &block
end