|
| 1 | +import * as React from 'react'; |
| 2 | +import { FlatList, RefreshControl, ScrollView, SectionList, Text, View } from 'react-native'; |
| 3 | + |
| 4 | +import { render, screen, userEvent } from '../../..'; |
| 5 | + |
| 6 | +describe('pullToRefresh()', () => { |
| 7 | + it('supports ScrollView', async () => { |
| 8 | + const onRefreshMock = jest.fn(); |
| 9 | + await render( |
| 10 | + <ScrollView |
| 11 | + testID="view" |
| 12 | + refreshControl={<RefreshControl refreshing={false} onRefresh={onRefreshMock} />} |
| 13 | + />, |
| 14 | + ); |
| 15 | + const user = userEvent.setup(); |
| 16 | + |
| 17 | + await user.pullToRefresh(screen.getByTestId('view')); |
| 18 | + expect(onRefreshMock).toHaveBeenCalled(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('supports FlatList', async () => { |
| 22 | + const onRefreshMock = jest.fn(); |
| 23 | + await render( |
| 24 | + <FlatList |
| 25 | + testID="view" |
| 26 | + data={['A', 'B', 'C']} |
| 27 | + renderItem={({ item }) => <Text>{item}</Text>} |
| 28 | + refreshControl={<RefreshControl refreshing={false} onRefresh={onRefreshMock} />} |
| 29 | + />, |
| 30 | + ); |
| 31 | + const user = userEvent.setup(); |
| 32 | + |
| 33 | + await user.pullToRefresh(screen.getByTestId('view')); |
| 34 | + expect(onRefreshMock).toHaveBeenCalled(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('supports SectionList', async () => { |
| 38 | + const onRefreshMock = jest.fn(); |
| 39 | + await render( |
| 40 | + <SectionList |
| 41 | + testID="view" |
| 42 | + sections={[ |
| 43 | + { title: 'Section 1', data: ['A', 'B', 'C'] }, |
| 44 | + { title: 'Section 2', data: ['D', 'E', 'F'] }, |
| 45 | + ]} |
| 46 | + renderItem={({ item }) => <Text>{item}</Text>} |
| 47 | + refreshControl={<RefreshControl refreshing={false} onRefresh={onRefreshMock} />} |
| 48 | + />, |
| 49 | + ); |
| 50 | + const user = userEvent.setup(); |
| 51 | + |
| 52 | + await user.pullToRefresh(screen.getByTestId('view')); |
| 53 | + expect(onRefreshMock).toHaveBeenCalled(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('does not throw when RefreshControl is not set', async () => { |
| 57 | + await render(<ScrollView testID="view" />); |
| 58 | + const user = userEvent.setup(); |
| 59 | + |
| 60 | + await expect(user.pullToRefresh(screen.getByTestId('view'))).resolves.toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not throw when RefreshControl onRefresh is not set', async () => { |
| 64 | + await render( |
| 65 | + <ScrollView testID="view" refreshControl={<RefreshControl refreshing={false} />} />, |
| 66 | + ); |
| 67 | + const user = userEvent.setup(); |
| 68 | + |
| 69 | + await expect(user.pullToRefresh(screen.getByTestId('view'))).resolves.toBeUndefined(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('throws when passed a non-ScrollView element', async () => { |
| 73 | + await render(<View testID="view" />); |
| 74 | + const user = userEvent.setup(); |
| 75 | + |
| 76 | + await expect(user.pullToRefresh(screen.getByTestId('view'))).rejects.toThrow( |
| 77 | + /pullToRefresh\(\) works only with host "ScrollView" elements/, |
| 78 | + ); |
| 79 | + }); |
| 80 | +}); |
0 commit comments