From 9745de0bf25e826186be07e7846f4ecd7c685592 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Mon, 2 Jul 2018 10:22:41 -0400 Subject: [PATCH] [New] Debug: `debugNode` now returns `[function]` for function children --- .../enzyme-test-suite/test/Debug-spec.jsx | 30 +++++++++++++++++++ packages/enzyme/src/Debug.js | 4 +++ 2 files changed, 34 insertions(+) diff --git a/packages/enzyme-test-suite/test/Debug-spec.jsx b/packages/enzyme-test-suite/test/Debug-spec.jsx index fc9c586a3..f665bc5a2 100644 --- a/packages/enzyme-test-suite/test/Debug-spec.jsx +++ b/packages/enzyme-test-suite/test/Debug-spec.jsx @@ -748,5 +748,35 @@ describe('debug', () => { ` )); }); + + it('handles function children', () => { + class Abomination extends React.Component { + render() { + /* eslint no-unused-vars: 0, func-names: 0 */ + return ( +
+ {function Foo() { /* hi */ }} + {} + {arrow => arrow('function')} + {[1, 2, NaN]} + {function (anonymous) {}} +
+ ); + } + } + + const wrapper = shallow(); + expect(wrapper.debug()).to.equal(( + `
+ [function Foo] + + [function] + 1 + 2 + NaN + [function] +
` + )); + }); }); }); diff --git a/packages/enzyme/src/Debug.js b/packages/enzyme/src/Debug.js index 27ccb372a..0d5974e84 100644 --- a/packages/enzyme/src/Debug.js +++ b/packages/enzyme/src/Debug.js @@ -66,6 +66,10 @@ function indentChildren(childrenStrs, indentLength) { export function debugNode(node, indentLength = 2, options = {}) { if (typeof node === 'string' || typeof node === 'number') return escape(node); + if (typeof node === 'function') { + const name = functionName(node); + return `[function${name ? ` ${name}` : ''}]`; + } if (!node) return ''; const childrenStrs = compact(childrenOfNode(node).map(n => debugNode(n, indentLength, options)));