Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Remove undefined props when pretty-format'ing React elements
  • Loading branch information
brunotavares committed May 9, 2018
commit a4f863d3ac4feb59d4b4d84fba310092c8beda18
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