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-render-builder

v2.0.0

Published

Encapsulate Context/Data Providers when testing Components and Hooks in React

Downloads

645

Readme

react-render-builder

Encapsulate Context/Data Providers when testing Components and Hooks in React

For react-native support, checkout react-native-render-builder

Extension of @testing-library/react render and renderHook functions with a builder interface for easily reusing setup of your JSX tree.

Install

yarn

yarn add --dev react-render-builder

npm

npm install --save-dev react-render-builder

This library has a peerDependencies listing for @testing-library/react-native. Make sure you install it alongside this library.

Usage

First create your RenderBuilder with any builder methods for adding in components you want

import { CounterProvider } from './CounterContext';
import { ReactRenderBuilder } from 'react-render-builder';

export class RenderBuilder extends ReactRenderBuilder {
    counter(initialValue: number): this {
        this.addElement(children => <CounterProvider initialValue={initialValue} children={children}/>);
        return this;
    }
}

Then you can use it in your tests

import React from 'react';
import { describe, expect, it } from '@jest/globals';
import { useCounter } from './CounterContext';
import { RenderBuilder } from './RenderBuilder';

describe('hello with counter 1', () => {
    const renderBuilder = new RenderBuilder().counter(1);

    it('render correct string in Hello Component', () => {
        const renderApi = renderBuilder.render(<Hello/>);
        renderApi.getByText('Hello 1');
    });

    it('returns correct string in useHelloHook', () => {
        const helloText = renderBuilder.renderHookResult(useHelloHook);
        expect(helloText).toEqual('Hello 1');
    });
});

function Hello() {
    const counterValue = useHelloHook();
    return (
        <div>
            <p>{counterValue}</p>
        </div>
    );
}

function useHelloHook() {
    const counterValue = useCounter();
    return `Hello ${counterValue.value}`;
}

Documentation

ReactRenderBuilder

Wraps @testing-library/react-native render and renderHook functions with a builder interface for easily reusing setup of your JSX tree.

Typically, you will extend this class (as in the example above) and add in builder methods you will use in your tests for reusing JSX Tree setup.

Methods

addElement

Only to be used on your extension of ReactRenderBuilder, this is used in your builder methods for wrapping a component in the tree.

Parameters
  • wrapperElement: ProviderFunction that is a function that given a child element returns an Element wrapped with that child.
Example
this.addElement(children => <MyContext children={children}/>);
render

Wraps @testing-library/react-native render function, but wraps given element with tree constructed via addElement in builder methods.

Parameters
  • element: React JSX Element to render
Returns
Example
const renderAPI = new RenderBuilder().render(<MyComponent/>);
toJSX

Returns JSX Tree that will be passed to render. Useful for debugging or for rerendering via the rerender method from the RenderResult.

Parameters
  • element: React JSX Element to render
Returns
  • JSX element that is the entire tree
Example
const jsxTree = new RenderBuilder().toJSX(<MyComponent/>);
renderHook

Wraps @testing-library/react-native renderHook function, but wraps given hook with tree constructed via addElement in builder methods.

Parameters
  • hook: React Hook to render
Returns
Example
const renderHookResult = new RenderBuilder().renderHook(useMyHook);
renderHookResult

Same as renderHook but returns the given hooks return value. Same as calling renderHook(useMyHook).result.current.

Parameters
  • hook: React Hook to render
Returns
  • return value of given hook
Example
const helloString = new RenderBuilder().renderHookResult(() => useHello('John'));