@@ -31,7 +31,9 @@ const prefix = 'cds';
3131
3232const ControlledComboBox = ( { controlledItem } ) => {
3333 const items = generateItems ( 5 , generateGenericItem ) ;
34- const [ value , setValue ] = useState ( controlledItem || items [ 0 ] ) ;
34+ const [ value , setValue ] = useState (
35+ typeof controlledItem !== 'undefined' ? controlledItem : items [ 0 ]
36+ ) ;
3537 const [ onChangeCallCount , setOnChangeCallCount ] = useState ( 0 ) ;
3638 const [ onInputChangeCallCount , setOnInputChangeCallCount ] = useState ( 0 ) ;
3739 const controlledOnChange = ( { selectedItem } ) => {
@@ -53,7 +55,7 @@ const ControlledComboBox = ({ controlledItem }) => {
5355 placeholder = "Filter..."
5456 type = "default"
5557 />
56- < div > value: { value ?. label || 'none' } </ div >
58+ < div data-testid = "selected-item" > { value ?. label || 'none' } </ div >
5759 < div > onChangeCallCount: { onChangeCallCount } </ div >
5860 < div > onInputChangeCallCount: { onInputChangeCallCount } </ div >
5961 < button onClick = { ( ) => setValue ( items [ 3 ] ) } > Choose item 3</ button >
@@ -375,6 +377,44 @@ describe('ComboBox', () => {
375377 ) ;
376378 } ) ;
377379
380+ it ( 'should yield highlighted item as `selectedItem` when pressing Enter with an unmodified input value' , async ( ) => {
381+ render ( < ControlledComboBox controlledItem = { null } /> ) ;
382+
383+ const input = findInputNode ( ) ;
384+
385+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
386+
387+ await userEvent . click ( input ) ;
388+ await userEvent . type ( input , 'Item 1' ) ;
389+ await userEvent . keyboard ( '{Enter}' ) ;
390+
391+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 1' ) ;
392+
393+ await userEvent . type ( input , '{backspace}' ) ;
394+ await userEvent . type ( input , '1' ) ;
395+ await userEvent . keyboard ( '{Enter}' ) ;
396+
397+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 1' ) ;
398+ } ) ;
399+
400+ it ( 'should yield highlighted item as `selectedItem` when pressing Enter with a modified input value' , async ( ) => {
401+ render ( < ControlledComboBox controlledItem = { null } /> ) ;
402+ const input = findInputNode ( ) ;
403+
404+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
405+
406+ await userEvent . click ( input ) ;
407+ await userEvent . type ( input , 'Item 2' ) ;
408+ await userEvent . keyboard ( '{Enter}' ) ;
409+
410+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 2' ) ;
411+
412+ await userEvent . type ( input , '{backspace}' ) ;
413+ await userEvent . keyboard ( '{Enter}' ) ;
414+
415+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 0' ) ;
416+ } ) ;
417+
378418 describe ( 'should display initially selected item found in `initialSelectedItem`' , ( ) => {
379419 it ( 'using an object type for the `initialSelectedItem` prop' , async ( ) => {
380420 render (
@@ -506,7 +546,7 @@ describe('ComboBox', () => {
506546 await openMenu ( ) ;
507547 await userEvent . click ( screen . getByRole ( 'option' , { name : 'Item 2' } ) ) ;
508548 expect ( screen . getByText ( 'onChangeCallCount: 1' ) ) . toBeInTheDocument ( ) ;
509- expect ( screen . getByText ( 'value: Item 2' ) ) . toBeInTheDocument ( ) ;
549+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 2' ) ;
510550 expect (
511551 screen . getByRole ( 'combobox' , { value : 'Item 2' } )
512552 ) . toBeInTheDocument ( ) ;
@@ -521,7 +561,7 @@ describe('ComboBox', () => {
521561 screen . getByRole ( 'button' , { name : 'Clear selected item' } )
522562 ) ;
523563 expect ( screen . getByText ( 'onChangeCallCount: 2' ) ) . toBeInTheDocument ( ) ;
524- expect ( screen . getByText ( 'value: none' ) ) . toBeInTheDocument ( ) ;
564+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
525565 expect ( findInputNode ( ) ) . toHaveDisplayValue ( '' ) ;
526566 } ) ;
527567 it ( 'should update and call `onChange` once when selection is cleared from the combobox after an external update is made, and the external state managing selectedItem is updated' , async ( ) => {
@@ -536,7 +576,7 @@ describe('ComboBox', () => {
536576 screen . getByRole ( 'button' , { name : 'Clear selected item' } )
537577 ) ;
538578 expect ( screen . getByText ( 'onChangeCallCount: 2' ) ) . toBeInTheDocument ( ) ;
539- expect ( screen . getByText ( 'value: none' ) ) . toBeInTheDocument ( ) ;
579+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
540580 expect ( findInputNode ( ) ) . toHaveDisplayValue ( '' ) ;
541581 } ) ;
542582 it ( 'should update and call `onChange` when a combination of external and combobox selections are made' , async ( ) => {
@@ -547,17 +587,17 @@ describe('ComboBox', () => {
547587 ) ;
548588 expect ( screen . getByText ( 'onChangeCallCount: 1' ) ) . toBeInTheDocument ( ) ;
549589 expect ( findInputNode ( ) ) . toHaveDisplayValue ( 'Item 3' ) ;
550- expect ( screen . getByText ( 'value: Item 3' ) ) . toBeInTheDocument ( ) ;
590+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 3' ) ;
551591 await openMenu ( ) ;
552592 await userEvent . click ( screen . getByRole ( 'option' , { name : 'Item 2' } ) ) ;
553593 expect ( screen . getByText ( 'onChangeCallCount: 2' ) ) . toBeInTheDocument ( ) ;
554594 expect ( findInputNode ( ) ) . toHaveDisplayValue ( 'Item 2' ) ;
555- expect ( screen . getByText ( 'value: Item 2' ) ) . toBeInTheDocument ( ) ;
595+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'Item 2' ) ;
556596 await userEvent . click (
557597 screen . getByRole ( 'button' , { name : 'Clear selected item' } )
558598 ) ;
559599 expect ( screen . getByText ( 'onChangeCallCount: 3' ) ) . toBeInTheDocument ( ) ;
560- expect ( screen . getByText ( 'value: none' ) ) . toBeInTheDocument ( ) ;
600+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
561601 expect ( findInputNode ( ) ) . toHaveDisplayValue ( '' ) ;
562602 } ) ;
563603 it ( 'should update and call `onChange` once when selection is updated externally' , async ( ) => {
@@ -576,7 +616,7 @@ describe('ComboBox', () => {
576616 await userEvent . click ( screen . getByRole ( 'option' , { name : 'Item 2' } ) ) ;
577617 await userEvent . click ( screen . getByRole ( 'button' , { name : 'reset' } ) ) ;
578618 expect ( screen . getByText ( 'onChangeCallCount: 2' ) ) . toBeInTheDocument ( ) ;
579- expect ( screen . getByText ( 'value: none' ) ) . toBeInTheDocument ( ) ;
619+ expect ( screen . getByTestId ( 'selected-item' ) . textContent ) . toBe ( 'none' ) ;
580620 expect ( findInputNode ( ) ) . toHaveDisplayValue ( '' ) ;
581621 } ) ;
582622 it ( 'should clear selected item when `selectedItem` is updated to `null` externally' , async ( ) => {
0 commit comments