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
32
33
34
35
36
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 25

def initialize
  @action = nil
  @target = nil
  @target_all = nil
  @content = nil
  @partial = nil
  @attributes = {}
  @children_only = false
  @expected_count = nil
  @count_type = :at_least
  @matching_count = 0
end

Instance Method Details

#at_least(n) ⇒ self

Asserts at least +n+ matching streams.

Parameters:

  • n (Integer)

Returns:

  • (self)


128
129
130
131
132
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 128

def at_least(n)
  @expected_count = n
  @count_type = :at_least
  self
end

#at_most(n) ⇒ self

Asserts at most +n+ matching streams.

Parameters:

  • n (Integer)

Returns:

  • (self)


137
138
139
140
141
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 137

def at_most(n)
  @expected_count = n
  @count_type = :at_most
  self
end

#children_onlyself

Constrains the match to morph streams with the +children-only+ attribute set.

Examples:

expect(response).to have_turbo_stream.with_action(:morph).children_only

Returns:

  • (self)


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

def children_only
  @children_only = true
  self
end

#descriptionString

Returns:

  • (String)


175
176
177
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 175

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

#does_not_match?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


160
161
162
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 160

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

#exactly(n) ⇒ self

Asserts exactly +n+ matching streams.

Parameters:

  • n (Integer)

Returns:

  • (self)


119
120
121
122
123
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 119

def exactly(n)
  @expected_count = n
  @count_type = :exactly
  self
end

#failure_messageString

Returns:

  • (String)


165
166
167
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 165

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

#failure_message_when_negatedString

Returns:

  • (String)


170
171
172
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 170

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

#matches?(response_or_body) ⇒ Boolean

Parameters:

  • response_or_body (#body, String)

Returns:

  • (Boolean)


151
152
153
154
155
156
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 151

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

#onceself

Asserts exactly one matching stream.

Returns:

  • (self)


106
107
108
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 106

def once
  exactly(1)
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)


79
80
81
82
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 79

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)


55
56
57
58
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 55

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)


63
64
65
66
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 63

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

#timesself

Fluent terminator so +.exactly(2).times+ reads naturally.

Returns:

  • (self)


145
146
147
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 145

def times
  self
end

#twiceself

Asserts exactly two matching streams.

Returns:

  • (self)


112
113
114
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 112

def twice
  exactly(2)
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)

Raises:

  • (ArgumentError)

    if the action is not a known built-in or registered custom action



42
43
44
45
46
47
48
49
50
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 42

def with_action(action)
  action_str = action.to_s
  unless TurboRspec.known_actions.include?(action_str)
    raise ArgumentError, "Unknown Turbo stream action #{action_str.inspect}. " \
      "Register custom actions with TurboRspec.register_action(:#{action_str})."
  end
  @action = action_str
  self
end

#with_attributes(attrs) ⇒ self

Constrains the match to streams that have all of the given HTML attributes. Keys and values are compared as strings.

Examples:

expect(response).to have_turbo_stream.with_attributes("data-controller" => "messages")

Parameters:

  • attrs (Hash)

Returns:

  • (self)


90
91
92
93
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 90

def with_attributes(attrs)
  @attributes = attrs.transform_keys(&:to_s).transform_values(&: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)


71
72
73
74
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 71

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