Skip to content

Commit 56b5c52

Browse files
Add new test case to make sure initial passed value is selected upon mount + fix other tests
1 parent 387ad3b commit 56b5c52

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

packages/components/src/custom-select-control-v2/legacy-component/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
3434
return;
3535
}
3636

37-
// @todo add test to verify that a value passed programmatically is
38-
// selected
39-
4037
// Executes the logic in a microtask after the popup is closed.
4138
// This is simply to ensure the isOpen state matches that in Downshift.
4239
await Promise.resolve();
40+
4341
const state = store.getState();
4442

4543
const option = options.find( ( item ) => item.name === nextValue );
@@ -60,7 +58,10 @@ function CustomSelectControl( props: LegacyCustomSelectProps ) {
6058

6159
useEffect( () => {
6260
// This is a workaround for selecting the right item upon mount
63-
store.setValue( value?.name! );
61+
if ( value ) {
62+
store.setValue( value.name );
63+
}
64+
// eslint-disable-next-line react-hooks/exhaustive-deps
6465
} );
6566

6667
const children = options.map(

packages/components/src/custom-select-control-v2/legacy-component/test/index.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ const ControlledCustomSelectControl = ( {
6060
onChange,
6161
...restProps
6262
}: React.ComponentProps< typeof UncontrolledCustomSelectControl > ) => {
63+
const { value: overrideValue } = restProps;
6364
const [ value, setValue ] = useState( options[ 0 ] );
65+
const initialValue = overrideValue ?? value;
6466
return (
6567
<UncontrolledCustomSelectControl
6668
{ ...restProps }
@@ -70,7 +72,7 @@ const ControlledCustomSelectControl = ( {
7072
setValue( args.selectedItem );
7173
} }
7274
value={ options.find(
73-
( option: any ) => option.key === value.key
75+
( option: any ) => option.key === initialValue.key
7476
) }
7577
/>
7678
);
@@ -500,4 +502,22 @@ describe.each( [
500502
} )
501503
);
502504
} );
505+
506+
it( 'Should display the initial value passed as the selected value', async () => {
507+
const initialSelectedItem = legacyProps.options[ 5 ];
508+
509+
const testProps = {
510+
...legacyProps,
511+
value: initialSelectedItem,
512+
};
513+
514+
render( <Component { ...testProps } /> );
515+
516+
const currentSelectedItem = await screen.findByRole( 'combobox', {
517+
expanded: false,
518+
} );
519+
520+
// Verify the initial selected value
521+
expect( currentSelectedItem ).toHaveTextContent( 'aquarela' );
522+
} );
503523
} );

0 commit comments

Comments
 (0)