feat: wrap render with act() for better React Hooks support (#122)

This commit is contained in:
Kacper Wiszczuk
2019-02-28 21:53:26 +01:00
committed by Michał Pierzchała
parent 747e864369
commit 11489b0e5e
11 changed files with 144 additions and 35 deletions
+22 -3
View File
@@ -1,20 +1,26 @@
// @flow
import * as React from 'react';
import TestRenderer from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
import act from './act';
import { getByAPI } from './helpers/getByAPI';
import { queryByAPI } from './helpers/queryByAPI';
import debugShallow from './helpers/debugShallow';
import debugDeep from './helpers/debugDeep';
type Options = {
createNodeMock: (element: React.Element<any>) => any,
};
/**
* Renders test component deeply using react-test-renderer and exposes helpers
* to assert on the output.
*/
export default function render(
component: React.Element<any>,
options?: { createNodeMock: (element: React.Element<any>) => any }
options?: Options
) {
const renderer = TestRenderer.create(component, options);
const renderer = renderWithAct(component, options);
const instance = renderer.root;
return {
@@ -27,6 +33,19 @@ export default function render(
};
}
function renderWithAct(
component: React.Element<any>,
options?: Options
): ReactTestRenderer {
let renderer: ReactTestRenderer;
act(() => {
renderer = TestRenderer.create(component, options);
});
return ((renderer: any): ReactTestRenderer);
}
function debug(instance: ReactTestInstance, renderer) {
function debugImpl(message?: string) {
return debugDeep(renderer.toJSON(), message);