Skip to content

Commit

Permalink
feat: custom data-cy attribute for dropdownmenu (#993)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Previously the DropdownMenu may have been targeted for tests using "popover".
Now it will need to be targeted with "dropdownmenu".
  • Loading branch information
nataliepina authored Aug 29, 2023
1 parent 6fd2bd5 commit e1398f1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 13 additions & 2 deletions packages/dropdownMenu/components/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ export interface DropdownMenuProps {
* Allows custom styling
*/
className?: string;
/**
* Human-readable selector used for writing tests
*/
"data-cy"?: string;
}

const defaultItemToString = (
Expand All @@ -116,7 +120,8 @@ const DropdownMenu = (props: DropdownMenuProps) => {
Direction.TopCenter,
Direction.TopLeft
],
trigger
trigger,
"data-cy": dataCy = "dropdownmenu"
} = props;

const handleSelection = (
Expand Down Expand Up @@ -172,6 +177,7 @@ const DropdownMenu = (props: DropdownMenuProps) => {
footer={footer}
key={`dropdown-${sectionIndex}`}
sectionIndex={sectionIndex}
data-cy={`${dataCy}-section-${sectionIndex}`}
>
{childrenWithKeys}
</SectionWrapper>
Expand Down Expand Up @@ -204,6 +210,7 @@ const DropdownMenu = (props: DropdownMenuProps) => {
<PopoverBox
maxHeight={menuMaxHeight}
maxWidth={menuMaxWidth}
data-cy={dataCy}
{...getMenuProps(
{ refKey: "menuRef" },
// The menu is not mounted when `isOpen` is false, so Downshift's ref check fails incorrectly
Expand All @@ -223,7 +230,11 @@ const DropdownMenu = (props: DropdownMenuProps) => {
})
})
) : (
<button {...getToggleButtonProps()} className={buttonReset}>
<button
{...getToggleButtonProps()}
className={buttonReset}
data-cy={`${dataCy}-button`}
>
{trigger}
</button>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports[`Dropdown renders a closed dropdown 1`] = `
aria-haspopup="true"
aria-label="open menu"
class="css-1m1ecn3"
data-cy="dropdownmenu-button"
data-toggle="true"
role="button"
type="button"
Expand Down Expand Up @@ -42,6 +43,7 @@ exports[`Dropdown renders a dropdown with a max height 1`] = `
aria-haspopup="true"
aria-label="open menu"
class="css-1m1ecn3"
data-cy="dropdownmenu-button"
data-toggle="true"
role="button"
type="button"
Expand Down Expand Up @@ -70,6 +72,7 @@ exports[`Dropdown renders an open dropdown 1`] = `
aria-haspopup="true"
aria-label="close menu"
class="css-1m1ecn3"
data-cy="dropdownmenu-button"
data-toggle="true"
role="button"
type="button"
Expand All @@ -87,7 +90,7 @@ exports[`Dropdown renders an open dropdown 1`] = `
<div
aria-labelledby="downshift-1-label"
class="css-1slvl4g"
data-cy="popover"
data-cy="dropdownmenu"
id="downshift-1-menu"
role="listbox"
>
Expand Down Expand Up @@ -153,6 +156,7 @@ exports[`Dropdown renders an open dropdown with multiple sections 1`] = `
aria-haspopup="true"
aria-label="close menu"
class="css-1m1ecn3"
data-cy="dropdownmenu-button"
data-toggle="true"
role="button"
type="button"
Expand All @@ -170,7 +174,7 @@ exports[`Dropdown renders an open dropdown with multiple sections 1`] = `
<div
aria-labelledby="downshift-3-label"
class="css-1slvl4g"
data-cy="popover"
data-cy="dropdownmenu"
id="downshift-3-menu"
role="listbox"
>
Expand Down

0 comments on commit e1398f1

Please sign in to comment.