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

test-drive-react

v1.4.0

Published

Opinionated library for Test-Driven Development of React Components.

Downloads

178

Readme

test-drive-react

npm version Build Status

Opinionated library for Test-Driven Development of React components, extending Test Drive and providing its DOM parts lookup, presence/absence matchers and event triggering layout matchers.

In addition, it reexports React simulate testing utility and integrated renderer

ClientRenderer

ClientRenderer provides a utility for rendering React components in consistent and convenient way. It creates the holding container, if necessary, with uniform positioning, automatically binds to it all important Test Drive helper functions, and proivides clean-up mechanism.

For a typical use, see the end-to-end test.

The renderer is created simply by invoking new ClientRenderer().

render(element, container?)

Renders the element React component. If container is not specified, a new one is created. Returns RenderingContext with following fields:

  • container
  • result - rendered root component (either DOM Element or React component instance)
  • select - DOM selector pre-bound to the container

cleanup()

Unmounts the root component and removes any container that had been created by the renderer.

Component Drivers

Test Drive React provides additional layer of abstraction over the basic assertion tools. In order to keep the intended behavior of a component separated from the actual implementation (DOM structure, particular DOM events, etc.), every component should provide it's "driver". Component driver translates meaningful actions and getters into specific DOM details.

All drivers should extend the DriverBase class. The basic (and recommended) way of creating drivers is to use .withDriver(DriverClass) method which is part of the RenderingContext interface.

For example, consider a TestComponent. There should be always relevant implementation of its driver, e.g.:

export class TestComponentDriver extends DriverBase<HTMLElement> {
  static ComponentClass = TestComponent;

  get samplePart() {
    return this.ensuredSelect('SAMPLE_PART') as HTMLDivElement;
  }

  doAction() {
    this.root.click();
  }
}

The ComponentClass points to the component for which is the driver relevant. It is used by the ClientRenderer for validation of a component/driver match during the rendering. (This validation is intended to prevent using a wrong driver on top of a DOM generated by a component.)

The getter .samplePart provides access (via data-automation-id) to specific parts of the component's shadow DOM, while keeping this detail encapsulated. Similarly, the .doAction() method represents specific methods, while keeping the technicalities (event type) private.

The driver then should be instantiated and used through ClientRenderer, e.g.:

const { driver } = clientRenderer.render(<TestComponent />).withDriver(TestComponentDriver);
expect(driver.samplePart).to.be.present();

Note that the .withDriver() function returns RenderingContextWithDriver, which has following members:

  • driver - instance of the component driver

In the case of composite components, the drivers should mirror their structure as well. .samplePart in the above example should, therefore, reference another (relevant) component driver, rather than plain DOM Element, if it corresponds to custom component.

License

MIT