import { render, waitFor } from '@testing-library/react-native'; import * as React from 'react'; import { AccessibilityInfo, Platform } from 'react-native'; import { FlatList } from './FlatList'; beforeEach(() => { jest.clearAllMocks(); AccessibilityInfo.announceForAccessibility = jest.fn(); }); Platform.OS = 'android'; describe('FlatList', () => { describe('listType "dynamic"', () => { it('does not announce the number of items on first render', async () => { render( null} />, ); await waitFor(() => { expect( AccessibilityInfo.announceForAccessibility, ).not.toHaveBeenCalled(); }); }); it.each` sliceStart | sliceEnd | count | type ${0} | ${2} | ${2} | ${'plural'} ${1} | ${2} | ${1} | ${'singular'} `( 'announce the number of items displayed when the data changes', ({ sliceStart, sliceEnd, count, type }) => { const messages: Record = { singular: '%count% item found', plural: '%count% items found', }; const renderAPI = render( null} />, ); renderAPI.update( null} />, ); expect(AccessibilityInfo.announceForAccessibility).toHaveBeenCalledWith( messages[type].replace('%count%', count), ); }, ); it.each` sliceStart | sliceEnd ${0} | ${2} ${1} | ${2} `('resets the count of announced items', ({ sliceStart, sliceEnd }) => { const messages: Record = { singular: '%count% item found', plural: '%count% items found', }; const renderAPI = render( null} />, ); renderAPI.update( null} />, ); renderAPI.update( null} />, ); renderAPI.update( null} />, ); expect(AccessibilityInfo.announceForAccessibility).toHaveBeenCalledTimes( 2, ); }); it('does not announce if the list shows again all the items', () => { const { update } = render( null} />, ); update( null} />, ); expect(AccessibilityInfo.announceForAccessibility).not.toHaveBeenCalled(); }); it('allows using a custom function to detect if the number of items displayed is plural', () => { const isPlural = jest.fn().mockReturnValue(false); const { update } = render( null} />, ); update( null} />, ); expect(isPlural).toHaveBeenCalledWith(2); expect(AccessibilityInfo.announceForAccessibility).toHaveBeenCalledWith( '2 item found', ); }); }); describe('listType="static"', () => { it.each` sliceStart | sliceEnd | count | type ${0} | ${2} | ${2} | ${'plural'} ${1} | ${2} | ${1} | ${'singular'} `( 'does not announce the number of items displayed when the data changes', ({ sliceStart, sliceEnd }) => { const renderAPI = render( null} />, ); renderAPI.update( null} />, ); expect( AccessibilityInfo.announceForAccessibility, ).not.toHaveBeenCalled(); }, ); it('uses the given rowCount when specified', () => { const { toJSON } = render( null} />, ); expect(toJSON()).toMatchInlineSnapshot(` `); }); it('divides the number of items by the number of columns', () => { const { toJSON } = render( null} />, ); expect(toJSON()).toMatchInlineSnapshot(` `); }); it('sets rowsCount to 0 if the data is undefined', () => { const { toJSON } = render( null} />, ); expect(toJSON()).toMatchInlineSnapshot(` `); }); }); }); jest.mock('./ListWrapper', () => { const { View } = jest.requireActual('react-native'); return { // @ts-ignore ListWrapper: props => , }; }); const DATA = [ { id: 'bd7acbea-c1b1-46c2-aed5-3ad53abb28ba', title: 'Formidable', }, { id: '3ac68afc-c605-48d3-a4f8-fbd91aa97f63', title: 'React Native', }, { id: '58694a0f-3da1-471f-bd96-145571e29d72', title: 'Typescript', }, ];