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

enzyme-selectors

v0.2.0

Published

Other testing libraries like [react-testing-library](https://github.com/testing-library/react-testing-library) have popularized testing react-components by locating them by specific html-attributes that enforce best pactices for accessibility. This module

Downloads

1

Readme

Enzyme Selectors

Other testing libraries like react-testing-library have popularized testing react-components by locating them by specific html-attributes that enforce best pactices for accessibility. This module brings the convenience of those selectors to enzyme ✨.

Setup

Install it by running:

yarn add -D enzyme-selectors

Or if using npm instead of yarn:

npm i -D enzyme-selectors

After installation, you should configure the selectors. If you already have enzyme configured this will be easy (if you haven't configured enzyme yet, follow their installation guide before continuing). In the setup file where you configure Enzyme you can add the following lines:

   const Enzyme = require("enzyme");
   const Adapter = require("enzyme-adapter-react-16");
+  const { configure } = require("enzyme-selectors");

   Enzyme.configure({
     adapter: new Adapter()
   });

+  configure();

Typescript

This module also includes it's own types, but since the original enzyme types are modified, you have to let typescript know how to add these types.

You can do this by by either putting the following line in a test file:

+ import 'enzyme-selectors';

or by putting:

+ /// <reference types="enzyme-selectors" />

in a global declaration file that you already use.

That's it, You are now ready to start using the selectors in your tests 🎉.

Selectors

All selectors apply to mount as well as shallow

After installing and configuring the following selectors will be added to enzyme:

findByTestId

Find components by their data-testid value

Params:

id: string

Example:

const component = mount(
  <div>
    <div data-testid="foo">foo</div>
  </div>
);

component.findByTestId("foo");

findByAriaLabel

Find components by their aria-label value

Params:

label: string

Example:

const component = mount(
  <div>
    <button aria-label="Close">&times;</button>
  </div>
);

component.findByAriaLabel("Close");

findByPlaceholderText

Find components by their placeholder value

Params:

text: string

Example:

const component = mount(
  <div>
    <input type="text" placeholder="Name" />
  </div>
);

component.findByPlaceholderText("Name");

findByAltText

Find components by their alt value

Params:

text: string

Example:

const component = mount(
  <div>
    <img alt="foo" />
  </div>
);

component.findByAltText("foo");

findByTitle

Find components by their title value

Params:

title: string

Example:

const component = mount(
  <div>
    <a href="https://..." title="foo">
      Go
    </a>
  </div>
);

component.findByTitle("foo"); // returns <a href="https://..." title="foo">Go</a>

findByRole

Find components by their role value

Params:

role: string

Example:

const component = mount(
  <div>
    <button role="Close">&times;</button>
  </div>
);

component.findByRole("Close"); // returns <button role="Close">&times;</button>

Utilities

All utilities apply to mount as well as shallow

This module also adds a couple of useful utilities that match the added selectors.

debugByAltText

Debug components with an alt text

Params:

query: string

debugByAltText

Debug components with an alt text

Params:

query: string

debugByAttribute

Debug components by attribute (values)

Params:

attribute: string

value: string

debugByPlaceholderText

Debug components with a placeholder

Params:

query: string

debugByRole

Debug components with a role attribute

Params:

query: string

debugByTestId

Debug components with a data-testid attribute

Params:

query: string

debugByTitle

Debug components with a title attribute

Params:

query: string