Simplest React.lazy alternative
...with able to fully coverage testing in Jest
Why?
There are currently difficulties/problems with testing React Suspense/lazy in jest and enzyme.
This package provides alternative with almost same API and testing util.
Install
# by npm npm install --save simplest-react-lazy # or yarn yarn add simplest-react-lazy
Usage
component.jsx
import React from 'react'; const Component = <div>It is just a component...</div>; ;
lazy-component.jsx
import lazy from 'simplest-react-lazy'; const LazyComponent = ;
index.jsx
import React useState from 'react';import LazyComponent from './lazy-component.jsx'; const App = const show toggleShow = ; return <div> <h1>Test App</h1> <p>simplest-react-lazy example</p> <button => Show lazy component </button> show && <LazyComponent ='Loading...' /> // no Suspense needed </div> ;;
Jest testing example
First of all setup jest
// in your jest setup fileimport mockLazy from 'simplest-react-lazy/test-utils'; // ...other setup... jest;
Write test with special util
import React from 'react';import act from 'react-dom/test-utils';import mount from 'enzyme';import importLazy from 'simplest-react-lazy/test-utils';// ...import components... ;
To Do
- ~remove
lodash/fp/prop
~ - ~show warning when resolveComponent returns non valid React type~