Module: TurboRspec::Helpers

Defined in:
lib/turbo_rspec/helpers.rb

Overview

Factory helpers for building Turbo HTML strings inline in tests. Auto-included into +type: :request+ and +type: :controller+ example groups.

Instance Method Summary collapse

Instance Method Details

#turbo_frame_html(id:, content: nil) ⇒ String

Builds a ++ HTML string for use in test assertions.

Examples:

turbo_frame_html(id: "messages", content: "Hello")

Parameters:

  • id (String)

    the frame's +id+ attribute

  • content (String, nil) (defaults to: nil)

    optional content inside the frame

Returns:

  • (String)


34
35
36
# File 'lib/turbo_rspec/helpers.rb', line 34

def turbo_frame_html(id:, content: nil)
  "<turbo-frame id=\"#{id}\">#{content}</turbo-frame>"
end

#turbo_stream_html(action:, target: nil, targets: nil, content: nil) ⇒ String

Builds a ++ HTML string for use in test assertions.

Examples:

turbo_stream_html(action: :append, target: "messages", content: "Hello")
turbo_stream_html(action: :remove, targets: ".item")

Parameters:

  • action (Symbol, String)

    the stream action (e.g. +:append+, +:replace+)

  • target (String, nil) (defaults to: nil)

    the +target+ DOM id attribute

  • targets (String, nil) (defaults to: nil)

    the +targets+ CSS selector attribute

  • content (String, nil) (defaults to: nil)

    optional content to place inside the template

Returns:

  • (String)


18
19
20
21
22
23
24
# File 'lib/turbo_rspec/helpers.rb', line 18

def turbo_stream_html(action:, target: nil, targets: nil, content: nil)
  attrs = "action=\"#{action}\""
  attrs += " target=\"#{target}\"" if target
  attrs += " targets=\"#{targets}\"" if targets
  inner = content ? "<template>#{content}</template>" : "<template></template>"
  "<turbo-stream #{attrs}>#{inner}</turbo-stream>"
end