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

Commit a8ba416

Browse files
ajsmthbcarroll22
authored andcommitted
feat: formatting options for render() to remove options from debug() (#70)
* feat: formatting options for render() to remove options from debug output * update name -- propsToRemove to just removeProps * omitProps: remove prettyPrint plugin in favour of removing props in toJSON() call * update options in render() to destructure debug config
1 parent 94406cd commit a8ba416

File tree

6 files changed

+78
-13
lines changed

6 files changed

+78
-13
lines changed

src/__tests__/debug.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import React from 'react';
2-
import { Text } from 'react-native';
2+
import { Text, View } from 'react-native';
33

44
import { cleanup, render } from '../';
55

6+
let log;
7+
68
beforeEach(() => {
7-
jest.spyOn(console, 'log').mockImplementation(() => {});
9+
jest.spyOn(console, 'log').mockImplementation(output => (log = output));
810
});
911

1012
afterEach(() => {
1113
cleanup();
14+
log = undefined;
1215
console.log.mockRestore();
1316
});
1417

@@ -19,3 +22,33 @@ test('debug pretty prints the baseElement', () => {
1922
expect(console.log).toHaveBeenCalledTimes(1);
2023
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Hello World'));
2124
});
25+
26+
test('debug can remove specified props from output', () => {
27+
const { debug } = render(
28+
<View style={{ width: 300 }}>
29+
<Text>Hello World!</Text>
30+
</View>,
31+
{
32+
options: {
33+
debug: {
34+
omitProps: ['style', 'pointerEvents', 'collapsable'],
35+
},
36+
},
37+
},
38+
);
39+
40+
debug();
41+
42+
expect(console.log).toHaveBeenCalledTimes(1);
43+
expect(log).toMatchInlineSnapshot(`
44+
"<View>
45+
<View>
46+
<View>
47+
<Text>
48+
Hello World!
49+
</Text>
50+
</View>
51+
</View>
52+
</View>"
53+
`);
54+
});

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import act from './act-compat';
1515
const renderers = new Set();
1616

1717
function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
18+
const { debug, ...rest } = options;
19+
1820
const wrapUiIfNeeded = innerElement =>
1921
WrapperComponent ? (
2022
<AppContainer>
@@ -27,7 +29,7 @@ function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
2729
let testRenderer;
2830

2931
act(() => {
30-
testRenderer = TR.create(wrapUiIfNeeded(ui), options);
32+
testRenderer = TR.create(wrapUiIfNeeded(ui), rest);
3133
});
3234

3335
renderers.add(testRenderer);
@@ -39,7 +41,7 @@ function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
3941
return {
4042
baseElement,
4143
container,
42-
debug: (el = baseElement) => console.log(prettyPrint(el)),
44+
debug: (el = baseElement) => console.log(prettyPrint(el, undefined, { debug })),
4345
unmount: () => testRenderer.unmount(),
4446
rerender: rerenderUi => {
4547
act(() => {

src/lib/__tests__/pretty-print.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,24 @@ test('it supports truncating the output length', () => {
8888

8989
expect(prettyPrint(container, 5)).toMatch(/\.\.\./);
9090
});
91+
92+
test('it supports removing props from output', () => {
93+
const { container } = render(
94+
<View style={{ width: 100 }}>
95+
<Text>Hello World!</Text>
96+
</View>,
97+
);
98+
99+
expect(prettyPrint(container, undefined, { debug: { omitProps: ['style', 'pointerEvents'] } }))
100+
.toMatchInlineSnapshot(`
101+
"<View
102+
collapsable={true}
103+
>
104+
<View>
105+
<Text>
106+
Hello World!
107+
</Text>
108+
</View>
109+
</View>"
110+
`);
111+
});

src/lib/pretty-print.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import { toJSON } from './to-json';
55

66
const { ReactTestComponent, ReactElement } = prettyFormat.plugins;
77

8-
function prettyPrint(element, maxLength, options) {
9-
const debugContent = prettyFormat(toJSON(element), {
10-
plugins: [ReactTestComponent, ReactElement],
8+
function prettyPrint(element, maxLength, options = {}) {
9+
let plugins = [ReactTestComponent, ReactElement];
10+
const { debug, ...rest } = options;
11+
12+
const debugContent = prettyFormat(toJSON(element, debug), {
13+
plugins: plugins,
1114
printFunctionName: false,
1215
highlight: true,
13-
...options,
16+
...rest,
1417
});
1518

1619
return maxLength !== undefined && debugContent && debugContent.toString().length > maxLength

src/lib/to-json.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function toJSON({ _fiber: { stateNode = null } = {} } = {}) {
1+
function toJSON({ _fiber: { stateNode = null } = {} } = {}, options = {}) {
22
if (!stateNode) return null;
33
if (stateNode.rootContainerInstance && stateNode.rootContainerInstance.children.length === 0)
44
return null;
55

6-
return _toJSON(stateNode);
6+
return _toJSON(stateNode, options);
77
}
88

9-
function _toJSON(inst) {
9+
function _toJSON(inst, { omitProps = [] }) {
1010
if (inst.isHidden) {
1111
// Omit timed out children from output entirely. This seems like the least
1212
// surprising behavior. We could perhaps add a separate API that includes
@@ -23,12 +23,13 @@ function _toJSON(inst) {
2323
const { children, ...props } = inst.props;
2424

2525
// Convert all children to the JSON format
26-
const renderedChildren = inst.children.map(child => _toJSON(child));
26+
const renderedChildren = inst.children.map(child => _toJSON(child, { omitProps }));
2727

2828
// Function props get noisy in debug output, so we'll exclude them
29+
// Also exclude any props configured via options.omitProps
2930
let renderedProps = {};
3031
Object.keys(props).filter(name => {
31-
if (typeof props[name] !== 'function') {
32+
if (typeof props[name] !== 'function' && !omitProps.includes(name)) {
3233
renderedProps[name] = props[name];
3334
}
3435
});

typings/pretty-print.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ import { NativeTestInstance } from './query-helpers';
33
export function prettyPrint(
44
element: NativeTestInstance | string,
55
maxLength?: number,
6+
options?: {
7+
debug: {
8+
omitProps: string[];
9+
};
10+
},
611
): string | false;

0 commit comments

Comments
 (0)