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

xpath-react

v6.1.0

Published

A React adapter for xpath-evaluator.

Downloads

30

Readme

xpath-react

Build Status Code Climate Test Coverage

xpath-react is a library that allows you to query the virtual DOM of React with XPath 1.0 expressions and thus testing it without a real DOM.

Table of Contents

Installation

The package can be installed with npm.

$ npm install xpath-react

React is a peer dependency and is expected to be available. The library is compatible with both react-15.5.x and react-16.x.x.

$ npm install react

Usage

The library provides a basic evaluate method that you might commonly know as Document.evaluate. Additionally, a convenience method exist to make usage more practical, as evaluate is a bit cumbersome. A practical example can be found in example/.

evaluate()

XPathResult evaluate (
  DOMString expression,
  ReactElement contextNode,
  XPathNSResolver resolver,
  XPathResultType type,
  XPathResult result
)

This method works in the same way as Document.evaluate, but accepts a ReactElement instead of a Node.

Example

import { evaluate, XPathResult } from "xpath-react";

const Foo = (
  <ul>
    <li>bar</li>
    <li>baz</li>
  </ul>
);

const result = evaluate("string(.//li[1])", Foo, null, XPathResult.STRING_TYPE);

result.stringValue; // => "bar"

find()

(ReactElement | String | Number | Boolean) find (
  ReactElement element,
  DOMString expression
)

Finds React elements inside another element. It can also be used to query for string, number and boolean values (e.g. attributes, element counts and conditions, respectively).

Example
import ShallowRenderer from "react-test-renderer/shallow";

import { find } from "xpath-react";

function shallow (component) {
  const renderer = new ShallowRenderer();
  renderer.render(component);
  return renderer.getRenderOutput();
}

class Foo extends React.Component {
  render() {
    return (
      <div>
        <p>Hello world!</p>
      </div>
    );
  }
}

const output = shallow(<Foo />);

find(output, ".//p"); // => ReactElement { type: "p", ... }
find(output, "string(.//p)"); // => "Hello world!"
find(output, "count(.//p)"); // => 1
find(output, "count(.//p) = 1"); // => true

You can also use it to assert presence of unrendered child components, as shown below. This assumes that the child component has a displayName property.

class Bar extends React.Component {
  render() {
    return (
      <div>
        <Foo />
      </div>
    );
  }
}

const output = shallow(<Bar />);

find(output, "count(.//Foo)"); // => 1

Known issues & limitations

  • An abstract document node is created internally around the context node provided by the user. This can however not be returned, meaning that the following XPath expression would yield an error.

    /self::node()
  • Namespaces are not supported because JSX does not support them.