-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathrender.test.tsx
More file actions
336 lines (261 loc) · 9.03 KB
/
render.test.tsx
File metadata and controls
336 lines (261 loc) · 9.03 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import * as React from 'react';
import { Text, View } from 'react-native';
import { render, screen } from '..';
import { _console, logger } from '../helpers/logger';
test('renders a simple component', async () => {
const TestComponent = () => (
<View testID="container">
<Text>Hello World</Text>
</View>
);
await render(<TestComponent />);
expect(screen.getByTestId('container')).toBeOnTheScreen();
expect(screen.getByText('Hello World')).toBeOnTheScreen();
});
beforeEach(() => {
jest.spyOn(_console, 'warn').mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
});
test('does not warn when no options are passed', async () => {
const TestComponent = () => <Text testID="test">Test</Text>;
await render(<TestComponent />);
expect(_console.warn).not.toHaveBeenCalled();
});
test('supports null options', async () => {
const TestComponent = () => <Text testID="test">Test</Text>;
await render(<TestComponent />, null as any);
expect(screen.getByTestId('test')).toBeOnTheScreen();
expect(_console.warn).not.toHaveBeenCalled();
});
test('does not warn when only valid options are passed', async () => {
const TestComponent = () => <Text testID="test">Test</Text>;
const Wrapper = ({ children }: { children: React.ReactNode }) => <View>{children}</View>;
await render(<TestComponent />, { wrapper: Wrapper });
expect(_console.warn).not.toHaveBeenCalled();
});
test('warns when unknown option is passed', async () => {
const TestComponent = () => <Text testID="test">Test</Text>;
await render(<TestComponent />, { unknownOption: 'value' } as any);
expect(_console.warn).toHaveBeenCalledTimes(1);
expect(jest.mocked(_console.warn).mock.calls[0][0]).toContain(
'Unknown option(s) passed to render: unknownOption',
);
});
describe('render options', () => {
test('renders component with wrapper option', async () => {
const TestComponent = () => <Text testID="inner">Inner Content</Text>;
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<View testID="wrapper">{children}</View>
);
await render(<TestComponent />, { wrapper: Wrapper });
expect(screen.getByTestId('wrapper')).toBeOnTheScreen();
expect(screen.getByTestId('inner')).toBeOnTheScreen();
expect(screen.getByText('Inner Content')).toBeOnTheScreen();
});
});
describe('component rendering', () => {
test('render accepts RCTText component', async () => {
await render(React.createElement('RCTText', { testID: 'text' }, 'Hello'));
expect(screen.getByTestId('text')).toBeOnTheScreen();
expect(screen.getByText('Hello')).toBeOnTheScreen();
});
test('render throws when text string is rendered without Text component', async () => {
await expect(render(<View>Hello</View>)).rejects.toThrowErrorMatchingInlineSnapshot(
`"Invariant Violation: Text strings must be rendered within a <Text> component. Detected attempt to render "Hello" string within a <View> component."`,
);
});
});
describe('rerender', () => {
test('rerender updates component with new props', async () => {
interface TestComponentProps {
message: string;
}
const TestComponent = ({ message }: TestComponentProps) => (
<Text testID="message">{message}</Text>
);
await render(<TestComponent message="Initial" />);
expect(screen.getByText('Initial')).toBeOnTheScreen();
await screen.rerender(<TestComponent message="Updated" />);
expect(screen.getByText('Updated')).toBeOnTheScreen();
expect(screen.queryByText('Initial')).not.toBeOnTheScreen();
});
test('rerender works with wrapper option', async () => {
interface TestComponentProps {
value: number;
}
const TestComponent = ({ value }: TestComponentProps) => <Text testID="value">{value}</Text>;
const Wrapper = ({ children }: { children: React.ReactNode }) => (
<View testID="wrapper">{children}</View>
);
await render(<TestComponent value={1} />, {
wrapper: Wrapper,
});
expect(screen.getByText('1')).toBeOnTheScreen();
await screen.rerender(<TestComponent value={2} />);
expect(screen.getByText('2')).toBeOnTheScreen();
expect(screen.getByTestId('wrapper')).toBeOnTheScreen();
});
});
test('unmount removes component from tree', async () => {
const TestComponent = () => <Text testID="content">Content</Text>;
await render(<TestComponent />);
expect(screen.getByTestId('content')).toBeOnTheScreen();
await screen.unmount();
expect(screen.queryByTestId('content')).not.toBeOnTheScreen();
});
test('rerender calls componentDidUpdate and unmount calls componentWillUnmount', async () => {
interface ClassComponentProps {
onUpdate?: () => void;
onUnmount?: () => void;
}
class ClassComponent extends React.Component<ClassComponentProps> {
componentDidUpdate() {
if (this.props.onUpdate) {
this.props.onUpdate();
}
}
componentWillUnmount() {
if (this.props.onUnmount) {
this.props.onUnmount();
}
}
render() {
return <Text>Class Component</Text>;
}
}
const onUpdate = jest.fn();
const onUnmount = jest.fn();
await render(<ClassComponent onUpdate={onUpdate} onUnmount={onUnmount} />);
expect(onUpdate).toHaveBeenCalledTimes(0);
expect(onUnmount).toHaveBeenCalledTimes(0);
await screen.rerender(<ClassComponent onUpdate={onUpdate} onUnmount={onUnmount} />);
expect(onUpdate).toHaveBeenCalledTimes(1);
expect(onUnmount).toHaveBeenCalledTimes(0);
await screen.unmount();
expect(onUpdate).toHaveBeenCalledTimes(1);
expect(onUnmount).toHaveBeenCalledTimes(1);
});
describe('toJSON', () => {
test('toJSON returns null for empty children', async () => {
const TestComponent = () => null;
await render(<TestComponent />);
expect(screen.toJSON()).toMatchInlineSnapshot(`null`);
});
test('toJSON returns single child when only one child exists', async () => {
const TestComponent = () => (
<View>
<Text testID="single">Single Child</Text>
</View>
);
await render(<TestComponent />);
expect(screen.toJSON()).toMatchInlineSnapshot(`
<View>
<Text
testID="single"
>
Single Child
</Text>
</View>
`);
});
test('toJSON returns full tree for React fragment with multiple children', async () => {
const TestComponent = () => (
<>
<Text testID="first">First</Text>
<Text testID="second">Second</Text>
</>
);
await render(<TestComponent />);
expect(screen.toJSON()).toMatchInlineSnapshot(`
<>
<Text
testID="first"
>
First
</Text>
<Text
testID="second"
>
Second
</Text>
</>
`);
});
});
describe('debug', () => {
beforeEach(() => {
jest.spyOn(logger, 'info').mockImplementation(() => {});
});
afterEach(() => {
jest.restoreAllMocks();
});
test('debug outputs formatted component tree', async () => {
const TestComponent = () => (
<View testID="container">
<Text>Debug Test</Text>
</View>
);
await render(<TestComponent />);
expect(() => {
screen.debug();
}).not.toThrow();
});
test('debug accepts options with message', async () => {
const TestComponent = () => <Text>Test</Text>;
await render(<TestComponent />);
expect(() => {
screen.debug({ message: 'Custom message' });
}).not.toThrow();
});
});
describe('result getters', () => {
test('container getter returns renderer container', async () => {
const TestComponent = () => <Text testID="content">Content</Text>;
const result = await render(<TestComponent />);
expect(result.container).toMatchInlineSnapshot(`
<>
<Text
testID="content"
>
Content
</Text>
</>
`);
});
test('root getter works correctly', async () => {
const TestComponent = () => <View testID="test" />;
const result = await render(<TestComponent />);
expect(result.root).toMatchInlineSnapshot(`
<View
testID="test"
/>
`);
});
});
describe('screen integration', () => {
test('render sets screen queries', async () => {
const TestComponent = () => (
<View>
<Text testID="text1">First Text</Text>
<Text testID="text2">Second Text</Text>
</View>
);
await render(<TestComponent />);
expect(screen.getByTestId('text1')).toBeOnTheScreen();
expect(screen.getByTestId('text2')).toBeOnTheScreen();
expect(screen.getByText('First Text')).toBeOnTheScreen();
expect(screen.getByText('Second Text')).toBeOnTheScreen();
});
test('screen queries work after rerender', async () => {
interface TestComponentProps {
label: string;
}
const TestComponent = ({ label }: TestComponentProps) => <Text testID="label">{label}</Text>;
await render(<TestComponent label="Initial" />);
expect(screen.getByText('Initial')).toBeOnTheScreen();
await screen.rerender(<TestComponent label="Updated" />);
expect(screen.getByText('Updated')).toBeOnTheScreen();
});
});