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-drivers

v3.0.355

Published

testing utility lib

Downloads

1,164

Readme

TDD Approved

Enzyme Drivers

Say What??

Enzyme Drivers is a library that will help you write enzyme-based tests in a way that is much cleaner and better organized than you might without it.

Note: Do you write your apps in angular? No problem, we've got you covered. Check out turnerjs, which implements a similar concept for angular components.

But, Why??

Enzyme is a great tool for writing component tests for React components. The only problem is the amount of bolierplate involved...especially once you try to write tests for React Native components. (You are testing your React Native app, right? Right.) That's because you can't render anything that includes native binaries in node, so any component that relies on native code must use mocks. Lots of them.

But you know you must test, so what can you do? Enzyme Drivers to the resuce!

Enzyme Drivers introduces a new way of organazing your enzyme tests, using test drivers. Of course, the concept is not really new. Test drivers have been with us for a long time now and we like them a lot. Now we have them for React components as well.

Not sure what a test driver is for? Think of it as a cool way to organize your tests and make them more readable than ever. How, you ask? Read on...

How

First, let's install:

npm install enzyme-drivers --save-dev

Now, let's look at some examples:

Consider the following React Native component:

export default function DummyReactNativeComponent({text, onTap}) {
  return (
    <View>
      <Text testID="myText">Some Text</Text>
      <Text testID="textFromProp" onPress={onTap}>{text}</Text>
    </View>
  );
}

We can test it using Enzyme Drivers:

it('should render component', () => {
  //given
  driver.render({text: 'It works!'});

  //then
  expect(driver.text).toBe('It works!');
});

Looks neat, right? The test is very readable and clear. Let's write another one. Let's test that tap is working:

it('should tapOn', () => {
  const tapSpy = jasmine.createSpy('tap');
  //given
  driver.render({text: 'yoba', onTap: tapSpy});
  //when
  driver.tap();
  //then
  expect(tapSpy).toHaveBeenCalled();
});

What's driver? I'm glad you asked.

// setup the driver in the beforeEach
beforeEach(() => {
  driver = new MyDriver({
    //the path is relative to the src/ folder (you can change it if you need. see api section)
    path: 'components/dummy-react-native-component',
    mocks: {},
  });
});

//The driver extends the React Native Driver. We also support regular react with BaseDriver
class MyDriver extends RNDriver {
  get text() {
    return this.childrenOf('textFromProp');
  }

  tap() {
    this.tapOn('textFromProp');
  }
}

That's it. Now checkout the full api.

API

For a better looking API check out our tests ;)

| Method | Parameters | Comment | |-------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | constructor | options: object | path: string relative path from the project root/src folder mocks: object - set of key / value. The key is the import you want to mock and the value is the mocked value isRelativePathFromRoot: boolean / optional / default to true rootFolder: string / optional / default to 'src/' targetImport default to 'default' commonjs: boolean / optional / support commonjs exports (module.exports = MyComponent) | | byId | testId: string | returns enzyme element by testID property | | childrenOf | testId: string | returns .props().children of the given element | | propsOf | testId: string | returns .props() of the given element | | tapOn | testId: string | react native only: simulate press on the given element | | stylesById | testId: string | react native only: returns a merged object of all of the styles on the given element | | state | | returns the component's state | | setState | | sets the component's state |