@@ -346,6 +346,71 @@ describe('Autocomplete', () => {
346346 expect ( child ) . toHaveClass ( 'additional-class' ) ;
347347 } ) ;
348348
349+ describe ( 'getTextFieldChild' , ( ) => {
350+ it ( 'renders correctly when a single TextField is provided' , ( ) => {
351+ renderAutocomplete ( {
352+ children : < TextField label = "Autocomplete field" name = "autocomplete_field" /> ,
353+ } ) ;
354+
355+ const input = screen . getByLabelText ( 'Autocomplete field' ) ;
356+ expect ( input ) . toBeInTheDocument ( ) ;
357+
358+ expect ( input . tagName ) . toBe ( 'INPUT' ) ;
359+
360+ expect ( screen . getByRole ( 'combobox' , { name : / a u t o c o m p l e t e f i e l d / i } ) ) . toBeInTheDocument ( ) ;
361+ } ) ;
362+
363+ it ( 'renders only the last TextField when multiple are provided' , ( ) => {
364+ renderAutocomplete ( {
365+ children : [
366+ < TextField key = "1" label = "First field" name = "first_field" /> ,
367+ < TextField key = "2" label = "Second field" name = "second_field" /> ,
368+ ] ,
369+ } ) ;
370+
371+ expect ( screen . queryByLabelText ( 'First field' ) ) . not . toBeInTheDocument ( ) ;
372+ expect ( screen . getByLabelText ( 'Second field' ) ) . toBeInTheDocument ( ) ;
373+ } ) ;
374+
375+ it ( 'renders correctly when children include one TextField and one non-TextField element' , ( ) => {
376+ renderAutocomplete ( {
377+ children : [
378+ < div key = "1" data-testid = "non-text-field" >
379+ Not a TextField
380+ </ div > ,
381+ < TextField key = "2" label = "Valid field" name = "valid_field" /> ,
382+ ] ,
383+ } ) ;
384+
385+ const input = screen . getByLabelText ( 'Valid field' ) ;
386+ expect ( input ) . toBeInTheDocument ( ) ;
387+
388+ expect ( screen . queryByTestId ( 'non-text-field' ) ) . not . toBeInTheDocument ( ) ;
389+ } ) ;
390+
391+ describe ( 'error cases' , ( ) => {
392+ beforeEach ( ( ) => {
393+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
394+ } ) ;
395+
396+ afterEach ( ( ) => {
397+ ( console . error as jest . Mock ) . mockRestore ( ) ;
398+ } ) ;
399+
400+ it ( 'throws an error when no TextField child is provided' , ( ) => {
401+ expect ( ( ) =>
402+ renderAutocomplete ( {
403+ children : < div > Not a text field</ div > ,
404+ } )
405+ ) . toThrow ( ) ;
406+ } ) ;
407+
408+ it ( 'throws an error when children are missing entirely' , ( ) => {
409+ expect ( ( ) => renderAutocomplete ( { children : undefined } ) ) . toThrow ( ) ;
410+ } ) ;
411+ } ) ;
412+ } ) ;
413+
349414 describe ( 'default props' , ( ) => {
350415 describe ( 'ariaClearLabel deprecation' , ( ) => {
351416 const originalEnv = process . env . NODE_ENV ;
0 commit comments