Skip to content

Commit

Permalink
Implement verbose debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
blainekasten committed Mar 1, 2018
1 parent 0efc948 commit 3904e0d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.6.1"
}
}
}
50 changes: 50 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,30 @@ describe('debug', () => {
<span>
span text
</span>
</div>`
));
});

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 (
<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":"~a"}} data-arry={[1,2,{"f":{"d":"f"}}]}>
Test Component
</div>`
));
});
Expand Down Expand Up @@ -684,5 +708,31 @@ describe('debug', () => {
</Bar>`
));
});

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 (
<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":"~a"}} data-arry={[1,2,{"f":{"d":"f"}}]}>
Test Component
</div>
</Foo>`
));
});
});
});
1 change: 1 addition & 0 deletions packages/enzyme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"license": "MIT",
"dependencies": {
"cheerio": "^1.0.0-rc.2",
"circular-json": "^0.5.1",
"function.prototype.name": "^1.0.3",
"has": "^1.0.1",
"is-boolean-object": "^1.0.0",
Expand Down
13 changes: 9 additions & 4 deletions packages/enzyme/src/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import isNumber from 'is-number-object';
import isCallable from 'is-callable';
import isBoolean from 'is-boolean-object';
import inspect from 'object-inspect';
import CircularJSON from 'circular-json';

import {
propsOfNode,
Expand All @@ -29,7 +30,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 +44,19 @@ function propString(prop) {
return `{${inspect(prop)}}`;
}
if (typeof prop === 'object') {
if (options.verbose) {
return `{${CircularJSON.stringify(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 +72,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
5 changes: 3 additions & 2 deletions packages/enzyme/src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
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, boxed primitives are unboxed.
* @returns {String}
*/
debug(options = {}) {
Expand Down

0 comments on commit 3904e0d

Please sign in to comment.