GithubHelp home page GithubHelp logo

react-render-builder's Introduction

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'));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.