11import * as NativeNavigation from '@react-navigation/native' ;
22import { fireEvent , render , screen } from '@testing-library/react-native' ;
3- import { useState } from 'react' ;
43import { SectionList } from 'react-native' ;
54import BaseSelectionList from '@components/SelectionList/BaseSelectionList' ;
65import RadioListItem from '@components/SelectionList/RadioListItem' ;
@@ -11,9 +10,6 @@ import CONST from '@src/CONST';
1110type BaseSelectionListSections < TItem extends ListItem > = {
1211 sections : SelectionListProps < TItem > [ 'sections' ] ;
1312 canSelectMultiple ?: boolean ;
14- initialNumToRender ?: number ;
15- searchText ?: string ;
16- setSearchText ?: ( searchText : string ) => void ;
1713} ;
1814
1915const mockSections = Array . from ( { length : 10 } , ( _ , index ) => ( {
@@ -22,18 +18,6 @@ const mockSections = Array.from({length: 10}, (_, index) => ({
2218 isSelected : index === 1 ,
2319} ) ) ;
2420
25- const largeMockSections = Array . from ( { length : 100 } , ( _ , index ) => ( {
26- text : `Item ${ index } ` ,
27- keyForList : `${ index } ` ,
28- isSelected : index === 1 ,
29- } ) ) ;
30-
31- const largeMockSectionsWithSelectedItemFromSecondPage = Array . from ( { length : 100 } , ( _ , index ) => ( {
32- text : `Item ${ index } ` ,
33- keyForList : `${ index } ` ,
34- isSelected : index === 70 ,
35- } ) ) ;
36-
3721jest . mock ( '@src/components/ConfirmedRoute.tsx' ) ;
3822jest . mock ( '@react-navigation/native' , ( ) => {
3923 const actualNav = jest . requireActual < typeof Navigation > ( '@react-navigation/native' ) ;
@@ -44,31 +28,20 @@ jest.mock('@react-navigation/native', () => {
4428 } ;
4529} ) ;
4630
47- jest . mock ( '@hooks/useLocalize' , ( ) =>
48- jest . fn ( ( ) => ( {
49- translate : jest . fn ( ( key : string ) => key ) ,
50- numberFormat : jest . fn ( ( num : number ) => num . toString ( ) ) ,
51- } ) ) ,
52- ) ;
53-
5431describe ( 'BaseSelectionList' , ( ) => {
5532 const onSelectRowMock = jest . fn ( ) ;
5633
5734 function BaseListItemRenderer < TItem extends ListItem > ( props : BaseSelectionListSections < TItem > ) {
58- const { sections, canSelectMultiple, initialNumToRender , setSearchText , searchText } = props ;
35+ const { sections, canSelectMultiple} = props ;
5936 const focusedKey = sections [ 0 ] . data . find ( ( item ) => item . isSelected ) ?. keyForList ;
6037 return (
6138 < BaseSelectionList
6239 sections = { sections }
63- textInputLabel = "common.search"
6440 ListItem = { RadioListItem }
6541 onSelectRow = { onSelectRowMock }
6642 shouldSingleExecuteRowSelect
6743 canSelectMultiple = { canSelectMultiple }
6844 initiallyFocusedOptionKey = { focusedKey }
69- initialNumToRender = { initialNumToRender }
70- onChangeText = { setSearchText }
71- textInputValue = { searchText }
7245 />
7346 ) ;
7447 }
@@ -114,102 +87,4 @@ describe('BaseSelectionList', () => {
11487 fireEvent . press ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 0` ) ) ;
11588 expect ( spy ) . toHaveBeenCalledWith ( expect . objectContaining ( { itemIndex : 0 } ) ) ;
11689 } ) ;
117-
118- it ( 'should show only elements from first page and Show More button when items exceed page limit' , ( ) => {
119- render (
120- < BaseListItemRenderer
121- sections = { [ { data : largeMockSections } ] }
122- canSelectMultiple = { false }
123- initialNumToRender = { 60 }
124- /> ,
125- ) ;
126-
127- // Should render exactly first page (50 items)
128- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 0` ) ) . toBeTruthy ( ) ;
129- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 49` ) ) . toBeTruthy ( ) ;
130-
131- // Should NOT render items from second page
132- expect ( screen . queryByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 50` ) ) . toBeFalsy ( ) ;
133- expect ( screen . queryByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 99` ) ) . toBeFalsy ( ) ;
134-
135- expect ( screen . getByText ( 'common.showMore' ) ) . toBeTruthy ( ) ;
136- expect ( screen . getByText ( '50' ) ) . toBeTruthy ( ) ;
137- expect ( screen . getByText ( '100' ) ) . toBeTruthy ( ) ;
138- } ) ;
139-
140- it ( 'should hide Show More button when items fit on one page' , ( ) => {
141- render (
142- < BaseListItemRenderer
143- sections = { [ { data : mockSections } ] }
144- canSelectMultiple = { false }
145- initialNumToRender = { 60 }
146- /> ,
147- ) ;
148-
149- expect ( screen . queryByText ( 'common.showMore' ) ) . toBeFalsy ( ) ;
150- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 0` ) ) . toBeTruthy ( ) ;
151- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 9` ) ) . toBeTruthy ( ) ;
152- } ) ;
153-
154- it ( 'should load more items when Show More button is clicked' , ( ) => {
155- render (
156- < BaseListItemRenderer
157- sections = { [ { data : largeMockSections } ] }
158- canSelectMultiple = { false }
159- initialNumToRender = { 110 }
160- /> ,
161- ) ;
162-
163- // Click Show More button
164- fireEvent . press ( screen . getByText ( 'common.showMore' ) ) ;
165-
166- // Should now show items from second page
167- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 50` ) ) . toBeTruthy ( ) ;
168- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 99` ) ) . toBeTruthy ( ) ;
169-
170- // Should not show, Show more button as we rendered whole list
171- expect ( screen . queryByText ( 'common.showMore' ) ) . toBeFalsy ( ) ;
172- } ) ;
173-
174- it ( 'should search for first item then scroll back to preselected item when search is cleared' , ( ) => {
175- function SearchableListWrapper ( ) {
176- const [ searchText , setSearchText ] = useState ( '' ) ;
177-
178- // Filter sections based on search text
179- const filteredSections = searchText
180- ? largeMockSectionsWithSelectedItemFromSecondPage . filter ( ( item ) => item . text . toLowerCase ( ) . includes ( searchText . toLowerCase ( ) ) )
181- : largeMockSectionsWithSelectedItemFromSecondPage ;
182-
183- return (
184- < BaseListItemRenderer
185- sections = { [ { data : filteredSections } ] }
186- searchText = { searchText }
187- setSearchText = { setSearchText }
188- canSelectMultiple = { false }
189- initialNumToRender = { 110 }
190- />
191- ) ;
192- }
193-
194- render ( < SearchableListWrapper /> ) ;
195-
196- // Initially should show item 70 as selected and visible
197- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 70` ) ) . toBeTruthy ( ) ;
198- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 70` ) ) . toBeSelected ( ) ;
199-
200- // Search for "Item 0"
201- fireEvent . changeText ( screen . getByTestId ( 'selection-list-text-input' ) , 'Item 0' ) ;
202-
203- // Should show only the first item (Item 0) in search results
204- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 0` ) ) . toBeTruthy ( ) ;
205- expect ( screen . queryByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 70` ) ) . toBeFalsy ( ) ;
206- expect ( screen . queryByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 1` ) ) . toBeFalsy ( ) ;
207-
208- // Clear the search text
209- fireEvent . changeText ( screen . getByTestId ( 'selection-list-text-input' ) , '' ) ;
210-
211- // Should scroll back to and show the preselected item 70
212- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 70` ) ) . toBeTruthy ( ) ;
213- expect ( screen . getByTestId ( `${ CONST . BASE_LIST_ITEM_TEST_ID } 70` ) ) . toBeSelected ( ) ;
214- } ) ;
21590} ) ;
0 commit comments