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

react-refract

v0.0.3

Published

A wrapper for React Components/Enzyme shallow that allows testing of lifecycle events and component instances with react 16.0.0-alpha-12 (or other as of this date unsupported versions). Works for react native shallow testing as well.

Downloads

5

Readme

Refract

A module designed to make it a bit easier to access the instance of the component you're rendering for tests.

We're currently developing in React Native 0.46.x and they have a dependency on React 16 in alpha (or beta). We like to test our components using Enzyme (airbnb/enzyme), but they currently don't support React 16 as it's not in a stable release yet.

It works for the most part, but we've faced a problem accessing the underlying instance of the component when doing shallow rendering. (instance is undefined)

This package provides a workaround, by wrapping the component you want to test, and exposing the instance to you.

To use:

import Refract from 'react-refract';

import { Component } from './component.js'; // Component under test

test('Component', () => {
    // Define the props for your <Component />
    const props = { ... };

    let {wrapper, instance} = Refract(Component, props); // This is instead of doing shallow(<Component ...props/>
    let self = instance(); // Access the instance after shallow rendering, like in Enzyme

    expect(wrapper).toBeDefined();
    expect(self).toBeDefined();

    self.componentDidMount();
    expect(...).toBe(...); // Test on whatever goes on in componentDidMount...

    wrapper.find('Button[title="Click!"]').simulate('press'); // Simulate works as usual
});

You can also use the underlying Wrap function and just use shallow from enzyme if you prefer to mount your props the old fashioned way:

import { Wrap } from 'react-refract';
import { shallow } from 'enzyme';

import { Component } from './component.js'; // Component under test

test('Shallow rendering the Refracted component with enzyme manually works as well', () => {
    let { Refracted, instance } = Wrap(TestScreen);

    const props = { ... };
    let wrapper = shallow(<Refracted {...props} />); // Usual rendering of the component

    expect(wrapper).toBeDefined();

    let self = instance(); // Call this after rendering (the instance is not assigned before render is called on the component)
    expect(self).toBeDefined();

    self.componentDidMount();
    expect(...).toBe(...); // Test on whatever goes on in componentDidMount...

    wrapper.find('Button[title="Click!"]').simulate('press'); // use the enzyme wrapper as usual...
});

Enjoy!

Thanks

Thanks to the guys at airbnb/enzyme. We can't wait for further support for the react-native workflow.