From c3925de7aad4939e68276f0bd9c53595a9a2fd7d 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 [Docs] setting jsx highlighting on certain blocks [Docs] Make clearer the docs for .mount() [Docs] fix doc linting errors --- docs/api/ReactWrapper/mount.md | 2 +- docs/guides/migration-from-2-to-3.md | 8 +-- package.json | 2 +- .../enzyme-test-suite/test/Debug-spec.jsx | 64 +++++++++++++++++++ packages/enzyme/src/Debug.js | 12 ++-- packages/enzyme/src/ReactWrapper.js | 8 ++- packages/enzyme/src/ShallowWrapper.js | 5 +- 7 files changed, 86 insertions(+), 15 deletions(-) diff --git a/docs/api/ReactWrapper/mount.md b/docs/api/ReactWrapper/mount.md index 6eeb7fd5d..ae7597c53 100644 --- a/docs/api/ReactWrapper/mount.md +++ b/docs/api/ReactWrapper/mount.md @@ -1,6 +1,6 @@ # `.mount() => Self` -A method that re-mounts the component. This can be used to simulate a component going through +A method that re-mounts the component, if it is not currently mounted. This can be used to simulate a component going through an unmount/mount lifecycle. #### Returns diff --git a/docs/guides/migration-from-2-to-3.md b/docs/guides/migration-from-2-to-3.md index 54589b443..920112b79 100644 --- a/docs/guides/migration-from-2-to-3.md +++ b/docs/guides/migration-from-2-to-3.md @@ -437,8 +437,8 @@ return 3 and not 1 as in previous versions. A closer look using [`debug`](../api/ReactWrapper/debug.md) reveals: - -``` + +```jsx // console.log(wrapper.find('[aria-expanded="true"]').debug()); @@ -465,9 +465,9 @@ To return only the html nodes use the `wrapper.find("[aria-expanded=true]").hostNodes().debug()` will now return: - + -``` +```jsx foo; ``` diff --git a/package.json b/package.json index 0b22f9dc2..459a6703e 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "eslint-config-airbnb": "^16.1.0", "eslint-plugin-import": "^2.8.0", "eslint-plugin-jsx-a11y": "^6.0.3", - "eslint-plugin-markdown": "^1.0.0-beta.6", + "eslint-plugin-markdown": "^1.0.0-beta.7", "eslint-plugin-react": "^7.6.1", "gitbook-cli": "^1.0.1", "gitbook-plugin-anchors": "^0.7.1", diff --git a/packages/enzyme-test-suite/test/Debug-spec.jsx b/packages/enzyme-test-suite/test/Debug-spec.jsx index 7ecd6b2e4..fc9c586a3 100644 --- a/packages/enzyme-test-suite/test/Debug-spec.jsx +++ b/packages/enzyme-test-suite/test/Debug-spec.jsx @@ -626,6 +626,36 @@ 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 +
` + )); + + expect(wrapper.debug({ verbose: false })).to.equal(( + `
+ Test Component
` )); }); @@ -684,5 +714,39 @@ describe('debug', () => { ` )); }); + + 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 = mount(); + expect(wrapper.debug({ verbose: true })).to.equal(( + ` +
+ Test Component +
+
` + )); + + expect(wrapper.debug({ verbose: false })).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..96f5f117c 100644 --- a/packages/enzyme/src/ReactWrapper.js +++ b/packages/enzyme/src/ReactWrapper.js @@ -244,7 +244,8 @@ class ReactWrapper { } /** - * A method that re-mounts the component. This can be used to simulate a component going through + * A method that re-mounts the component, if it is not currently mounted. + * This can be used to simulate a component going through * an unmount/mount lifecycle. * * @returns {ReactWrapper} @@ -988,8 +989,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, arrays and objects to be verbosely printed. * @returns {String} */ debug(options = {}) { diff --git a/packages/enzyme/src/ShallowWrapper.js b/packages/enzyme/src/ShallowWrapper.js index 2144bc630..5a76c63c7 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, arrays and objects to be verbosely printed. * @returns {String} */ debug(options = {}) {