Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Suspense in shallow renderer #14638

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/react-test-renderer/src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import {isForwardRef} from 'react-is';
import {isForwardRef, isSuspense} from 'react-is';
import describeComponentFrame from 'shared/describeComponentFrame';
import getComponentName from 'shared/getComponentName';
import shallowEqual from 'shared/shallowEqual';
Expand Down Expand Up @@ -499,7 +499,9 @@ class ReactShallowRenderer {
element.type,
);
invariant(
isForwardRef(element) || typeof element.type === 'function',
isSuspense(element) ||
isForwardRef(element) ||
typeof element.type === 'function',
'ReactShallowRenderer render(): Shallow rendering works only with custom ' +
'components, but the provided element type was `%s`.',
Array.isArray(element.type)
Expand All @@ -520,7 +522,9 @@ class ReactShallowRenderer {
if (this._instance) {
this._updateClassComponent(element, this._context);
} else {
if (isForwardRef(element)) {
if (isSuspense(element)) {
this._rendered = element.props.children;
} else if (isForwardRef(element)) {
this._rendered = element.type.render(element.props, element.ref);
} else if (shouldConstruct(element.type)) {
this._instance = new element.type(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
let createRenderer;
let PropTypes;
let React;
let ReactIs;

describe('ReactShallowRenderer', () => {
beforeEach(() => {
Expand All @@ -21,6 +22,7 @@ describe('ReactShallowRenderer', () => {
createRenderer = require('react-test-renderer/shallow').createRenderer;
PropTypes = require('prop-types');
React = require('react');
ReactIs = require('react-is');
});

it('should call all of the legacy lifecycle hooks', () => {
Expand Down Expand Up @@ -256,6 +258,31 @@ describe('ReactShallowRenderer', () => {
);
});

it('should handle Suspense', () => {
const fallback = <div>fallback</div>;
const shallowRenderer = createRenderer();
const targetText = 'test';
const result = shallowRenderer.render(
<React.Suspense fallback={fallback}>
<div>{targetText}</div>
</React.Suspense>,
);
expect(result.type).toBe('div');
expect(result.props.children).toBe(targetText);
});

it('should shallow render lazy component in children and not to handle fallback', () => {
const fallback = <div>fallback</div>;
const LazyComponent = React.lazy(() => Promise.resolve({default: null}));
const shallowRenderer = createRenderer();
const result = shallowRenderer.render(
<React.Suspense fallback={fallback}>
<LazyComponent />
</React.Suspense>,
);
expect(ReactIs.isLazy(result.type)).toBe(true);
});

it('should enable shouldComponentUpdate to prevent a re-render', () => {
let renderCounter = 0;
class SimpleComponent extends React.Component {
Expand Down