Class: Phlex::Reactive::TestHelpers::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/phlex/reactive/test_helpers.rb

Overview

The action's outcome, in the same terms the endpoint reasons about. Wraps the action's RETURN VALUE:

* a Phlex::Reactive::Response — honored explicitly (replace/remove/
redirect derived from it, exposed via #response).
* anything else — the legacy contract (return value ignored by the
endpoint, which falls back to the single self-replace). So a legacy
action reports replace? true and #response nil.

#streams reproduces the endpoint's actor streams (issue #110) — including the token-refresh injection response_streams performs — so a matcher can assert on exactly what the client would receive.

Constant Summary collapse

SELF_RENDER_ACTIONS =

A stream re-renders component's root (so its fresh token counts as the refresh) iff its action is one of these — never append/prepend, which insert children carrying their OWN token (issue #44). Same set the endpoint's carries_token_for? uses.

%w[replace update reactive:token].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(returned:, component:) ⇒ Result

Returns a new instance of Result.



222
223
224
225
226
# File 'lib/phlex/reactive/test_helpers.rb', line 222

def initialize(returned:, component:)
  @returned = returned
  @component = component
  @response = returned if returned.is_a?(Phlex::Reactive::Response)
end

Instance Attribute Details

#componentObject (readonly)

The component the action actually ran against — the instance REBUILT from the round-tripped identity, NOT the one passed to run_reactive (the endpoint always acts on a rebuilt instance). Read its ivars to assert state-backed changes: run_reactive(c, :set, count: "42").component.



214
215
216
# File 'lib/phlex/reactive/test_helpers.rb', line 214

def component
  @component
end

#responseObject (readonly)

The Response the action returned, or nil when it returned a legacy (non-Response) value.



208
209
210
# File 'lib/phlex/reactive/test_helpers.rb', line 208

def response
  @response
end

Instance Method Details

#redirect?Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/phlex/reactive/test_helpers.rb', line 250

def redirect?
  !@response.nil? && @response.redirect?
end

#redirect_urlObject



254
255
256
# File 'lib/phlex/reactive/test_helpers.rb', line 254

def redirect_url
  @response&.redirect_url
end

#remove?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/phlex/reactive/test_helpers.rb', line 237

def remove?
  return false if @response.nil?

  # Check the OPENING <turbo-stream> tag, not the whole string: a rendered
  # body that happens to contain the literal `action="remove"` (a quoted
  # code sample, say) must not false-positive. Same parse the token check
  # and the matchers use.
  @response.streams.any? do
    open_tag = it[/<turbo-stream\b[^>]*>/] || ""
    open_tag.include?(%(action="remove"))
  end
end

#replace?Boolean

A legacy return (no Response) is the implicit single self-replace. A Response replaces unless it removes or redirects (render_self? is false for those, and for a .streams/.with that opts out).

Returns:

  • (Boolean)


231
232
233
234
235
# File 'lib/phlex/reactive/test_helpers.rb', line 231

def replace?
  return true if @response.nil?

  @response.render_self?
end

#streamsObject

The actor's turbo-stream strings, exactly as the endpoint would render them (response_streams) — a legacy return is the single self-replace; a Response is honored, with the same token-refresh streams appended.



261
262
263
# File 'lib/phlex/reactive/test_helpers.rb', line 261

def streams
  @streams ||= build_streams
end