-
Notifications
You must be signed in to change notification settings - Fork 24k
/
VirtualizedList-test.js
459 lines (417 loc) · 14.6 KB
/
VirtualizedList-test.js
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+react_native
*/
'use strict';
const React = require('react');
const ReactTestRenderer = require('react-test-renderer');
const VirtualizedList = require('../VirtualizedList');
describe('VirtualizedList', () => {
it('renders simple list', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders simple list using ListItemComponent', () => {
function ListItemComponent({item}) {
return <item value={item.key} />;
}
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
ListItemComponent={ListItemComponent}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(component).toMatchSnapshot();
});
it('warns if both renderItem or ListItemComponent are specified. Uses ListItemComponent', () => {
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
function ListItemComponent({item}) {
return <item value={item.key} testID={`${item.key}-ListItemComponent`} />;
}
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}]}
ListItemComponent={ListItemComponent}
renderItem={({item}) => (
<item value={item.key} testID={`${item.key}-renderItem`} />
)}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(console.warn).toBeCalledWith(
'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take precedence over renderItem.',
);
expect(component).toMatchSnapshot();
console.warn.mockRestore();
});
it('throws if no renderItem or ListItemComponent', () => {
// Silence the React error boundary warning; we expect an uncaught error.
const consoleError = console.error;
jest.spyOn(console, 'error').mockImplementation(message => {
if (message.startsWith('The above error occured in the ')) {
return;
}
consoleError(message);
});
const componentFactory = () =>
ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(componentFactory).toThrow(
'VirtualizedList: Either ListItemComponent or renderItem props are required but none were found.',
);
console.error.mockRestore();
});
it('renders empty list', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders null list', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={undefined}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => 0}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders empty list with empty component', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[]}
ListEmptyComponent={() => <empty />}
ListFooterComponent={() => <footer />}
ListHeaderComponent={() => <header />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders list with empty component', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'hello'}]}
ListEmptyComponent={() => <empty />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders all the bells and whistles', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
ItemSeparatorComponent={() => <separator />}
ListEmptyComponent={() => <empty />}
ListFooterComponent={() => <footer />}
ListHeaderComponent={() => <header />}
data={new Array(5).fill().map((_, ii) => ({id: String(ii)}))}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
getItemLayout={({index}) => ({length: 50, offset: index * 50})}
inverted={true}
keyExtractor={(item, index) => item.id}
onRefresh={jest.fn()}
refreshing={false}
renderItem={({item}) => <item value={item.id} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('test getItem functionality where data is not an Array', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={new Map([['id_0', {key: 'item_0'}]])}
getItem={(data, index) => data.get('id_' + index)}
getItemCount={(data: Map) => data.size}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('handles separators correctly', () => {
const infos = [];
const component = ReactTestRenderer.create(
<VirtualizedList
ItemSeparatorComponent={props => <separator {...props} />}
data={[{key: 'i0'}, {key: 'i1'}, {key: 'i2'}]}
renderItem={info => {
infos.push(info);
return <item title={info.item.key} />;
}}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(component).toMatchSnapshot();
infos[1].separators.highlight();
expect(component).toMatchSnapshot();
infos[2].separators.updateProps('leading', {press: true});
expect(component).toMatchSnapshot();
infos[1].separators.unhighlight();
});
it('handles nested lists', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'outer0'}, {key: 'outer1'}]}
renderItem={outerInfo => (
<VirtualizedList
data={[
{key: outerInfo.item.key + ':inner0'},
{key: outerInfo.item.key + ':inner1'},
]}
horizontal={outerInfo.item.key === 'outer1'}
renderItem={innerInfo => {
return <item title={innerInfo.item.key} />;
}}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>
)}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
expect(component).toMatchSnapshot();
});
it('returns the viewableItems correctly in the onViewableItemsChanged callback after changing the data', () => {
const ITEM_HEIGHT = 800;
let data = [{key: 'i1'}, {key: 'i2'}, {key: 'i3'}];
const nativeEvent = {
contentOffset: {y: 0, x: 0},
layoutMeasurement: {width: 300, height: 600},
contentSize: {width: 300, height: data.length * ITEM_HEIGHT},
zoomScale: 1,
contentInset: {right: 0, top: 0, left: 0, bottom: 0},
};
const onViewableItemsChanged = jest.fn();
const props = {
data,
renderItem: ({item}) => <item value={item.key} />,
getItem: (items, index) => items[index],
getItemCount: items => items.length,
getItemLayout: (items, index) => ({
length: ITEM_HEIGHT,
offset: ITEM_HEIGHT * index,
index,
}),
onViewableItemsChanged,
};
const component = ReactTestRenderer.create(<VirtualizedList {...props} />);
const instance = component.getInstance();
instance._onScrollBeginDrag({nativeEvent});
instance._onScroll({
timeStamp: 1000,
nativeEvent,
});
expect(onViewableItemsChanged).toHaveBeenCalledTimes(1);
expect(onViewableItemsChanged).toHaveBeenLastCalledWith(
expect.objectContaining({
viewableItems: [expect.objectContaining({isViewable: true, key: 'i1'})],
}),
);
data = [{key: 'i4'}, ...data];
component.update(<VirtualizedList {...props} data={data} />);
instance._onScroll({
timeStamp: 2000,
nativeEvent: {
...nativeEvent,
contentOffset: {y: 100, x: 0},
},
});
expect(onViewableItemsChanged).toHaveBeenCalledTimes(2);
expect(onViewableItemsChanged).toHaveBeenLastCalledWith(
expect.objectContaining({
viewableItems: [expect.objectContaining({isViewable: true, key: 'i4'})],
}),
);
});
it('getScrollRef for case where it returns a ScrollView', () => {
const listRef = React.createRef(null);
ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
ref={listRef}
/>,
);
const scrollRef = listRef.current.getScrollRef();
// This is checking if the ref acts like a ScrollView. If we had an
// `isScrollView(ref)` method, that would be preferred.
expect(scrollRef.scrollTo).toBeInstanceOf(Function);
});
it('getScrollRef for case where it returns a View', () => {
const listRef = React.createRef(null);
ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'outer0'}, {key: 'outer1'}]}
renderItem={outerInfo => (
<VirtualizedList
data={[
{key: outerInfo.item.key + ':inner0'},
{key: outerInfo.item.key + ':inner1'},
]}
renderItem={innerInfo => {
return <item title={innerInfo.item.key} />;
}}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
ref={listRef}
/>
)}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
const scrollRef = listRef.current.getScrollRef();
// This is checking if the ref acts like a host component. If we had an
// `isHostComponent(ref)` method, that would be preferred.
expect(scrollRef.measure).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureLayout).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureInWindow).toBeInstanceOf(jest.fn().constructor);
});
it('does not call onEndReached when onContentSizeChange happens after onLayout', () => {
const ITEM_HEIGHT = 40;
const layout = {width: 300, height: 600};
let data = Array(20)
.fill()
.map((_, key) => ({key: String(key)}));
const onEndReached = jest.fn();
const props = {
data,
initialNumToRender: 10,
onEndReachedThreshold: 2,
windowSize: 21,
renderItem: ({item}) => <item value={item.key} />,
getItem: (items, index) => items[index],
getItemCount: items => items.length,
getItemLayout: (items, index) => ({
length: ITEM_HEIGHT,
offset: ITEM_HEIGHT * index,
index,
}),
onEndReached,
};
const component = ReactTestRenderer.create(<VirtualizedList {...props} />);
const instance = component.getInstance();
instance._onLayout({nativeEvent: {layout}});
const initialContentHeight = props.initialNumToRender * ITEM_HEIGHT;
// We want to test the unusual case of onContentSizeChange firing after
// onLayout, which can cause https://github.com/facebook/react-native/issues/16067
instance._onContentSizeChange(300, initialContentHeight);
instance._onContentSizeChange(300, data.length * ITEM_HEIGHT);
jest.runAllTimers();
expect(onEndReached).not.toHaveBeenCalled();
instance._onScroll({
timeStamp: 1000,
nativeEvent: {
contentOffset: {y: initialContentHeight, x: 0},
layoutMeasurement: layout,
contentSize: {...layout, height: data.length * ITEM_HEIGHT},
zoomScale: 1,
contentInset: {right: 0, top: 0, left: 0, bottom: 0},
},
});
jest.runAllTimers();
expect(onEndReached).toHaveBeenCalled();
});
it('provides a trace when a listKey collision occurs', () => {
const errors = [];
jest.spyOn(console, 'error').mockImplementation((...args) => {
// Silence the DEV-only React error boundary warning.
if ((args[0] || '').startsWith('The above error occured in the ')) {
return;
}
errors.push(args);
});
const commonProps = {
data: [{key: 'cell0'}],
getItem: (data, index) => data[index],
getItemCount: data => data.length,
renderItem: ({item}) => <item value={item.key} />,
};
try {
ReactTestRenderer.create(
<VirtualizedList
{...commonProps}
horizontal={false}
listKey="level0"
renderItem={() => (
<VirtualizedList
{...commonProps}
horizontal={true}
listKey="level1"
renderItem={() => (
<>
{/* Force a collision */}
<VirtualizedList
{...commonProps}
horizontal={true}
listKey="level2"
/>
<VirtualizedList
{...commonProps}
horizontal={true}
listKey="level2"
/>
</>
)}
/>
)}
/>,
);
expect(errors).toMatchInlineSnapshot(`
Array [
Array [
"A VirtualizedList contains a cell which itself contains more than one VirtualizedList of the same orientation as the parent list. You must pass a unique listKey prop to each sibling list.
VirtualizedList trace:
Child (horizontal):
listKey: level2
cellKey: cell0
Parent (horizontal):
listKey: level1
cellKey: cell0
Parent (vertical):
listKey: level0
cellKey: rootList",
],
]
`);
} finally {
console.error.mockRestore();
}
});
});