Skip to content

Commit

Permalink
Remove undefined props when pretty-format'ing React elements (#6162)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotavares authored and mjesun committed May 14, 2018
1 parent 4ae3e40 commit 19c5278
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
configuration ([#5976](https://github.com/facebook/jest/pull/5976))
* `[website]` Fix website docs
([#5853](https://github.com/facebook/jest/pull/5853))
* `[pretty-format]` [**BREAKING**] Remove undefined props from React elements
([#6162](https://github.com/facebook/jest/pull/6162))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ exports[`generates a snapshot with correctly transformed dependencies 1`] = `
className="App-logo"
src="logo.svg"
/>
<h2
className={undefined}
>
<h2>
Welcome to React
</h2>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ exports[`ReactTestComponent plugin highlights syntax with color from theme optio
</>Hello, Mouse!</>
<cyan></Mouse></>"
`;
exports[`ReactTestComponent removes undefined props 1`] = `
"<cyan><Mouse</>
<yellow>xyz</>=<red>{true}</>
<cyan>/></>"
`;
25 changes: 25 additions & 0 deletions packages/pretty-format/src/__tests__/react.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,28 @@ test('supports context Consumer with a child', () => {
),
).toEqual('<Context.Consumer>\n [Function anonymous]\n</Context.Consumer>');
});

test('ReactElement removes undefined props', () => {
assertPrintedJSX(
React.createElement('Mouse', {
abc: undefined,
xyz: true,
}),
'<Mouse\n xyz={true}\n/>',
);
});

test('ReactTestComponent removes undefined props', () => {
const jsx = React.createElement('Mouse', {
abc: undefined,
xyz: true,
});
expect(
formatElement(jsx, {
highlight: true,
theme: {
value: 'red',
},
}),
).toMatchSnapshot();
});
12 changes: 9 additions & 3 deletions packages/pretty-format/src/plugins/react_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ const getType = element => {
return 'UNDEFINED';
};

const getPropKeys = element => {
const {props} = element;

return Object.keys(props)
.filter(key => key !== 'children' && props[key] !== undefined)
.sort();
};

export const serialize = (
element: React$Element<any>,
config: Config,
Expand All @@ -79,9 +87,7 @@ export const serialize = (
: printElement(
getType(element),
printProps(
Object.keys(element.props)
.filter(key => key !== 'children')
.sort(),
getPropKeys(element),
element.props,
config,
indentation + config.indent,
Expand Down
12 changes: 11 additions & 1 deletion packages/pretty-format/src/plugins/react_test_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ import {

const testSymbol = Symbol.for('react.test.json');

const getPropKeys = object => {
const {props} = object;

return props
? Object.keys(props)
.filter(key => props[key] !== undefined)
.sort()
: [];
};

export const serialize = (
object: ReactTestObject,
config: Config,
Expand All @@ -38,7 +48,7 @@ export const serialize = (
object.type,
object.props
? printProps(
Object.keys(object.props).sort(),
getPropKeys(object),
// Despite ternary expression, Flow 0.51.0 found incorrect error:
// undefined is incompatible with the expected param type of Object
// $FlowFixMe
Expand Down

0 comments on commit 19c5278

Please sign in to comment.