Element
An Element
is a wrapper around a React element (more specifically a
test instance) that hides implementation details and
is primarily used for traversing the React tree. It can be accessed via the root
or when finding/querying through another Element
instance.
debug()
#
Element#debug(options?: DebugOptions): string
Like the rendered result debug()
but only represents the current element
tree.
dispatch()
#
Element#dispatch(name: EventType, event?: React.SyntheticEvent | EventOptions, options?: DispatchOptions): this
Dispatch an event for the defined event handler. Accepts a SyntheticEvent
, an event options
object, or nothing (will create an event behind the scenes). To ease integration, Rut provides a
mockSyntheticEvent()
function.
This may only be executed on host components (DOM elements). Why? Because it's an abstraction that forces testing on what the consumer will ultimately interact with. Executing handlers on a React component is a code smell.
#
Optionspropagate
(boolean
) - Propagate the event up or down the tree by executing the same handler on every element until hitting the root, leaf, or the event has been stopped. (Experimental)
dispatchAndWait()
#
async Element#dispatchAndWait(name: EventType, event?: React.SyntheticEvent | EventOptions, options?: DispatchOptions): Promise<void>
Like dispatch()
but waits for async calls within the dispatch and updating phase to
complete before returning the re-rendered result. Because of this, the function must be await
ed.
find()
#
Element#find<T extends HostComponentType, P extends InferComponentProps<T>>(type: T, props?: Partial<P>): Element<T>[]
Element#find<T extends React.ComponentType, P extends InferComponentProps<T>>(type: T, props?: Partial<P>): Element<T>[]
Search through the current tree for all elements that match the defined React component or HTML tag.
If any are found, a list of Element
s is returned.
A caveat exists for this method.
Also accepts a partial props object as a 2nd argument. When defined, will further filter elements and only return those that have the defined props in common.
findAt()
#
Element#findAt<T extends HostComponentType, P extends InferComponentProps<T>>(type: T, at: 'first' | 'last' | number, props?: Partial<P>): Element<T>
Element#findAt<T extends React.ComponentType, P extends InferComponentProps<T>>(type: T, at: 'first' | 'last' | number, props?: Partial<P>): Element<T>
Like find()
but returns the element at the defined index. Accepts shorthand first
and
last
indices, or a numerical index. If no element is found, an error is thrown.
findOne()
#
Element#findOne<T extends HostComponentType, P extends InferComponentProps<T>>(type: T, props?: Partial<P>): Element<T>
Element#findOne<T extends React.ComponentType, P extends InferComponentProps<T>>(type: T, props?: Partial<P>): Element<T>
Like find()
but only returns a single instance. If no elements are found, or too many
elements are found, an error is thrown.
name()
#
Element#name(jsx: boolean = false): string
Returns the name of the component (most commonly from displayName
). If a component has been
wrapped with an HOC, it will attempt to preserve the name.
query()
#
Element#query<T extends ElementType>(predicate: Predicate | ((node: TestNode, fiber: FiberNode) => boolean), options?: QueryOptions): Element<T>[]
A low-level abstraction for querying and finding components in the current tree using a predicate
function. This predicate is passed the react-rest-renderer
test instance and a react
fiber node,
for use in comparisons. To simplify this process, a predicate can be used.
#
Optionsdeep
(boolean
) - Continue searching through the entire tree when a match is found, otherwise return the found result immediately. Defaults totrue
.
ref()
#
Element#ref<T>(name?: string): T | null
Returns any ref associated with the current component. The renderer will attempt to find a valid ref using the following patterns, in order:
- If a ref is found on the internal React fiber node, it will be used.
- If defined as a class component instance property (either via
React.createRef()
or a callback ref), will match against thename
provided. - If defined as a string ref, will match against the
name
provided. - Otherwise
null
is returned.
Be sure to mock your ref using the
mockRef()
option.