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

Remove undefined props when pretty-format'ing React elements #6162

Merged
merged 3 commits into from
May 14, 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
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