Skip to content

Commit

Permalink
Implement verbose debug output
Browse files Browse the repository at this point in the history
switch to inspect

[Docs] setting jsx highlighting on certain blocks

[Docs] Make clearer the docs for .mount()

[Docs] fix doc linting errors
  • Loading branch information
blainekasten committed Mar 2, 2018
1 parent 0efc948 commit c3925de
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/api/ReactWrapper/mount.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/guides/migration-from-2-to-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,8 @@ return 3 and not 1 as in previous versions. A closer look using
[`debug`](../api/ReactWrapper/debug.md) reveals:

<!-- eslint react/prop-types: 0, react/prefer-stateless-function: 0 -->

```
<!-- eslint-skip -->
```jsx
// console.log(wrapper.find('[aria-expanded="true"]').debug());

<HelpLinkContainer aria-expanded={true} text="foo">
Expand All @@ -465,9 +465,9 @@ To return only the html nodes use the

`wrapper.find("[aria-expanded=true]").hostNodes().debug()` will now return:

<!-- eslint react/prop-types: 0, react/prefer-stateless-function: 0 -->
<!-- eslint react/prop-types: 0, react/prefer-stateless-function: 0, no-unused-expressions: 0, jsx-a11y/anchor-is-valid: 0 -->

```
```jsx
<a aria-expanded="true">foo</a>;
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
64 changes: 64 additions & 0 deletions packages/enzyme-test-suite/test/Debug-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,36 @@ describe('debug', () => {
<span>
span text
</span>
</div>`
));
});

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 (
<div data-json={nestedData} data-arry={arry}>Test Component</div>
);
}
}

const wrapper = shallow(<Foo />);
expect(wrapper.debug({ verbose: true })).to.equal((
`<div data-json={{ a: [ 1, 3, { true: true } ], b: false, c: { d: 'f' }, d: [ 1, 3, { true: true } ] }} data-arry={[ 1, 2, { f: { d: 'f' } } ]}>
Test Component
</div>`
));

expect(wrapper.debug({ verbose: false })).to.equal((
`<div data-json={{...}} data-arry={{...}}>
Test Component
</div>`
));
});
Expand Down Expand Up @@ -684,5 +714,39 @@ describe('debug', () => {
</Bar>`
));
});

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 (
<div data-json={nestedData} data-arry={arry}>Test Component</div>
);
}
}

const wrapper = mount(<Foo />);
expect(wrapper.debug({ verbose: true })).to.equal((
`<Foo>
<div data-json={{ a: [ 1, 3, { true: true } ], b: false, c: { d: 'f' }, d: [ 1, 3, { true: true } ] }} data-arry={[ 1, 2, { f: { d: 'f' } } ]}>
Test Component
</div>
</Foo>`
));

expect(wrapper.debug({ verbose: false })).to.equal((
`<Foo>
<div data-json={{...}} data-arry={{...}}>
Test Component
</div>
</Foo>`
));
});
});
});
12 changes: 8 additions & 4 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand All @@ -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) {
Expand All @@ -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
? '>'
Expand Down
8 changes: 5 additions & 3 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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 = {}) {
Expand Down
5 changes: 3 additions & 2 deletions packages/enzyme/src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down

0 comments on commit c3925de

Please sign in to comment.