From 59dc2d03b8836699023cbbc6b416e9401ce37bd3 Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Thu, 1 Mar 2018 09:42:14 -0600 Subject: [PATCH] Implement verbose debug output switch to inspect --- .../enzyme-test-suite/test/Debug-spec.jsx | 50 +++++++++++++++++++ packages/enzyme/src/Debug.js | 12 +++-- packages/enzyme/src/ReactWrapper.js | 5 +- packages/enzyme/src/ShallowWrapper.js | 5 +- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/packages/enzyme-test-suite/test/Debug-spec.jsx b/packages/enzyme-test-suite/test/Debug-spec.jsx index 7ecd6b2e4..ccfd8a724 100644 --- a/packages/enzyme-test-suite/test/Debug-spec.jsx +++ b/packages/enzyme-test-suite/test/Debug-spec.jsx @@ -626,6 +626,30 @@ describe('debug', () => { span text +` + )); + }); + + it('options.verbose causes arrays and objects to be verbosely printed', () => { + class Foo extends React.Component { + render() { + const nestedData = { + a: [1, 3, { true: true }], + b: false, + c: { d: 'f' }, + }; + nestedData.d = nestedData.a; + const arry = [1, 2, { f: nestedData.c }]; + return ( +
Test Component
+ ); + } + } + + const wrapper = shallow(); + expect(wrapper.debug({ verbose: true })).to.equal(( + `
+ Test Component
` )); }); @@ -684,5 +708,31 @@ describe('debug', () => { ` )); }); + + it('options.verbose causes boxed primitives to be unboxed', () => { + class Foo extends React.Component { + render() { + const nestedData = { + a: [1, 3, { true: true }], + b: false, + c: { d: 'f' }, + }; + nestedData.d = nestedData.a; + const arry = [1, 2, { f: nestedData.c }]; + return ( +
Test Component
+ ); + } + } + + const wrapper = mount(); + expect(wrapper.debug({ verbose: true })).to.equal(( + ` +
+ Test Component +
+
` + )); + }); }); }); diff --git a/packages/enzyme/src/Debug.js b/packages/enzyme/src/Debug.js index e82690c33..27ccb372a 100644 --- a/packages/enzyme/src/Debug.js +++ b/packages/enzyme/src/Debug.js @@ -29,7 +29,7 @@ export function indent(depth, string) { return string.split('\n').map(x => `${spaces(depth)}${x}`).join('\n'); } -function propString(prop) { +function propString(prop, options) { if (isString(prop)) { return inspect(String(prop), { quoteStyle: 'double' }); } @@ -43,15 +43,19 @@ function propString(prop) { return `{${inspect(prop)}}`; } if (typeof prop === 'object') { + if (options.verbose) { + return `{${inspect(prop)}}`; + } + return '{{...}}'; } return `{[${typeof prop}]}`; } -function propsString(node) { +function propsString(node, options) { const props = propsOfNode(node); const keys = without(Object.keys(props), 'children'); - return keys.map(key => `${key}=${propString(props[key])}`).join(' '); + return keys.map(key => `${key}=${propString(props[key], options)}`).join(' '); } function indentChildren(childrenStrs, indentLength) { @@ -67,7 +71,7 @@ export function debugNode(node, indentLength = 2, options = {}) { const childrenStrs = compact(childrenOfNode(node).map(n => debugNode(n, indentLength, options))); const type = typeName(node); - const props = options.ignoreProps ? '' : propsString(node); + const props = options.ignoreProps ? '' : propsString(node, options); const beforeProps = props ? ' ' : ''; const afterProps = childrenStrs.length ? '>' diff --git a/packages/enzyme/src/ReactWrapper.js b/packages/enzyme/src/ReactWrapper.js index d14a3fc0a..7ba7bcd95 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -988,8 +988,9 @@ class ReactWrapper { /** * Returns an HTML-like string of the shallow render for debugging purposes. * - * @param {Object} options - (Optional) Property bag of additional options. - * options.ignoreProps - if true, props are omitted from the string. + * @param {Object} [options] - Property bag of additional options. + * @param {boolean} [options.ignoreProps] - if true, props are omitted from the string. + * @param {boolean} [options.verbose] - if true, boxed primitives are unboxed. * @returns {String} */ debug(options = {}) { diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 2144bc630..5fada2540 100644 --- a/packages/enzyme/src/ShallowWrapper.js +++ b/packages/enzyme/src/ShallowWrapper.js @@ -1132,8 +1132,9 @@ class ShallowWrapper { /** * Returns an HTML-like string of the shallow render for debugging purposes. * - * @param {Object} options - (Optional) Property bag of additional options. - * options.ignoreProps - if true, props are omitted from the string. + * @param {Object} [options] - Property bag of additional options. + * @param {boolean} [options.ignoreProps] - if true, props are omitted from the string. + * @param {boolean} [options.verbose] - if true, boxed primitives are unboxed. * @returns {String} */ debug(options = {}) {