Skip to content

feat: make debug.deep more usable #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ There's also `debug.deep` that renders deeply to stdout.
import { debug } from 'react-native-testing-library';

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

logs:
Expand Down
28 changes: 25 additions & 3 deletions src/__tests__/__snapshots__/debug.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
exports[`debug 1`] = `
"<TouchableOpacity
activeOpacity={0.2}
onPress={[Function bound fn]}
onPress={[Function _callee]}
>
<TextComponent
text=\\"Press me\\"
text=\\"Press me 0\\"
/>
</TouchableOpacity>"
`;
Expand All @@ -28,7 +28,29 @@ exports[`debug.deep 1`] = `
}
>
<Text>
Press me
Press me 0
</Text>
</View>"
`;

exports[`debug.deep async test 1`] = `
"<View
accessible={true}
isTVSelectable={true}
onResponderGrant={[Function bound touchableHandleResponderGrant]}
onResponderMove={[Function bound touchableHandleResponderMove]}
onResponderRelease={[Function bound touchableHandleResponderRelease]}
onResponderTerminate={[Function bound touchableHandleResponderTerminate]}
onResponderTerminationRequest={[Function bound touchableHandleResponderTerminationRequest]}
onStartShouldSetResponder={[Function bound touchableHandleStartShouldSetResponder]}
style={
Object {
\\"opacity\\": 1,
}
}
>
<Text>
Press me 1
</Text>
</View>"
`;
34 changes: 30 additions & 4 deletions src/__tests__/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
import React from 'react';
import { TouchableOpacity, Text } from 'react-native';
import stripAnsi from 'strip-ansi';
import { debug } from '..';
import { debug, render, fireEvent, flushMicrotasksQueue } from '..';

function TextComponent({ text }) {
return <Text>{text}</Text>;
}

class Button extends React.Component<*> {
class Button extends React.Component<*, *> {
state = { counter: 0 };

onPress = async () => {
await Promise.resolve();

this.setState({ counter: 1 });
this.props.onPress();
};

render() {
return (
<TouchableOpacity onPress={this.props.onPress}>
<TextComponent text={this.props.text} />
<TouchableOpacity onPress={this.onPress}>
<TextComponent text={`${this.props.text} ${this.state.counter}`} />
</TouchableOpacity>
);
}
Expand Down Expand Up @@ -58,3 +67,20 @@ test('debug.deep', () => {

expect(console.log).toBeCalledWith(output, 'test message');
});

test('debug.deep async test', async () => {
// $FlowFixMe
console.log = jest.fn();
const { toJSON, getByName } = render(
<Button onPress={jest.fn} text="Press me" />
);

fireEvent.press(getByName('TouchableOpacity'));
await flushMicrotasksQueue();

debug.deep(toJSON());

const output = console.log.mock.calls[0][0];

expect(stripAnsi(output)).toMatchSnapshot();
});
17 changes: 13 additions & 4 deletions src/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ function debugShallow(
/**
* Log pretty-printed deep test component instance
*/
function debugDeep(instance: React.Element<*>, message?: any) {
const { toJSON } = render(instance);

console.log(format(toJSON()), message || '');
function debugDeep(
instance: React.Element<*> | ?ReactTestRendererJSON,
message?: any = ''
) {
try {
// We're assuming React.Element<*> here and fallback to
// rendering ?ReactTestRendererJSON
// $FlowFixMe
const { toJSON } = render(instance);
console.log(format(toJSON()), message);
} catch (e) {
console.log(format(instance), message);
}
}

const format = input =>
Expand Down
1 change: 1 addition & 0 deletions typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ debug.shallow(<TestComponent />);
debug.shallow(<TestComponent />, 'message');
debug.deep(<TestComponent />);
debug.deep(<TestComponent />, 'message');
debug.deep(tree.toJSON());

const waitBy: Promise<ReactTestInstance> = waitForElement<ReactTestInstance>(
() => tree.getByName('View')
Expand Down
5 changes: 4 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export type DebugFunction = (

export type DebugAPI = DebugFunction & {
shallow: DebugFunction;
deep: (instance: React.ReactElement<any>, message?: string) => void;
deep: (
instance: React.ReactElement<any> | ReactTestRendererJSON | null,
message?: string
) => void;
};

export declare const render: (
Expand Down