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 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
6 changes: 5 additions & 1 deletion examples/enzyme/__tests__/checkbox_with_label.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
24 changes: 13 additions & 11 deletions examples/typescript/CheckboxWithLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

import * as React from 'react'

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

var CheckboxWithLabel = React.createClass<CheckboxWithLabelProps, any>( {
getInitialState: function() {
return {isChecked: false}
},
onChange: function() {
this.setState({isChecked: !this.state.isChecked});
},
interface CheckboxWithLabelState {
isChecked: boolean;
}

class CheckboxWithLabel extends React.Component<CheckboxWithLabelProps, CheckboxWithLabelState> {
constructor(props: CheckboxWithLabelProps) {
super(props);
this.state = {isChecked: false};
}

render: function() {
render() {
return (
<label>
<input
type="checkbox"
checked={this.state.isChecked}
onChange={this.onChange}
onChange={() => this.setState(current => ({isChecked: !current.isChecked}))}
/>
{this.state.isChecked ? this.props.labelOn : this.props.labelOff}
</label>
);
}
});
}

export = CheckboxWithLabel;
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"codecov": "^3.0.0",
"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 +59,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
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 @@ -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
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
Loading