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-native-test-render-counter

v1.0.3

Published

Testing utility to track components render count in React Native applications

Downloads

24

Readme

react-native-test-render-counter

Testing utility to track components render count in React Native applications

Proposal

Unnecessary Component re-renders is one of the most impactful performance issue in React Native applications, because most of the time it results in visible flicker, or UI not responding. With the rise of React hooks, now it's easy to make not optimized hooks, i.e. useSelector, or just misuse them, which result in multiple re-renders that could be avoided.

It's not hard to fix those performance problems, but it's difficult not to break these optimisations later, since there is nothing preventing a developer from doing so and in most cases performance degradation is not intended and is not noticed when running the app on a simulator.

The general idea is to optimize rendering in your application and then "lock" render count in unit tests, so if a developer makes changes which impact rendering count, whether it was intended or not, it will notify with a failing test. With such tests we can make sure our rendering performance is not degrading while refactoring and/or developing features.

Prerequisites

  • React Native in typescript
  • ts-jest
  • react-component-driver

Test Example:

Let's say we have a Post component, which renders content and like button. So if we wanted to test render count during press on like action, then ideally like button should render two times, but the content only once.

import {startRenderCounter, stopRenderCounter} from 'react-native-test-render-counter';

it('should validate render count during like action', async () => {
    const {getRenderCountByTestID} = startRenderCounter();
    const component = driver().render();

    expect(getRenderCountByTestID(testIDs.POST_CONTENT)).toEqual(1);
    expect(getRenderCountByTestID(testIDs.POST_LIKE_BUTTON)).toEqual(1);

    component.getPostAt(0).pressLike();

    expect(getRenderCountByTestID(testIDs.POST_CONTENT)).toEqual(1);
    expect(getRenderCountByTestID(testIDs.POST_LIKE_BUTTON)).toEqual(2);

    stopRenderCounter();
});

Jest Config:

In order to track render count with testID prop, we need to make sure each react element is rendered once, i.e. rendered shallowly so it does not have any more children with the same name. We need to mock all react-native components that are used for render count:

const React = require('react');

function mockComponent(name) {
  return class extends React.PureComponent {
    static displayName = name;
    render() {
      return React.createElement(name, this.props);
    }
  };
}

jest.mock('react-native', () => {
  const UI = jest.requireActual('react-native');

  const mocks = {
    View: mockComponent('View'),
    Button: mockComponent('Button'),
    TextInput: mockComponent('TextInput'),
  };

  return new Proxy(UI, {
    get(obj, key) {
      return key in mocks ? mocks[key] : obj[key];
    },
  });
});