From a4143cb8031e019d360d167cf45dd1fbb038b95b Mon Sep 17 00:00:00 2001 From: Mike Perrotti Date: Wed, 17 Jul 2019 18:16:12 -0400 Subject: [PATCH] feat: adds data attributes to simplify selectors for Cypress testing --- README.md | 15 +++ packages/appChrome/components/AppChrome.tsx | 12 +- packages/appChrome/components/SidebarItem.tsx | 6 + .../appChrome/components/SidebarSection.tsx | 2 +- .../appChrome/components/SidebarSubMenu.tsx | 6 +- .../components/SidebarSubMenuItem.tsx | 6 +- .../__snapshots__/AppChrome.test.tsx.snap | 5 +- .../tests/__snapshots__/Sidebar.test.tsx.snap | 13 ++- packages/avatar/components/Avatar.tsx | 1 + .../tests/__snapshots__/avatar.test.tsx.snap | 2 + packages/badge/components/badge.tsx | 6 +- packages/badge/components/badgeButton.tsx | 2 +- .../tests/__snapshots__/badge.test.tsx.snap | 7 ++ .../__snapshots__/badgeButton.test.tsx.snap | 7 ++ packages/breadcrumb/components/Breadcrumb.tsx | 1 + .../__snapshots__/Breadcrumb.test.tsx.snap | 4 + packages/button/components/DangerButton.tsx | 6 +- .../components/DangerDropdownButton.tsx | 6 +- packages/button/components/PrimaryButton.tsx | 6 +- .../components/PrimaryDropdownButton.tsx | 6 +- .../button/components/SecondaryButton.tsx | 6 +- .../components/SecondaryDropdownButton.tsx | 6 +- packages/button/components/StandardButton.tsx | 6 +- .../components/StandardDropdownButton.tsx | 6 +- packages/button/components/SuccessButton.tsx | 6 +- .../components/SuccessDropdownButton.tsx | 6 +- packages/card/components/Card.tsx | 2 +- .../tests/__snapshots__/Card.test.tsx.snap | 3 + .../components/CheckboxInput.tsx | 18 +++ .../__snapshots__/CheckboxInput.test.tsx.snap | 31 +++++- packages/clickable/components/clickable.tsx | 10 +- .../components/ConfigurationMap.tsx | 7 +- .../components/ConfigurationMapHeading.tsx | 1 + .../components/ConfigurationMapLabel.tsx | 9 +- .../components/ConfigurationMapRow.tsx | 42 +++---- .../components/ConfigurationMapRowAction.tsx | 5 +- .../components/ConfigurationMapSection.tsx | 9 +- .../components/ConfigurationMapValue.tsx | 9 +- packages/configurationmap/style.ts | 19 ++++ .../tests/ConfigurationMap.test.tsx | 20 +--- .../ConfigurationMap.test.tsx.snap | 75 +++++++++++-- .../tests/__snapshots__/HashMap.test.tsx.snap | 39 ++++++- packages/donutChart/components/DonutChart.tsx | 9 +- .../__snapshots__/DonutChart.test.tsx.snap | 5 + .../dropdowMenu/components/DropdownMenu.tsx | 1 + .../components/DropdownMenuItem.tsx | 1 + .../__snapshots__/DropdownMenu.test.tsx.snap | 1 + .../DropdownMenuItem.test.tsx.snap | 5 + .../__snapshots__/Dropdownable.test.tsx.snap | 4 + packages/icon/components/Icon.tsx | 1 + .../tests/__snapshots__/Icon.test.tsx.snap | 2 + packages/infobox/components/InfoBox.tsx | 14 ++- .../tests/__snapshots__/InfoBox.test.tsx.snap | 5 + packages/list/components/BorderedList.tsx | 5 +- packages/list/components/List.tsx | 2 +- .../__snapshots__/BorderedList.test.tsx.snap | 1 + .../tests/__snapshots__/List.test.tsx.snap | 2 + packages/modal/components/DialogModal.tsx | 13 ++- packages/modal/components/FullscreenModal.tsx | 3 + packages/modal/components/ModalBase.tsx | 19 +++- packages/modal/components/ModalContents.tsx | 9 +- .../tests/__snapshots__/Modal.test.tsx.snap | 60 ++++++++++ packages/pageheader/components/PageHeader.tsx | 6 +- .../__snapshots__/PageHeader.test.tsx.snap | 6 + .../selectInput/components/SelectInput.tsx | 24 +++- .../__snapshots__/SelectInput.test.tsx.snap | 6 +- .../layout/container/components/Container.tsx | 4 +- .../__snapshots__/Container.test.tsx.snap | 1 + .../layout/flexbox/components/Flex.tsx | 1 + .../layout/flexbox/components/FlexItem.tsx | 1 + .../tests/__snapshots__/Flex.test.tsx.snap | 3 + .../__snapshots__/FlexItem.test.tsx.snap | 3 + .../layout/gridList/components/GridList.tsx | 1 + .../__snapshots__/GridList.test.tsx.snap | 3 + .../modifiers/components/BorderedBox.tsx | 1 + .../styleUtils/modifiers/components/Box.tsx | 12 +- .../modifiers/components/SpacingBox.tsx | 6 +- .../__snapshots__/BorderedBox.test.tsx.snap | 2 + .../tests/__snapshots__/Box.test.tsx.snap | 2 + .../__snapshots__/SpacingBox.test.tsx.snap | 3 + .../typography/components/CaptionText.tsx | 7 +- .../typography/components/DangerText.tsx | 2 +- .../typography/components/HeadingText1.tsx | 2 +- .../typography/components/HeadingText2.tsx | 8 +- .../typography/components/HeadingText3.tsx | 2 +- .../typography/components/InteractiveText.tsx | 1 + .../typography/components/MonospaceText.tsx | 1 + .../typography/components/SmallText.tsx | 7 +- .../typography/components/SuccessText.tsx | 2 +- .../styleUtils/typography/components/Text.tsx | 20 +++- .../typography/components/WarningText.tsx | 2 +- .../typographyComponents.test.tsx.snap | 25 +++++ packages/table/components/CheckboxTable.tsx | 8 +- packages/table/components/Table.tsx | 19 +++- .../__snapshots__/CheckboxTable.test.tsx.snap | 104 ++++++++++++++++-- .../tests/__snapshots__/Table.test.tsx.snap | 43 ++++++++ packages/tabs/components/Tabs.tsx | 6 +- .../tests/__snapshots__/Tabs.test.tsx.snap | 1 + packages/textInput/components/TextInput.tsx | 22 +++- .../__snapshots__/TextInput.test.tsx.snap | 75 ++++++++++--- .../TextInputWithIcon.test.tsx.snap | 45 ++++++-- packages/textarea/components/Textarea.tsx | 28 ++++- packages/toaster/components/Toast.tsx | 11 +- packages/toaster/components/Toaster.tsx | 5 +- .../tests/__snapshots__/Toast.test.tsx.snap | 3 + .../toggleInput/components/ToggleInput.tsx | 7 +- .../ToggleInputList.test.tsx.snap | 74 +++++++++++-- packages/tooltip/components/Tooltip.tsx | 1 + .../tooltip/components/TooltipContent.tsx | 1 + .../tests/__snapshots__/Tooltip.test.tsx.snap | 15 +++ packages/typeahead/components/Typeahead.tsx | 4 +- .../__snapshots__/Typeahead.test.tsx.snap | 71 ++++++++++-- 112 files changed, 1129 insertions(+), 194 deletions(-) diff --git a/README.md b/README.md index bbe9fc094..c349c48b9 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,21 @@ describe('Badge', () => { }); ``` +## Testing with Cypress +To make it easier to select DOM nodes of our components, DOM nodes have a `data-cy` attribute. + +### `data-cy` value naming convention +**Parent nodes:** The value of `data-cy` for component's parent node is the same as the component name, but camelCased. For example: The parent node for `` will have `data-cy="primaryButton"` + +**Child nodes:** If a child node has a `data-cy` added, there will be a dash between the parent node's name and a string to describe the child node. For example: The footer element of a `` will have `data-cy="fullscreenModal-footer"` + +**States and variants:** If a node has a special "state", `data-cy` will prepend a string describing that state after a dot. +For example: +* `` will have `data-cy="textInput textInput.disabled"` +* A `` with an error will have `data-cy="textInput textInput.error"` + +For more information on writing selectors, see the [Cypress guide](https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements). + ## Commits You should follow [conventional commit](https://conventionalcommits.org/) formatting rules, as they provide a framework to write explicit messages that are easy to comprehend when looking through the project history and enable automatic change log generation. diff --git a/packages/appChrome/components/AppChrome.tsx b/packages/appChrome/components/AppChrome.tsx index 5ed0feb17..cff0ddab7 100644 --- a/packages/appChrome/components/AppChrome.tsx +++ b/packages/appChrome/components/AppChrome.tsx @@ -33,11 +33,17 @@ class AppChrome extends React.PureComponent { textSize("m"), flex({ direction: "column" }) )} + data-cy="appChrome" > -
{headerBar}
+
{headerBar}
-
{sidebar}
-
+
+ {sidebar} +
+
{mainContent}
diff --git a/packages/appChrome/components/SidebarItem.tsx b/packages/appChrome/components/SidebarItem.tsx index c36dd0d31..c0cefeaf8 100644 --- a/packages/appChrome/components/SidebarItem.tsx +++ b/packages/appChrome/components/SidebarItem.tsx @@ -14,6 +14,11 @@ export interface SidebarItemProps { class SidebarItemComponent extends React.PureComponent { public render() { const { children, isActive, onClick } = this.props; + const dataCy = [ + "sidebarItem", + ...(isActive ? ["sidebarItem.active"] : []) + ].join(" "); + const Item = styled("li")` ${props => { return css` @@ -40,6 +45,7 @@ class SidebarItemComponent extends React.PureComponent { tabIndex={0} role="link" disableFocusOutline={true} + dataCy={dataCy} > {children} diff --git a/packages/appChrome/components/SidebarSection.tsx b/packages/appChrome/components/SidebarSection.tsx index 5aca55aa8..b5a17c873 100644 --- a/packages/appChrome/components/SidebarSection.tsx +++ b/packages/appChrome/components/SidebarSection.tsx @@ -51,7 +51,7 @@ class SidebarSection extends React.PureComponent { `; return ( -
+
{label && (

+
  • +
    {children}
    diff --git a/packages/appChrome/tests/__snapshots__/AppChrome.test.tsx.snap b/packages/appChrome/tests/__snapshots__/AppChrome.test.tsx.snap index 435bc0f75..451968f94 100644 --- a/packages/appChrome/tests/__snapshots__/AppChrome.test.tsx.snap +++ b/packages/appChrome/tests/__snapshots__/AppChrome.test.tsx.snap @@ -84,9 +84,10 @@ exports[`AppChrome renders with the app chrome regions 1`] = ` >
    HeaderBar content goes here @@ -97,6 +98,7 @@ exports[`AppChrome renders with the app chrome regions 1`] = ` >
    Sidebar content @@ -104,6 +106,7 @@ exports[`AppChrome renders with the app chrome regions 1`] = `
    Main app content goes here diff --git a/packages/appChrome/tests/__snapshots__/Sidebar.test.tsx.snap b/packages/appChrome/tests/__snapshots__/Sidebar.test.tsx.snap index b4fbf833a..41a96c986 100644 --- a/packages/appChrome/tests/__snapshots__/Sidebar.test.tsx.snap +++ b/packages/appChrome/tests/__snapshots__/Sidebar.test.tsx.snap @@ -3,6 +3,7 @@ exports[`Sidebar SidebarItem renders 1`] = ` +
    @@ -131,7 +134,13 @@ exports[`Sidebar SidebarSubMenu renders 1`] = ` padding-left: 0; } -
  • +
  • diff --git a/packages/avatar/components/Avatar.tsx b/packages/avatar/components/Avatar.tsx index fa328a56b..c6d775adb 100644 --- a/packages/avatar/components/Avatar.tsx +++ b/packages/avatar/components/Avatar.tsx @@ -39,6 +39,7 @@ const Avatar: React.StatelessComponent = (props: AvatarProps) => { className={cx(avatarContainer, avatarSize(size))} role="img" aria-label={label} + data-cy="avatar" > {/* tslint:disable react-a11y-img-has-alt diff --git a/packages/avatar/tests/__snapshots__/avatar.test.tsx.snap b/packages/avatar/tests/__snapshots__/avatar.test.tsx.snap index eeb398800..8e34448c4 100644 --- a/packages/avatar/tests/__snapshots__/avatar.test.tsx.snap +++ b/packages/avatar/tests/__snapshots__/avatar.test.tsx.snap @@ -19,6 +19,7 @@ exports[`Avatar renders default 1`] = `
    { public render() { const { appearance, children } = this.props; - return {children}; + return ( + + {children} + + ); } } diff --git a/packages/badge/components/badgeButton.tsx b/packages/badge/components/badgeButton.tsx index 422e476ab..c86c252a3 100644 --- a/packages/badge/components/badgeButton.tsx +++ b/packages/badge/components/badgeButton.tsx @@ -40,7 +40,7 @@ export class BadgeButton extends React.PureComponent { ${badgeButton(appearance)}; `; return ( - + {children} ); diff --git a/packages/badge/tests/__snapshots__/badge.test.tsx.snap b/packages/badge/tests/__snapshots__/badge.test.tsx.snap index 0fea3cd45..a2a805475 100644 --- a/packages/badge/tests/__snapshots__/badge.test.tsx.snap +++ b/packages/badge/tests/__snapshots__/badge.test.tsx.snap @@ -3,6 +3,7 @@ exports[`Badge accept jsx as children 1`] = ` string @@ -13,6 +14,7 @@ exports[`Badge accept jsx as children 1`] = ` exports[`Badge danger 1`] = ` danger @@ -21,6 +23,7 @@ exports[`Badge danger 1`] = ` exports[`Badge default 1`] = ` default @@ -29,6 +32,7 @@ exports[`Badge default 1`] = ` exports[`Badge outline 1`] = ` outline @@ -37,6 +41,7 @@ exports[`Badge outline 1`] = ` exports[`Badge primary 1`] = ` primary @@ -45,6 +50,7 @@ exports[`Badge primary 1`] = ` exports[`Badge success 1`] = ` success @@ -53,6 +59,7 @@ exports[`Badge success 1`] = ` exports[`Badge warning 1`] = ` warning diff --git a/packages/badge/tests/__snapshots__/badgeButton.test.tsx.snap b/packages/badge/tests/__snapshots__/badgeButton.test.tsx.snap index b5e62862b..1389d7b8e 100644 --- a/packages/badge/tests/__snapshots__/badgeButton.test.tsx.snap +++ b/packages/badge/tests/__snapshots__/badgeButton.test.tsx.snap @@ -3,6 +3,7 @@ exports[`BadgeButton accept jsx as children 1`] = ` { className={cx(textWeight("medium"), textSize("l"), { [padding("left", "xs")]: i !== 0 })} + data-cy="breadcrumb" > {crumb}
    diff --git a/packages/breadcrumb/tests/__snapshots__/Breadcrumb.test.tsx.snap b/packages/breadcrumb/tests/__snapshots__/Breadcrumb.test.tsx.snap index a4e8d7d58..2b54c6039 100644 --- a/packages/breadcrumb/tests/__snapshots__/Breadcrumb.test.tsx.snap +++ b/packages/breadcrumb/tests/__snapshots__/Breadcrumb.test.tsx.snap @@ -6,6 +6,7 @@ exports[`Breadcrumb default 1`] = ` >
    One @@ -13,10 +14,12 @@ exports[`Breadcrumb default 1`] = `
    Two diff --git a/packages/button/components/DangerButton.tsx b/packages/button/components/DangerButton.tsx index 6882b5bff..6d0a85e2e 100644 --- a/packages/button/components/DangerButton.tsx +++ b/packages/button/components/DangerButton.tsx @@ -6,7 +6,11 @@ import { } from "./ButtonBase"; const DangerButton = (props: ButtonProps) => ( - + ); export default DangerButton; diff --git a/packages/button/components/DangerDropdownButton.tsx b/packages/button/components/DangerDropdownButton.tsx index 8742f09ee..c732fa937 100644 --- a/packages/button/components/DangerDropdownButton.tsx +++ b/packages/button/components/DangerDropdownButton.tsx @@ -4,7 +4,11 @@ import { ButtonProps } from "./ButtonBase"; import { SystemIcons } from "../../icons/dist/system-icons-enum"; const DangerDropdownButton = (props: ButtonProps) => ( - + ); export default DangerDropdownButton; diff --git a/packages/button/components/PrimaryButton.tsx b/packages/button/components/PrimaryButton.tsx index 4a9e5fa47..cce2ca1cb 100644 --- a/packages/button/components/PrimaryButton.tsx +++ b/packages/button/components/PrimaryButton.tsx @@ -6,7 +6,11 @@ import { } from "./ButtonBase"; const PrimaryButton = (props: ButtonProps) => ( - + ); export default PrimaryButton; diff --git a/packages/button/components/PrimaryDropdownButton.tsx b/packages/button/components/PrimaryDropdownButton.tsx index 3783f82ac..117e97943 100644 --- a/packages/button/components/PrimaryDropdownButton.tsx +++ b/packages/button/components/PrimaryDropdownButton.tsx @@ -4,7 +4,11 @@ import { ButtonProps } from "./ButtonBase"; import { SystemIcons } from "../../icons/dist/system-icons-enum"; const PrimaryDropdownButton = (props: ButtonProps) => ( - + ); export default PrimaryDropdownButton; diff --git a/packages/button/components/SecondaryButton.tsx b/packages/button/components/SecondaryButton.tsx index 68e7792bd..ce4eb36a5 100644 --- a/packages/button/components/SecondaryButton.tsx +++ b/packages/button/components/SecondaryButton.tsx @@ -6,7 +6,11 @@ import { } from "./ButtonBase"; const SecondaryButton = (props: ButtonProps) => ( - + ); export default SecondaryButton; diff --git a/packages/button/components/SecondaryDropdownButton.tsx b/packages/button/components/SecondaryDropdownButton.tsx index 3f78a5028..54d63c0cc 100644 --- a/packages/button/components/SecondaryDropdownButton.tsx +++ b/packages/button/components/SecondaryDropdownButton.tsx @@ -4,7 +4,11 @@ import { ButtonProps } from "./ButtonBase"; import { SystemIcons } from "../../icons/dist/system-icons-enum"; const SecondaryDropdownButton = (props: ButtonProps) => ( - + ); export default SecondaryDropdownButton; diff --git a/packages/button/components/StandardButton.tsx b/packages/button/components/StandardButton.tsx index 993abc8e1..e3186a602 100644 --- a/packages/button/components/StandardButton.tsx +++ b/packages/button/components/StandardButton.tsx @@ -6,7 +6,11 @@ import { } from "./ButtonBase"; const StandardButton = (props: ButtonProps) => ( - + ); export default StandardButton; diff --git a/packages/button/components/StandardDropdownButton.tsx b/packages/button/components/StandardDropdownButton.tsx index bb683162c..386bf932e 100644 --- a/packages/button/components/StandardDropdownButton.tsx +++ b/packages/button/components/StandardDropdownButton.tsx @@ -4,7 +4,11 @@ import { ButtonProps } from "./ButtonBase"; import { SystemIcons } from "../../icons/dist/system-icons-enum"; const StandardDropdownButton = (props: ButtonProps) => ( - + ); export default StandardDropdownButton; diff --git a/packages/button/components/SuccessButton.tsx b/packages/button/components/SuccessButton.tsx index f54d9396c..ac3b4adcc 100644 --- a/packages/button/components/SuccessButton.tsx +++ b/packages/button/components/SuccessButton.tsx @@ -6,7 +6,11 @@ import { } from "./ButtonBase"; const SuccessButton = (props: ButtonProps) => ( - + ); export default SuccessButton; diff --git a/packages/button/components/SuccessDropdownButton.tsx b/packages/button/components/SuccessDropdownButton.tsx index e5bfa5a01..77f7e3d85 100644 --- a/packages/button/components/SuccessDropdownButton.tsx +++ b/packages/button/components/SuccessDropdownButton.tsx @@ -4,7 +4,11 @@ import { ButtonProps } from "./ButtonBase"; import { SystemIcons } from "../../icons/dist/system-icons-enum"; const SuccessDropdownButton = (props: ButtonProps) => ( - + ); export default SuccessDropdownButton; diff --git a/packages/card/components/Card.tsx b/packages/card/components/Card.tsx index 201c960e5..402625d93 100644 --- a/packages/card/components/Card.tsx +++ b/packages/card/components/Card.tsx @@ -28,7 +28,7 @@ class Card extends React.PureComponent { : null; return ( -
    +
    {children}
    ); diff --git a/packages/card/tests/__snapshots__/Card.test.tsx.snap b/packages/card/tests/__snapshots__/Card.test.tsx.snap index 873a2c197..d2e3fc51e 100644 --- a/packages/card/tests/__snapshots__/Card.test.tsx.snap +++ b/packages/card/tests/__snapshots__/Card.test.tsx.snap @@ -3,6 +3,7 @@ exports[`Card default 1`] = `
    {/* tslint:enable */} diff --git a/packages/checkboxInput/tests/__snapshots__/CheckboxInput.test.tsx.snap b/packages/checkboxInput/tests/__snapshots__/CheckboxInput.test.tsx.snap index eb48148cd..003fdff33 100644 --- a/packages/checkboxInput/tests/__snapshots__/CheckboxInput.test.tsx.snap +++ b/packages/checkboxInput/tests/__snapshots__/CheckboxInput.test.tsx.snap @@ -78,12 +78,15 @@ exports[`CheckboxInput renders all appearances 1`] = ` > -
    +