Skip to content

Commit e1398f1

Browse files
authored
feat: custom data-cy attribute for dropdownmenu (#993)
BREAKING CHANGE: Previously the DropdownMenu may have been targeted for tests using "popover". Now it will need to be targeted with "dropdownmenu".
1 parent 6fd2bd5 commit e1398f1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/dropdownMenu/components/DropdownMenu.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export interface DropdownMenuProps {
9393
* Allows custom styling
9494
*/
9595
className?: string;
96+
/**
97+
* Human-readable selector used for writing tests
98+
*/
99+
"data-cy"?: string;
96100
}
97101

98102
const defaultItemToString = (
@@ -116,7 +120,8 @@ const DropdownMenu = (props: DropdownMenuProps) => {
116120
Direction.TopCenter,
117121
Direction.TopLeft
118122
],
119-
trigger
123+
trigger,
124+
"data-cy": dataCy = "dropdownmenu"
120125
} = props;
121126

122127
const handleSelection = (
@@ -172,6 +177,7 @@ const DropdownMenu = (props: DropdownMenuProps) => {
172177
footer={footer}
173178
key={`dropdown-${sectionIndex}`}
174179
sectionIndex={sectionIndex}
180+
data-cy={`${dataCy}-section-${sectionIndex}`}
175181
>
176182
{childrenWithKeys}
177183
</SectionWrapper>
@@ -204,6 +210,7 @@ const DropdownMenu = (props: DropdownMenuProps) => {
204210
<PopoverBox
205211
maxHeight={menuMaxHeight}
206212
maxWidth={menuMaxWidth}
213+
data-cy={dataCy}
207214
{...getMenuProps(
208215
{ refKey: "menuRef" },
209216
// The menu is not mounted when `isOpen` is false, so Downshift's ref check fails incorrectly
@@ -223,7 +230,11 @@ const DropdownMenu = (props: DropdownMenuProps) => {
223230
})
224231
})
225232
) : (
226-
<button {...getToggleButtonProps()} className={buttonReset}>
233+
<button
234+
{...getToggleButtonProps()}
235+
className={buttonReset}
236+
data-cy={`${dataCy}-button`}
237+
>
227238
{trigger}
228239
</button>
229240
)}

packages/dropdownMenu/tests/__snapshots__/DropdownMenu.test.tsx.snap

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exports[`Dropdown renders a closed dropdown 1`] = `
1515
aria-haspopup="true"
1616
aria-label="open menu"
1717
class="css-1m1ecn3"
18+
data-cy="dropdownmenu-button"
1819
data-toggle="true"
1920
role="button"
2021
type="button"
@@ -42,6 +43,7 @@ exports[`Dropdown renders a dropdown with a max height 1`] = `
4243
aria-haspopup="true"
4344
aria-label="open menu"
4445
class="css-1m1ecn3"
46+
data-cy="dropdownmenu-button"
4547
data-toggle="true"
4648
role="button"
4749
type="button"
@@ -70,6 +72,7 @@ exports[`Dropdown renders an open dropdown 1`] = `
7072
aria-haspopup="true"
7173
aria-label="close menu"
7274
class="css-1m1ecn3"
75+
data-cy="dropdownmenu-button"
7376
data-toggle="true"
7477
role="button"
7578
type="button"
@@ -87,7 +90,7 @@ exports[`Dropdown renders an open dropdown 1`] = `
8790
<div
8891
aria-labelledby="downshift-1-label"
8992
class="css-1slvl4g"
90-
data-cy="popover"
93+
data-cy="dropdownmenu"
9194
id="downshift-1-menu"
9295
role="listbox"
9396
>
@@ -153,6 +156,7 @@ exports[`Dropdown renders an open dropdown with multiple sections 1`] = `
153156
aria-haspopup="true"
154157
aria-label="close menu"
155158
class="css-1m1ecn3"
159+
data-cy="dropdownmenu-button"
156160
data-toggle="true"
157161
role="button"
158162
type="button"
@@ -170,7 +174,7 @@ exports[`Dropdown renders an open dropdown with multiple sections 1`] = `
170174
<div
171175
aria-labelledby="downshift-3-label"
172176
class="css-1slvl4g"
173-
data-cy="popover"
177+
data-cy="dropdownmenu"
174178
id="downshift-3-menu"
175179
role="listbox"
176180
>

0 commit comments

Comments
 (0)