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 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/enzyme/__tests__/checkbox_with_label.test.js
Expand Up @@ -3,7 +3,11 @@
/* eslint-disable no-unused-vars */

import React from 'react';
import {shallow} from 'enzyme';
import Enzyme, {shallow} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({adapter: new Adapter()});

import CheckboxWithLabel from '../CheckboxWithLabel';

it('CheckboxWithLabel changes the text after click', () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/react/__tests__/checkbox_with_label.test.js
Expand Up @@ -4,7 +4,7 @@

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import * as TestUtils from 'react-dom/test-utils';
import CheckboxWithLabel from '../CheckboxWithLabel';

it('CheckboxWithLabel changes the text after click', () => {
Expand Down
3 changes: 2 additions & 1 deletion examples/typescript/CheckboxWithLabel.tsx
@@ -1,13 +1,14 @@
/// <reference path="./typings/react/react.d.ts" />

import * as React from 'react'
import * as createReactClass from 'create-react-class';

interface CheckboxWithLabelProps extends React.Props<any> {
labelOff: string;
labelOn: string;
}

var CheckboxWithLabel = React.createClass<CheckboxWithLabelProps, any>( {
var CheckboxWithLabel = createReactClass<CheckboxWithLabelProps, any>( {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nah, let's use a regular class component. Nobody should use that in non-legacy codebases.

getInitialState: function() {
return {isChecked: false}
},
Expand Down
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -23,9 +23,11 @@
"browserify": "^16.1.0",
"chalk": "^2.0.1",
"codecov": "^3.0.0",
"create-react-class": "^15.6.3",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't need that

"cross-spawn": "^6.0.4",
"debug": "^3.0.1",
"enzyme": "^2.8.2",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "~4.13.0",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-babel": "^4.1.1",
Expand Down Expand Up @@ -58,10 +60,9 @@
"prettier": "^1.11.0",
"prettylint": "^1.0.0",
"progress": "^2.0.0",
"react": "^15.4.2",
"react-addons-test-utils": "^15.4.2",
"react-dom": "^15.6.1",
"react-test-renderer": "^15.4.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-test-renderer": "^16.2.0",
"regenerator-runtime": "^0.11.0",
"resolve": "^1.4.0",
"rimraf": "^2.6.2",
Expand Down
27 changes: 27 additions & 0 deletions packages/pretty-format/src/__tests__/react.test.js
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 @@ -300,6 +301,32 @@ test('supports undefined element type', () => {
);
});

test('supports a fragment with no children', () => {
expect(
formatElement({$$typeof: elementSymbol, props: {}, type: fragmentSymbol}),
).toEqual('<React.Fragment />');
});

test('supports a fragment with string child', () => {
expect(
formatElement({
$$typeof: elementSymbol,
props: {children: 'test'},
type: fragmentSymbol,
}),
).toEqual('<React.Fragment>\n test\n</React.Fragment>');
});

test('supports a fragment with element child', () => {
expect(
formatElement({
$$typeof: elementSymbol,
props: {children: React.createElement('div', null, 'test')},
type: fragmentSymbol,
}),
).toEqual('<React.Fragment>\n <div>\n test\n </div>\n</React.Fragment>');
});

test('supports a single element with React elements with a child', () => {
assertPrintedJSX(
React.createElement('Mouse', {
Expand Down
4 changes: 4 additions & 0 deletions packages/pretty-format/src/plugins/react_element.js
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