Skip to content

Commit

Permalink
Update to React 16, handle React.Fragment better (#5816)
Browse files Browse the repository at this point in the history
* Add React.Fragment to snapshots in unhandled cases

* Add test

* Upgrade to React 16

* Fix flow

* Convert to class component
  • Loading branch information
rickhanlonii authored and cpojer committed Mar 18, 2018
1 parent b9ed115 commit 442e93d
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 367 deletions.
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

0 comments on commit 442e93d

Please sign in to comment.