Skip to content

Commit ab08df5

Browse files
committed
test: add support for test-utils, add data-test-indices to multiselect+select selectable-item
1 parent a49de49 commit ab08df5

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

src/select/parts/item.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import { getBaseProps } from '../../internal/base-component';
1010
import CheckboxIcon from '../../internal/components/checkbox-icon';
1111
import Option from '../../internal/components/option';
1212
import { DropdownOption, OptionDefinition } from '../../internal/components/option/interfaces';
13+
import { getTestOptionIndexes } from '../../internal/components/options-list/utils/test-indexes';
1314
import { HighlightType } from '../../internal/components/options-list/utils/use-highlight-option.js';
1415
import SelectableItem from '../../internal/components/selectable-item';
1516
import Tooltip from '../../internal/components/tooltip';
1617
import useHiddenDescription from '../../internal/hooks/use-hidden-description';
1718
import { SelectProps } from '../interfaces';
1819

20+
import optionStyles from '../../internal/components/option/styles.css.js';
1921
import styles from './styles.css.js';
2022

2123
export interface ItemProps {
@@ -74,8 +76,12 @@ const Item = (
7476
const [canShowTooltip, setCanShowTooltip] = useState(true);
7577
useEffect(() => setCanShowTooltip(true), [highlighted]);
7678

79+
const { throughIndex, inGroupIndex, groupIndex } = getTestOptionIndexes(option) || {};
7780
return (
7881
<SelectableItem
82+
data-test-index={throughIndex}
83+
data-in-group-index={inGroupIndex}
84+
data-group-index={groupIndex}
7985
disableContentStyling={!!renderOption}
8086
ariaSelected={Boolean(selected)}
8187
selected={selected}
@@ -105,13 +111,22 @@ const Item = (
105111
</div>
106112
)}
107113
{renderOption ? (
108-
renderOption({
109-
option: option.option,
110-
selected: !!selected,
111-
highlighted: !!highlighted,
112-
disabled: !!disabled,
113-
type: option.type === 'parent' ? 'parent' : 'child',
114-
})
114+
<div
115+
data-value={wrappedOption.value}
116+
className={clsx(optionStyles.option, {
117+
disabled: !!disabled,
118+
selected: !!selected,
119+
highlighted: !!highlighted,
120+
})}
121+
>
122+
{renderOption({
123+
option: option.option,
124+
selected: !!selected,
125+
highlighted: !!highlighted,
126+
disabled: !!disabled,
127+
type: option.type === 'parent' ? 'parent' : 'child',
128+
})}
129+
</div>
115130
) : (
116131
<Option
117132
option={{ ...wrappedOption, disabled }}

src/select/parts/multiselect-item.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
import React, { ReactNode, useEffect, useRef, useState } from 'react';
4+
import clsx from 'clsx';
45

56
import { useMergeRefs } from '@cloudscape-design/component-toolkit/internal';
67

78
import { getBaseProps } from '../../internal/base-component';
89
import CheckboxIcon from '../../internal/components/checkbox-icon';
910
import Option from '../../internal/components/option';
1011
import { OptionDefinition } from '../../internal/components/option/interfaces';
12+
import { getTestOptionIndexes } from '../../internal/components/options-list/utils/test-indexes';
1113
import SelectableItem from '../../internal/components/selectable-item';
1214
import Tooltip from '../../internal/components/tooltip';
1315
import useHiddenDescription from '../../internal/hooks/use-hidden-description';
1416
import { MultiselectProps } from '../../multiselect/interfaces';
1517
import { ItemProps } from './item';
1618

19+
import optionStyles from '../../internal/components/option/styles.css.js';
1720
import styles from './styles.css.js';
1821
interface MultiselectItemProps extends ItemProps {
1922
indeterminate?: boolean;
@@ -62,8 +65,13 @@ const MultiSelectItem = (
6265

6366
const [canShowTooltip, setCanShowTooltip] = useState(true);
6467
useEffect(() => setCanShowTooltip(true), [highlighted]);
68+
const { throughIndex, inGroupIndex, groupIndex } = getTestOptionIndexes(option) || {};
69+
6570
return (
6671
<SelectableItem
72+
data-test-index={throughIndex}
73+
data-in-group-index={inGroupIndex}
74+
data-group-index={groupIndex}
6775
disableContentStyling={!!renderOption}
6876
ariaChecked={isParent && indeterminate ? 'mixed' : Boolean(selected)}
6977
selected={selected}
@@ -100,13 +108,22 @@ const MultiSelectItem = (
100108
</div>
101109
)}
102110
{renderOption ? (
103-
renderOption({
104-
option: option.option,
105-
selected: !!selected,
106-
highlighted: !!highlighted,
107-
disabled: !!disabled,
108-
type: option.type !== 'use-entered' ? (option.type ?? 'child') : 'child',
109-
})
111+
<div
112+
data-value={wrappedOption.value}
113+
className={clsx(optionStyles.option, {
114+
disabled: !!disabled,
115+
selected: !!selected,
116+
highlighted: !!highlighted,
117+
})}
118+
>
119+
{renderOption({
120+
option: option.option,
121+
selected: !!selected,
122+
highlighted: !!highlighted,
123+
disabled: !!disabled,
124+
type: option.type !== 'use-entered' ? (option.type ?? 'child') : 'child',
125+
})}
126+
</div>
110127
) : (
111128
<Option
112129
option={{ ...wrappedOption, disabled }}

0 commit comments

Comments
 (0)