RTL-HELPERS

Useful functions for tests integrated with React

Installation

Use the package manager npm to install rtl-helpers.

npm install rtl-helpers

Usage

import renderWithRouterAndRedux from 'rtl-helpers';
import { renderWithRedux, renderWithRouter } from 'rtl-helpers';

Functions

renderWithRouterAndRedux

  • 1st parameter: component to be rendered
  • 2st parameter: Reducer, prefer to create a file with the “rootReducer” of all reducers combined
  • 3st parameter: A settings object:

{
    initialState = {}, // Default State
    store = createStore(rootReducer, initialState), // if you do not pass a store by parameter, a new one will be created
    initialEntries = ['/'], // if you want to redirect to some URL
    history = createMemoryHistory({ initialEntries }), // if you want a history
  }

Example

 renderWithRouterAndRedux(<App />, {
   initialState: initialStateMock, initialEntries: ['/customers'],
 });

renderWithRouter and renderWithRedux

the renderWithRouter function takes the parameters:

  • 1st component to be rendered
  • 2nd optional URL
The renderWithRedux function receives the same parameters as the renderWithRouterAndRedux function but it does not provide the history to the rendered components

Required Libraries

import React from 'react';
import { Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import { createStore } from 'redux';

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Authors

@wendryosales

The function was not created by me.

License

ISC

GitHub

View Github