Class: Ruact::Testing::ComponentQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/ruact/testing/component_query.rb

Overview

Resolves "was component rendered, and with what props?" out of a decoded Flight tree. Pure — no RSpec, no I/O. The public have_ruact_component matcher is a thin RSpec wrapper over this.

A component is TWO wire rows (Story 7.5 / FR108 semantics):

* an `:import` row — `["<module path>", "<export name>", [chunks…]]` —
sourced from the client manifest (`reference_for`); its `:id` is the
integer the model rows reference; AND
* one or more `:model` rows carrying a React element
`["$", "$L<hex id>", key, {props}]` whose type references that import.

have_ruact_component("PostList") matches the import row's DECLARED name (D3): its export name OR its module basename (/PostList.jsxPostList) — honest to what the wire actually contains, not the ERB tag or a mapping the wire does not carry. Props are read from the model element's 4th slot in their SERIALIZED wire form (D4: string keys, serialized values).

Constant Summary collapse

ELEMENT_HEAD =

A React element on the wire is a 4-tuple whose head is the sentinel "$".

"$"
CLIENT_REF =

A client-component element's type is "$L" followed by the import's hex id.

/\A\$L(?<hex>\h+)\z/

Instance Method Summary collapse

Constructor Details

#initialize(wire) ⇒ ComponentQuery

Returns a new instance of ComponentQuery.

Parameters:

  • wire (String)

    the raw Flight wire byte string.



31
32
33
34
35
# File 'lib/ruact/testing/component_query.rb', line 31

def initialize(wire)
  @rows = FlightWireParser.parse(wire)
  @imports = index_imports(@rows)
  @instances = collect_instances(@rows)
end

Instance Method Details

#props_for(name) ⇒ Array<Hash>

Returns the serialized props hash of every rendered instance of name, in wire order. Empty when name was not rendered.

Returns:

  • (Array<Hash>)

    the serialized props hash of every rendered instance of name, in wire order. Empty when name was not rendered.



44
45
46
47
48
49
# File 'lib/ruact/testing/component_query.rb', line 44

def props_for(name)
  ids = import_ids_for(name)
  return [] if ids.empty?

  @instances.select { |inst| ids.include?(inst[:ref_id]) }.map { |inst| inst[:props] }
end

#rendered?(name) ⇒ Boolean

Returns whether any instance of name was rendered.

Returns:

  • (Boolean)

    whether any instance of name was rendered.



38
39
40
# File 'lib/ruact/testing/component_query.rb', line 38

def rendered?(name)
  !props_for(name).empty?
end

#rendered_namesArray<String>

Returns every distinct component name present in the wire (by declared export name), for a legible failure message.

Returns:

  • (Array<String>)

    every distinct component name present in the wire (by declared export name), for a legible failure message.



53
54
55
56
# File 'lib/ruact/testing/component_query.rb', line 53

def rendered_names
  rendered_ids = @instances.map { |inst| inst[:ref_id] }.uniq
  @imports.slice(*rendered_ids).map { |_, meta| meta[:export_name] }.uniq
end