npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-testutils-query

v0.10.0

Published

Utility functions for accessing elements within a React Virtual-DOM tree.

Downloads

8

Readme

react-testutils-query

Utility functions for accessing elements within a React Virtual-DOM tree.

Installation

npm install --save-dev react-testutils-query

Usage


import React from 'react';
import assert from 'assert';
import $ from 'react-testutils-query';

const form = $(
  <form>

    <div id="fav-color">
      <label>Favourite color:</label>
      <ol className="list list--color">
        <li className="list__item">
          <label><input type="radio" name="color" value="red"/> Red</label>
        </li>
        <li className="list__item">
          <label><input type="radio" name="color" value="green"/> Green</label>
        </li>
        <li className="list__item">
          <label><input type="radio" name="color" value="blue" defaultChecked/> Blue</label>
        </li>
      </ol>
    </div>

    <button className="button button--primary" disabled>Use now!</button>

  </form>
);

form.find('li')
  .each(element => {
    assert(element.hasClass('list__item'));
    assert(element.hasText(/Red|Green|Blue/));
  })
;

assert(form.find('button').hasProp('disabled'));

API

$(element : object) : Element

Wrap a react element with methods for querying.

Parameters:

  • element : object - The React element to wrap

Returns:

A wrapper element.

$(elements : Array<object>) : ElementCollection

Wrap an array of react elements with methods for querying.

Parameters:

  • elements : Array<object> - The React elements to wrap

Returns:

A collection of wrapper elements.

$(selector, elements) : ElementCollection

Get the descendant elements of the element(s), filtered by selector.

This method is an alias for $(elements).find(selector)

Parameters:

  • selector : string|React.Component - A CSS-like selector or React component
  • elements : object|Array<object> - The React element(s)

Returns:

A collection of wrapper elements.

Element

.find(selector = '*') : ElementCollection

Get the descendant elements of the element, filtered by selector.

Parameters:

  • selector : string|ReactComponent - A CSS-like selector or React component

    Supports:

    • *
    • tag
    • #id
    • .class
    • [attr][attr=value]

Returns:

A collection of wrapper elements.

.children(selector = '*') : ElementCollection

Get the children elements of the element, filtered by selector.

Parameters:

  • selector : string|ReactComponent - A CSS-like selector or React component

    Supports:

    • *
    • tag
    • #id
    • .class
    • [attr][attr=value]

Returns:

A collection of wrapper elements.

.prop(name : string) : *

Get the value of a property on the element.

Returns:

The prop value if one exists, or undefined.

.classes() : Array<string>

Get a list of class names applied to the element.

Returns:

An array of class names.

.text() : string

Get the text the element (like .textContent).

Returns:

A string of text.

.html() : string

Get the children elements as a HTML string (like .innerHTML).

Returns:

A string of text.

.toString() : string

Get the element as a HTML string (like .outerHTML).

Returns:

A HTML string.

.type() : string

.key() : string

.hasKey(key) : boolean

.ref() : string

.props() : object

.hasProp(name: string, [value: *]) : string

.hasClass(name: string) : string

.hasText(value: string|RegExp) : string

ElementCollection

.first() : Element

Get the first element in the collection.

Returns:

The first element in the collection.

Throws:

If the collection is empty.

.last() : Element

Get the last element in the collection.

Returns:

The last element in the collection.

Throws:

If the collection is empty.

.at(index) : Element

Get the element at the specified index in the collection.

Parameters:

  • index : number - The index

Returns:

An element.

Throws:

If the index is out of bounds.

.find(selector = '*') : ElementCollection

Get the descendant elements of every element in the collection, filtered by selector.

Parameters:

  • selector : string|ReactComponent - A CSS-like selector or React component

    Supports:

    • *
    • tag
    • #id
    • .class
    • [attr][attr=value]

Returns:

A collection of wrapper elements.

.children(selector = '*') : ElementCollection

Get the children elements of every element in the collection, filtered by selector.

Parameters:

  • selector : string|ReactComponent - A CSS-like selector or React component

    Supports:

    • *
    • tag
    • #id
    • .class
    • [attr][attr=value]

Returns:

A collection of wrapper elements.

.classes() : Array<string>

Get a list of class names applied to every element in the collection.

Returns:

An array of unique class names.

.text() : string

Get the text from all the elements in the collection (like .textContent).

Returns:

A string of text.

.toString() : string

Get all the elements in the collection as a HTML string (like .outerHTML).

Returns:

A HTML string.

.toArray() : Array<Element>

Get all the elements in the collection as an array of elements.

Returns:

An array of elements.

Change log

0.10.0

  • changed $() to:
    • return an Element when a single element is passed (as per 0.6.x behaviour)
    • return an ElementCollection when multiple elements are passed (as per 0.8-9 behaviour)

0.9.1

  • added missing ElementCollection.classes()

0.9.0

  • added a notification when the provided selector is undefined (happens frequently when the passed thing is not a React component)
  • removed methods from ElementCollection which just proxy to individual Elements (prop(), hasProp(), classes(), hasClass()) because they're confusing and result in incorrect results when you've assumed they work on all the elements!
  • changed .text() to return the tex of all the Elements in the collection

0.8.0

  • added support for children selectors e.g. [disabled]
  • changed $() to also take an array of elements
  • improved documentation

0.7.0

  • added .children() method
  • modified .toString() to return a string representation of the element itself

To do

  • multi-level selectors e.g. .class-1 .class-2
  • other jQuery methods

License

The MIT License (MIT)

Copyright (c) 2016 James Newell