-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathrender-debug.test.tsx
175 lines (145 loc) · 4.72 KB
/
render-debug.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as React from 'react';
import { Pressable, Text, TextInput, View } from 'react-native';
import { configure, fireEvent, render, screen } from '..';
import { logger } from '../helpers/logger';
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
const INPUT_FRESHNESS = 'Custom Freshie';
const INPUT_CHEF = 'I inspected freshie';
const DEFAULT_INPUT_CHEF = 'What did you inspect?';
const DEFAULT_INPUT_CUSTOMER = 'What banana?';
const ignoreWarnings = ['Using debug("message") is deprecated'];
beforeEach(() => {
jest.spyOn(logger, 'info').mockImplementation(() => {});
jest.spyOn(logger, 'warn').mockImplementation((message) => {
if (!ignoreWarnings.some((warning) => `${message}`.includes(warning))) {
logger.warn(message);
}
});
});
afterEach(() => {
jest.restoreAllMocks();
});
interface MyButtonProps {
onPress: () => void;
children: string;
}
function MyButton(props: MyButtonProps) {
return (
<Pressable role="button" onPress={props.onPress}>
<Text>{props.children}</Text>
</Pressable>
);
}
class Banana extends React.Component<any, { fresh: boolean }> {
state = {
fresh: false,
};
componentDidUpdate() {
if (this.props.onUpdate) {
this.props.onUpdate();
}
}
componentWillUnmount() {
if (this.props.onUnmount) {
this.props.onUnmount();
}
}
changeFresh = () => {
this.setState((state) => ({
fresh: !state.fresh,
}));
};
render() {
const test = 0;
return (
<View>
<Text>Is the banana fresh?</Text>
<Text testID="bananaFresh">{this.state.fresh ? 'fresh' : 'not fresh'}</Text>
<TextInput
testID="bananaCustomFreshness"
placeholder={PLACEHOLDER_FRESHNESS}
value={INPUT_FRESHNESS}
/>
<TextInput
testID="bananaChef"
placeholder={PLACEHOLDER_CHEF}
value={INPUT_CHEF}
defaultValue={DEFAULT_INPUT_CHEF}
/>
<TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} />
<TextInput defaultValue={'hello'} value="" />
<MyButton onPress={this.changeFresh}>Change freshness!</MyButton>
<Text testID="duplicateText">First Text</Text>
<Text testID="duplicateText">Second Text</Text>
<Text>{test}</Text>
</View>
);
}
}
test('debug', () => {
render(<Banana />);
screen.debug();
screen.debug({ message: 'another custom message' });
screen.debug({ mapProps: null });
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot();
expect(`${mockCalls[1][0]}\n${mockCalls[1][1]}`).toMatchSnapshot('Option message');
expect(`${mockCalls[2][0]}\n${mockCalls[2][1]}`).toMatchSnapshot('All Props');
});
test('debug changing component', () => {
render(<Banana />);
fireEvent.press(screen.getByRole('button', { name: 'Change freshness!' }));
screen.debug({ mapProps: null });
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot('bananaFresh button message should now be "fresh"');
});
test('debug with only children prop', () => {
render(<Banana />);
screen.debug({ mapProps: () => ({}) });
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot();
});
test('debug with only prop whose value is bananaChef', () => {
render(<Banana />);
screen.debug({
mapProps: (props) => {
const filterProps: Record<string, unknown> = {};
Object.keys(props).forEach((key) => {
if (props[key] === 'bananaChef') {
filterProps[key] = props[key];
}
});
return filterProps;
},
});
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot();
});
test('debug should use debugOptions from config when no option is specified', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
render(
<View style={{ backgroundColor: 'red' }}>
<Text>hello</Text>
</View>,
);
screen.debug();
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot();
});
test('filtering out props through mapProps option should not modify component', () => {
render(<View testID="viewTestID" />);
screen.debug({ mapProps: () => ({}) });
expect(screen.getByTestId('viewTestID')).toBeTruthy();
});
test('debug should use given options over config debugOptions', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
render(
<View style={{ backgroundColor: 'red' }}>
<Text>hello</Text>
</View>,
);
screen.debug({ mapProps: (props) => props });
const mockCalls = jest.mocked(logger.info).mock.calls;
expect(mockCalls[0][0]).toMatchSnapshot();
});