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

@commercetools/enzyme-extensions

v5.0.0

Published

Enzyme extensions tailored at improving dealing with shallowly rendered wrappers

Downloads

6,130

Readme

NOTE This package used to provide a renderProp test helper, which is now part of enzyme itself as of v3.8.0 🎈.

We therefore dropped renderProp in v4.0.0 of this package. We recommend to use renderProp from enzyme itself instead.

Be aware that the API has changed while moving the function to enzyme.

// before
wrapper.renderProp('foo', 10, 20);
// after
wrapper.renderProp('foo')(10, 20);

We are happy that our little helper has made it into enzyme.

What assumptions is this built with?

  • We like to shallow render and avoid mounting
    • 🤺 Shallow rendering is fast and ensures that you only interact with the unit under test
    • 🏙 Shallow rendering ensures that you do not snapshot past your test's concern
    • 🏎 Shallow rendering has shown to be more performant for us than mounting
  • We like declarative components and Render Props
    • 🧠 We can compose components easily while following along their interactions
    • 🔪 We like stubbing to test individual pieces of logic

Installation

1. Add package

yarn add @commercetools/enzyme-extensions -D

2. Add a test setup file (test runner dependent)

For Jest you would set up a setupTestFrameworkScriptFile. Create that file and add it to the jest configuration.

3. Extend Enzyme with this package's helpers

In that testFrameworkScriptFile file, import the extensions and add them to Enzyme

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-xx';
import configure from '@commercetools/enzyme-extensions';
import ShallowWrapper from 'enzyme/ShallowWrapper';

// You likely had this part already
Enzyme.configure({ adapter: new Adapter() });

// This is the actual integration.
// Behind the scenes this extends the prototype of the passed in `ShallowWrapper`
configure(ShallowWrapper);

Usage

Once set up, you can use the extension in your test files like this:

import React from 'react';
import { shallow } from 'enzyme';

describe('when rendering `<App>`', () => {
  const App = () => (
    <div id="app">
      <Mouse
        render={(x, y) => (
          <div>
            Cursor is at {x} {y}
          </div>
        )}
      />
    </div>
  );

  // Here we call the render function defined on Mouse and we provide
  // some custom arguments to it. This means we are effectively mocking
  // the Mouse component's implementation.
  // This is great to keep test concerns separate.
  const wrapper = shallow(<App />)
    .find(Mouse)
    // This is where we are actually using the drill function
    // Since we defined it on the prototype in the Installation step,
    // it does not need to be imported into the test itself.
    // We can call any property dynamically and even derive the property to
    // call depending on the props which are passed as the arguments of the
    // function passed to `drill`.
    .drill(props => props.render(10, 20));

  it('should render the mouse position', () => {
    expect(wrapper.equals(<div>Cursor is at 10 20</div>)).toBe(true);
  });
});

Enzyme's renderProp is built as an easy to use test helper for the most common cases. In case you need more control, you can use drill instead. drill offers more flexibility as:

  • the prop-to-call can be derived from the other props
  • the returned element can be set dynamically

See the drill documentation for more.

Documentation