Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 474c43a

Browse files
committed
feat(debug): allow custom element to be passed to debug
1 parent b9f52bf commit 474c43a

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/__tests__/pretty-print.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { prettyPrint } from '../pretty-print';
66

77
test('it prints out the given DOM element tree', () => {
88
const { container } = render(<Text>Hello World!</Text>);
9-
expect(prettyPrint(container.toJSON())).toMatchInlineSnapshot(`
9+
expect(prettyPrint(container)).toMatchInlineSnapshot(`
1010
"<Text>
1111
Hello World!
1212
</Text>"
@@ -15,5 +15,5 @@ test('it prints out the given DOM element tree', () => {
1515

1616
test('it supports truncating the output length', () => {
1717
const { container } = render(<Text>Hello World!</Text>);
18-
expect(prettyPrint(container.toJSON(), 5)).toMatch(/\.\.\./);
18+
expect(prettyPrint(container, 5)).toMatch(/\.\.\./);
1919
});

src/__tests__/render.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React from 'react';
2-
import { View } from 'react-native';
2+
import { Text, View } from 'react-native';
33
import { render } from '../';
44

5+
test('renders View', () => {
6+
const ref = React.createRef();
7+
const { debug } = render(<View ref={ref} />);
8+
9+
debug(<Text>hi</Text>);
10+
});
11+
512
test('renders View', () => {
613
const ref = React.createRef();
714
const { rootInstance } = render(<View ref={ref} />);

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function render(ui, { options = {}, wrapper: WrapperComponent } = {}) {
2121
return {
2222
container,
2323
rootInstance: container.root,
24-
debug: (el = container) => console.log(prettyPrint(el.toJSON())),
24+
debug: (el = container) => console.log(prettyPrint(el)),
2525
unmount: () => container.unmount(),
2626
rerender: rerenderUi => {
2727
container.update(wrapUiIfNeeded(rerenderUi));

src/pretty-print.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import prettyFormat from 'pretty-format';
2-
32
const { ReactTestComponent, ReactElement } = prettyFormat.plugins;
43

5-
function prettyPrint(reactElement, maxLength, options) {
4+
function getPrettyOutput(reactElement, maxLength, options) {
65
const debugContent = prettyFormat(reactElement, {
76
plugins: [ReactTestComponent, ReactElement],
87
printFunctionName: false,
@@ -15,4 +14,12 @@ function prettyPrint(reactElement, maxLength, options) {
1514
: debugContent;
1615
}
1716

17+
function prettyPrint(reactElement, ...args) {
18+
try {
19+
return getPrettyOutput(reactElement.toJSON(), ...args);
20+
} catch (e) {
21+
return getPrettyOutput(reactElement, ...args);
22+
}
23+
}
24+
1825
export { prettyPrint };

0 commit comments

Comments
 (0)