-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathrender-debug.test.tsx
207 lines (174 loc) · 5.86 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* eslint-disable no-console */
import * as React from 'react';
import { View, Text, TextInput, Pressable } from 'react-native';
import stripAnsi from 'strip-ansi';
import { render, fireEvent, configure } from '..';
type ConsoleLogMock = jest.Mock<Array<string>>;
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'];
const realConsoleWarn = console.warn;
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation((message) => {
if (!ignoreWarnings.some((warning) => message.includes(warning))) {
realConsoleWarn(message);
}
});
});
afterEach(() => {
jest.restoreAllMocks();
});
class MyButton extends React.Component<any> {
render() {
return (
<Pressable onPress={this.props.onPress}>
<Text>{this.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} type="primary">
Change freshness!
</MyButton>
<Text testID="duplicateText">First Text</Text>
<Text testID="duplicateText">Second Text</Text>
<Text>{test}</Text>
</View>
);
}
}
test('debug', () => {
const { debug } = render(<Banana />);
debug();
debug('my custom message');
debug.shallow();
debug.shallow('my other custom message');
debug({ message: 'another custom message' });
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
expect(stripAnsi(mockCalls[1][0] + mockCalls[1][1])).toMatchSnapshot(
'with message'
);
expect(stripAnsi(mockCalls[2][0])).toMatchSnapshot('shallow');
expect(stripAnsi(mockCalls[3][0] + mockCalls[3][1])).toMatchSnapshot(
'shallow with message'
);
expect(stripAnsi(mockCalls[4][0] + mockCalls[4][1])).toMatchSnapshot(
'another custom message'
);
const mockWarnCalls = (console.warn as any as ConsoleLogMock).mock.calls;
expect(mockWarnCalls[0]).toEqual([
'Using debug("message") is deprecated and will be removed in future release, please use debug({ message; "message" }) instead.',
]);
});
test('debug changing component', () => {
const { UNSAFE_getByProps, debug } = render(<Banana />);
fireEvent.press(UNSAFE_getByProps({ type: 'primary' }));
debug();
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot(
'bananaFresh button message should now be "fresh"'
);
});
test('debug with only children prop', () => {
const { debug } = render(<Banana />);
debug({ mapProps: () => ({}) });
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug with only prop whose value is bananaChef', () => {
const { debug } = render(<Banana />);
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 = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug with only props from TextInput components', () => {
const { debug } = render(<Banana />);
debug({
mapProps: (props, node) => (node.type === 'TextInput' ? props : {}),
});
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('debug should use debugOptions from config when no option is specified', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
const { debug } = render(
<View style={{ backgroundColor: 'red' }}>
<Text>hello</Text>
</View>
);
debug();
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});
test('filtering out props through mapProps option should not modify component', () => {
const { debug, getByTestId } = render(<View testID="viewTestID" />);
debug({ mapProps: () => ({}) });
expect(getByTestId('viewTestID')).toBeTruthy();
});
test('debug should use given options over config debugOptions', () => {
configure({ defaultDebugOptions: { mapProps: () => ({}) } });
const { debug } = render(
<View style={{ backgroundColor: 'red' }}>
<Text>hello</Text>
</View>
);
debug({ mapProps: (props) => props });
const mockCalls = (console.log as any as ConsoleLogMock).mock.calls;
expect(stripAnsi(mockCalls[0][0])).toMatchSnapshot();
});