Skip to content

Commit 6fbbe2c

Browse files
authored
feat: make debug.deep more usable (#40)
<!-- Please provide enough information so that others can review your pull request. --> <!-- Keep pull requests small and focused on a single change. --> ### Summary Make `debug.deep` more usable by allowing it to print arbitrary JSON (and actually any) files. This proves handy when we want to debug async component which we want to snapshot after async operations: ```jsx const { toJSON, getByName } = render( <Button onPress={jest.fn} text="Press me" /> ); fireEvent.press(getByName('TouchableOpacity')); await flushMicrotasksQueue(); debug.deep(toJSON()); ``` ### Test plan added one
1 parent 44ca90c commit 6fbbe2c

File tree

6 files changed

+74
-12
lines changed

6 files changed

+74
-12
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ There's also `debug.deep` that renders deeply to stdout.
295295
import { debug } from 'react-native-testing-library';
296296

297297
debug.deep(<Component />);
298+
debug.deep(toJSON(), 'actually debug JSON too'); // useful when Component state changes
298299
```
299300

300301
logs:

Diff for: src/__tests__/__snapshots__/debug.test.js.snap

+25-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
exports[`debug 1`] = `
44
"<TouchableOpacity
55
activeOpacity={0.2}
6-
onPress={[Function bound fn]}
6+
onPress={[Function _callee]}
77
>
88
<TextComponent
9-
text=\\"Press me\\"
9+
text=\\"Press me 0\\"
1010
/>
1111
</TouchableOpacity>"
1212
`;
@@ -28,7 +28,29 @@ exports[`debug.deep 1`] = `
2828
}
2929
>
3030
<Text>
31-
Press me
31+
Press me 0
32+
</Text>
33+
</View>"
34+
`;
35+
36+
exports[`debug.deep async test 1`] = `
37+
"<View
38+
accessible={true}
39+
isTVSelectable={true}
40+
onResponderGrant={[Function bound touchableHandleResponderGrant]}
41+
onResponderMove={[Function bound touchableHandleResponderMove]}
42+
onResponderRelease={[Function bound touchableHandleResponderRelease]}
43+
onResponderTerminate={[Function bound touchableHandleResponderTerminate]}
44+
onResponderTerminationRequest={[Function bound touchableHandleResponderTerminationRequest]}
45+
onStartShouldSetResponder={[Function bound touchableHandleStartShouldSetResponder]}
46+
style={
47+
Object {
48+
\\"opacity\\": 1,
49+
}
50+
}
51+
>
52+
<Text>
53+
Press me 1
3254
</Text>
3355
</View>"
3456
`;

Diff for: src/__tests__/debug.test.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
import React from 'react';
44
import { TouchableOpacity, Text } from 'react-native';
55
import stripAnsi from 'strip-ansi';
6-
import { debug } from '..';
6+
import { debug, render, fireEvent, flushMicrotasksQueue } from '..';
77

88
function TextComponent({ text }) {
99
return <Text>{text}</Text>;
1010
}
1111

12-
class Button extends React.Component<*> {
12+
class Button extends React.Component<*, *> {
13+
state = { counter: 0 };
14+
15+
onPress = async () => {
16+
await Promise.resolve();
17+
18+
this.setState({ counter: 1 });
19+
this.props.onPress();
20+
};
21+
1322
render() {
1423
return (
15-
<TouchableOpacity onPress={this.props.onPress}>
16-
<TextComponent text={this.props.text} />
24+
<TouchableOpacity onPress={this.onPress}>
25+
<TextComponent text={`${this.props.text} ${this.state.counter}`} />
1726
</TouchableOpacity>
1827
);
1928
}
@@ -58,3 +67,20 @@ test('debug.deep', () => {
5867

5968
expect(console.log).toBeCalledWith(output, 'test message');
6069
});
70+
71+
test('debug.deep async test', async () => {
72+
// $FlowFixMe
73+
console.log = jest.fn();
74+
const { toJSON, getByName } = render(
75+
<Button onPress={jest.fn} text="Press me" />
76+
);
77+
78+
fireEvent.press(getByName('TouchableOpacity'));
79+
await flushMicrotasksQueue();
80+
81+
debug.deep(toJSON());
82+
83+
const output = console.log.mock.calls[0][0];
84+
85+
expect(stripAnsi(output)).toMatchSnapshot();
86+
});

Diff for: src/debug.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ function debugShallow(
2020
/**
2121
* Log pretty-printed deep test component instance
2222
*/
23-
function debugDeep(instance: React.Element<*>, message?: any) {
24-
const { toJSON } = render(instance);
25-
26-
console.log(format(toJSON()), message || '');
23+
function debugDeep(
24+
instance: React.Element<*> | ?ReactTestRendererJSON,
25+
message?: any = ''
26+
) {
27+
try {
28+
// We're assuming React.Element<*> here and fallback to
29+
// rendering ?ReactTestRendererJSON
30+
// $FlowFixMe
31+
const { toJSON } = render(instance);
32+
console.log(format(toJSON()), message);
33+
} catch (e) {
34+
console.log(format(instance), message);
35+
}
2736
}
2837

2938
const format = input =>

Diff for: typings/__tests__/index.test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ debug.shallow(<TestComponent />);
9090
debug.shallow(<TestComponent />, 'message');
9191
debug.deep(<TestComponent />);
9292
debug.deep(<TestComponent />, 'message');
93+
debug.deep(tree.toJSON());
9394

9495
const waitBy: Promise<ReactTestInstance> = waitForElement<ReactTestInstance>(
9596
() => tree.getByName('View')

Diff for: typings/index.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export type DebugFunction = (
5858

5959
export type DebugAPI = DebugFunction & {
6060
shallow: DebugFunction;
61-
deep: (instance: React.ReactElement<any>, message?: string) => void;
61+
deep: (
62+
instance: React.ReactElement<any> | ReactTestRendererJSON | null,
63+
message?: string
64+
) => void;
6265
};
6366

6467
export declare const render: (

0 commit comments

Comments
 (0)