Class: TurboRspec::Matchers::HaveTurboStream

Inherits:
Object
  • Object
show all
Defined in:
lib/turbo_rspec/matchers/have_turbo_stream.rb

Overview

RSpec matcher for asserting that a response body contains a ++ element. Constraints are applied via a fluent chain; all specified constraints must match the same stream element.

Examples:

Basic usage

expect(response).to have_turbo_stream

Chained constraints

expect(response).to have_turbo_stream
  .with_action(:append)
  .targeting("messages")
  .with_content("Hello")

Negation

expect(response).not_to have_turbo_stream.with_action(:replace)

See Also:

Instance Method Summary collapse

Constructor Details

#initializeHaveTurboStream

Returns a new instance of HaveTurboStream.



25
26
27
28
29
30
31
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 25

def initialize
  @action = nil
  @target = nil
  @target_all = nil
  @content = nil
  @partial = nil
end

Instance Method Details

#descriptionString

Returns:

  • (String)


98
99
100
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 98

def description
  "have turbo stream#{constraint_description}"
end

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


83
84
85
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 83

def does_not_match?(response_or_body)
  !matches?(response_or_body)
end

#failure_messageString

Returns:

  • (String)


88
89
90
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 88

def failure_message
  "expected response to contain a turbo stream#{constraint_description}\n#{found_streams_message}"
end

#failure_message_when_negatedString

Returns:

  • (String)


93
94
95
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 93

def failure_message_when_negated
  "expected response not to contain a turbo stream#{constraint_description}"
end

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


75
76
77
78
79
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 75

def matches?(response_or_body)
  @body = extract_body(response_or_body)
  @streams = parse_streams(@body)
  @streams.any? { |stream| stream_matches?(stream) }
end

#rendering(partial) ⇒ self

Constrains the match to streams whose rendered HTML includes the given partial path.

Parameters:

  • partial (String)

    e.g. +"messages/_message"+

Returns:

  • (self)


68
69
70
71
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 68

def rendering(partial)
  @partial = partial.to_s
  self
end

#targeting(dom_id) ⇒ self

Constrains the match to streams targeting a specific DOM id.

Parameters:

  • dom_id (String)

Returns:

  • (self)


44
45
46
47
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 44

def targeting(dom_id)
  @target = dom_id.to_s
  self
end

#targeting_all(selector) ⇒ self

Constrains the match to streams targeting a CSS selector (the +targets+ attribute).

Parameters:

  • selector (String)

    e.g. +".message-item"+

Returns:

  • (self)


52
53
54
55
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 52

def targeting_all(selector)
  @target_all = selector.to_s
  self
end

#with_action(action) ⇒ self

Constrains the match to streams with the given action.

Parameters:

  • action (Symbol, String)

    e.g. +:append+, +:replace+, +:remove+, +:refresh+, +:morph+

Returns:

  • (self)


36
37
38
39
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 36

def with_action(action)
  @action = action.to_s
  self
end

#with_content(text) ⇒ self

Constrains the match to streams whose template content includes the given text.

Parameters:

  • text (String)

Returns:

  • (self)


60
61
62
63
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 60

def with_content(text)
  @content = text.to_s
  self
end