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.
yarn add --dev react-render-builder
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.
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}`;
}
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.
Only to be used on your extension of ReactRenderBuilder
, this is used in your builder methods for wrapping a component
in the tree.
- wrapperElement: ProviderFunction that is a function that given a child element returns an Element wrapped with that child.
this.addElement(children => <MyContext children={children}/>);
Wraps @testing-library/react-native render
function, but wraps given element with tree constructed via addElement
in builder methods.
- element: React JSX Element to render
- renderResult: @testing-library/react-native RenderResult
const renderAPI = new RenderBuilder().render(<MyComponent/>);
Returns JSX Tree that will be passed to render
. Useful for debugging or for rerendering via the
rerender method from
the RenderResult.
- element: React JSX Element to render
- JSX element that is the entire tree
const jsxTree = new RenderBuilder().toJSX(<MyComponent/>);
Wraps @testing-library/react-native renderHook
function, but wraps given hook with tree constructed via addElement
in builder methods.
- hook: React Hook to render
- renderHookResult: @testing-library/react-native RenderHookResult
const renderHookResult = new RenderBuilder().renderHook(useMyHook);
Same as renderHook
but returns the given hooks return value. Same as calling renderHook(useMyHook).result.current
.
- hook: React Hook to render
- return value of given hook
const helloString = new RenderBuilder().renderHookResult(() => useHello('John'));