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

Update to React 16, handle React.Fragment better #5816

Merged
merged 6 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions packages/pretty-format/src/__tests__/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const React = require('react');
const renderer = require('react-test-renderer');

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

const prettyFormat = require('..');
Expand Down Expand Up @@ -90,6 +91,13 @@ test('supports a single element with mixed children', () => {
);
});

test('supports a single fragment with one child', () => {
Copy link
Member Author

@rickhanlonii rickhanlonii Mar 16, 2018

Choose a reason for hiding this comment

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

This test fails because we're on React 15:

Thoughts on updating?

Copy link
Member Author

@rickhanlonii rickhanlonii Mar 16, 2018

Choose a reason for hiding this comment

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

The alternatives are:

  • Don't support
  • Don't test
  • Unit test just getType
  • try/catch the React throw:
test('supports a single fragment with one child', () => {
  const spy = jest.spyOn(console, 'error').mockImplementationOnce(() => {});
  try {
    assertPrintedJSX(
      React.createElement(fragmentSymbol, null, 0),
      '<React.Fragment>\n  0\n</React.Fragment>',
    );
  } catch(e) {
    if (e.message.indexOf('Element type is invalid:') === 0) {
      return;
    }

    throw e;
  }
  spy.mockRestore();
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should update to latest React

assertPrintedJSX(
React.createElement(fragmentSymbol, null, 0),
'<React.Fragment>\n 0\n</React.Fragment>',
);
});

test('supports props with strings', () => {
assertPrintedJSX(
React.createElement('Mouse', {style: 'color:red'}),
Expand Down
4 changes: 4 additions & 0 deletions packages/pretty-format/src/plugins/react_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from './lib/markup';

const elementSymbol = Symbol.for('react.element');
const fragmentSymbol = Symbol.for('react.fragment');

// Given element.props.children, or subtree during recursive traversal,
// return flattened array of children.
Expand All @@ -38,6 +39,9 @@ const getType = element => {
if (typeof element.type === 'function') {
return element.type.displayName || element.type.name || 'Unknown';
}
if (element.type === fragmentSymbol) {
return 'React.Fragment';
}
return 'UNDEFINED';
};

Expand Down