/** * 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( } getItem={(data, index) => data[index]} getItemCount={data => data.length} />, ); expect(component).toMatchSnapshot(); }); it('renders simple list using ListItemComponent', () => { function ListItemComponent({item}) { return ; } const component = ReactTestRenderer.create( 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 ; } const component = ReactTestRenderer.create( ( )} 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( 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( } getItem={(data, index) => data[index]} getItemCount={data => data.length} />, ); expect(component).toMatchSnapshot(); }); it('renders null list', () => { const component = ReactTestRenderer.create( } getItem={(data, index) => data[index]} getItemCount={data => 0} />, ); expect(component).toMatchSnapshot(); }); it('renders empty list with empty component', () => { const component = ReactTestRenderer.create( } ListFooterComponent={() =>