Class: TurboRspec::Matchers::HaveTurboStream
- Inherits:
-
Object
- Object
- TurboRspec::Matchers::HaveTurboStream
- Defined in:
- lib/turbo_rspec/matchers/have_turbo_stream.rb
Overview
RSpec matcher for asserting that a response body contains a
+
Instance Method Summary collapse
-
#at_least(n) ⇒ self
Asserts at least +n+ matching streams.
-
#at_most(n) ⇒ self
Asserts at most +n+ matching streams.
-
#children_only ⇒ self
Constrains the match to morph streams with the +children-only+ attribute set.
- #description ⇒ String
- #does_not_match?(response_or_body) ⇒ Boolean
-
#exactly(n) ⇒ self
Asserts exactly +n+ matching streams.
- #failure_message ⇒ String
- #failure_message_when_negated ⇒ String
-
#initialize ⇒ HaveTurboStream
constructor
A new instance of HaveTurboStream.
- #matches?(response_or_body) ⇒ Boolean
-
#once ⇒ self
Asserts exactly one matching stream.
-
#rendering(partial) ⇒ self
Constrains the match to streams whose rendered HTML includes the given partial path.
-
#targeting(dom_id) ⇒ self
Constrains the match to streams targeting a specific DOM id.
-
#targeting_all(selector) ⇒ self
Constrains the match to streams targeting a CSS selector (the +targets+ attribute).
-
#times ⇒ self
Fluent terminator so +.exactly(2).times+ reads naturally.
-
#twice ⇒ self
Asserts exactly two matching streams.
-
#with_action(action) ⇒ self
Constrains the match to streams with the given action.
-
#with_attributes(attrs) ⇒ self
Constrains the match to streams that have all of the given HTML attributes.
-
#with_content(text) ⇒ self
Constrains the match to streams whose template content includes the given text.
Constructor Details
#initialize ⇒ HaveTurboStream
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.
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.
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_only ⇒ self
Constrains the match to morph streams with the +children-only+ attribute set.
99 100 101 102 |
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 99 def children_only @children_only = true self end |
#description ⇒ 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
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.
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_message ⇒ String
165 166 167 |
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 165 def "expected response to contain a turbo stream#{constraint_description}#{count_description}\n#{}" end |
#failure_message_when_negated ⇒ String
170 171 172 |
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 170 def "expected response not to contain a turbo stream#{constraint_description}#{count_description}" end |
#matches?(response_or_body) ⇒ 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 |
#once ⇒ self
Asserts exactly one matching stream.
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.
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.
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).
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 |
#times ⇒ self
Fluent terminator so +.exactly(2).times+ reads naturally.
145 146 147 |
# File 'lib/turbo_rspec/matchers/have_turbo_stream.rb', line 145 def times self end |
#twice ⇒ self
Asserts exactly two matching streams.
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.
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.
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.
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 |