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

@oskarer/enzyme-wait

v1.5.0

Published

Wait for an async element to appear when performing integration tests with enzyme.

Downloads

40

Readme

enzyme-wait

Wait for an async element to appear when performing integration tests with enzyme. Returns a promise which resolves with the root component you performed your search on.

NEW: There is now a fully working example using both Promises and async/await.

How to use:

createWaitForElement(
    enzymeSelector,
    /*Optional*/ timeOut,
    /*Optional*/ intervalDuration
)(componentToSearchOn)
    .then(/* ... */)
    .catch(/* ... */)

Example Usage (Promises):

import React from 'react';
import { mount } from 'enzyme'
import { createWaitForElement } from 'enzyme-wait';

/**
 * The component you want to test. Assume it displays
 * the string "ready" after performing some async action
 * which takes time.
 */
import SampleComponent from '...';

const waitForSample = createWaitForElement('#sample-ready');

const component = mount(<SampleComponent />);

it('displays ready once it is ready', ()=> {
    waitForSample(component)
        .then( component => expect(component.text()).to.include('ready') );
});

Example Usage (async/await)

The same as above but using async/await instead of Promises:

it('displays ready once it is ready', async ()=> {
    const componentReady = await waitForSample(component);
    expect(componentReady.text()).to.include('ready');
});

Chaining promises

If you have multiple async actions happening, just make sure to always return a Promise which resolves with the root component. This way you can create nice looking chains and avoid callback hell.

Example:

const component = mount(<SampleComponent />);

it('displays ready after multiple interactions', ()=> {
    createWaitForElement('#sample-ready')(component)
        .then( /* do something and return a resolved promise with the comp */ )
        .then( /* do something and return a resolved promise with the comp */ )
        .then( createWaitForElement('#another-component-ready') )
        .then( component => expect(component.text().to.include('ready') );
});

Checking out the example repo

There is now a working example inside this repo using both the Promise-approach as well as the async/await-approach.

The example uses Jest, but it should work with any test framework. In other frameworks you might need to call done() on asynchronous tests.

To play around with this example you can:

  1. clone this repo
  2. run npm install && npm run dist on the root repo (this is required to create a lib version of this package which is listed in the example's dependencies )
  3. go to the example folder cd example
  4. in there, run npm install && npm start
  5. open your browser at http://localhost:9000 to see the example or run npm test to see the tests working.