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 React forwardRef in pretty-format #6076

Closed
wants to merge 5 commits 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
([#5968](https://github.com/facebook/jest/pull/5968))
* `[jest-config]` Ensure that custom resolvers are used when resolving the
configuration ([#5976](https://github.com/facebook/jest/pull/5976))
* `[pretty-format]` Handle React.forwardRef
* `[website]` Fix website docs
([#5853](https://github.com/facebook/jest/pull/5853))

Expand Down
19 changes: 17 additions & 2 deletions packages/pretty-format/src/__tests__/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ const renderer = require('react-test-renderer');

const elementSymbol = Symbol.for('react.element');
const fragmentSymbol = Symbol.for('react.fragment');
const forwardRefSymbol = Symbol.for('react.forward_ref');
const testSymbol = Symbol.for('react.test.json');

const prettyFormat = require('..');
const {ReactElement, ReactTestComponent} = prettyFormat.plugins;
const {
ReactElement,
ReactTestComponent,
ReactForwardRef,
} = prettyFormat.plugins;

const formatElement = (element: any, options?: OptionsReceived) =>
prettyFormat(
element,
Object.assign(
({
plugins: [ReactElement],
plugins: [ReactElement, ReactForwardRef],
}: OptionsReceived),
options,
),
Expand Down Expand Up @@ -327,6 +332,16 @@ test('supports a fragment with element child', () => {
).toEqual('<React.Fragment>\n <div>\n test\n </div>\n</React.Fragment>');
});

test('supports forwardRef with name', () => {
function Cat(props) {
return React.createElement('div', props);
}

expect(formatElement({$$typeof: forwardRefSymbol, render: Cat})).toEqual(
'<ForwardRef(Cat) … />',
);
});

test('supports a single element with React elements with a child', () => {
assertPrintedJSX(
React.createElement('Mouse', {
Expand Down
2 changes: 2 additions & 0 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import DOMElement from './plugins/dom_element';
import Immutable from './plugins/immutable';
import ReactElement from './plugins/react_element';
import ReactTestComponent from './plugins/react_test_component';
import ReactForwardRef from './plugins/react_forward_ref';

const toString = Object.prototype.toString;
const toISOString = Date.prototype.toISOString;
Expand Down Expand Up @@ -490,6 +491,7 @@ prettyFormat.plugins = {
DOMElement,
Immutable,
ReactElement,
ReactForwardRef,
ReactTestComponent,
};

Expand Down
39 changes: 39 additions & 0 deletions packages/pretty-format/src/plugins/react_forward_ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat';

import {printElementAsLeaf} from './lib/markup';

const forwardRefSymbol = Symbol.for('react.forward_ref');

const getType = element => {
if (element.$$typeof === forwardRefSymbol) {
const functionName =
element.render.displayName || element.render.name || '';

return functionName !== ''
? 'ForwardRef(' + functionName + ')'
: 'ForwardRef';
}
return 'UNDEFINED';
};

export const serialize = (
element: any,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer,
): string => printElementAsLeaf(getType(element), config);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this part of the normal ReactElementPlugin instead of creating a new one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, I should read the OP more closely

Had to create a separate plugin since the forwardRef spec doesn't conform to normal React elements


export const test = (val: any) => val && val.$$typeof === forwardRefSymbol;

export default ({serialize, test}: NewPlugin);