Skip to content

Commit

Permalink
feat(react): added tabbing and other interactions into cypress tests …
Browse files Browse the repository at this point in the history
…suite

Previously cypress did not support tabbing(keyboard), adding cypress-real-events allows us to test
the components in a more comprehensive manner in which our users would interact with them
  • Loading branch information
gd2910 committed Mar 22, 2024
1 parent 986bd60 commit 06d4a51
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/react/cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference types="cypress" />

import "./component";
import "cypress-real-events";

import Commands from "./commands";

Expand Down
10 changes: 10 additions & 0 deletions packages/react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"cypress-axe": "^1.5.0",
"cypress-file-upload": "^5.0.8",
"cypress-image-diff-js": "^2.1.3",
"cypress-real-events": "^1.12.0",
"eslint-plugin-cypress": "^2.15.1",
"mkdirp": "^1.0.4",
"np": "^7.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,32 @@ describe("IcAccordionGroup", () => {
<TwoAccordionsWithOneExpanded />
</IcAccordionGroup>
);
cy.checkHydrated(IC_ACCORDION_GROUP);

cy.get(getAccordionSelector(1))
.invoke("prop", "expanded")
.should("eq", false);
cy.get(getAccordionSelector(2))
.invoke("prop", "expanded")
.should("eq", true);

cy.get(getAccordionSelector(1)).click();
cy.findShadowEl(getAccordionSelector(1), "button")
.focus()
.realPress("Space");
cy.get(getAccordionSelector(1))
.invoke("prop", "expanded")
.should("eq", true);
cy.get(getAccordionSelector(2))
.invoke("prop", "expanded")
.should("eq", false);

cy.realPress("Tab").realPress("Space");
cy.get(getAccordionSelector(1))
.invoke("prop", "expanded")
.should("eq", false);
cy.get(getAccordionSelector(2))
.invoke("prop", "expanded")
.should("eq", true);
});

it("both accordions should open when single expansion is false", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
StaticSideNav,
checkSideNavSize,
} from "./IcSideNavigationTestData";
import _ from "cypress/types/lodash";

const DEFAULT_HEIGHT = 1171;
const DEFAULT_TEST_THRESHOLD = 0.06;
Expand Down Expand Up @@ -285,6 +286,18 @@ describe("IcSideNavigation", () => {
cy.checkSideNavSize(false);
});

it("expands when expand button is tabbed to and pressed", () => {
mount(<BasicSideNav />);
cy.checkHydrated(SIDE_NAV_LABEL);
cy.findShadowEl("ic-side-navigation", ".title-link").focus();
Cypress._.times(4, () => cy.realPress("Tab"));
cy.realPress("Enter");
cy.checkSideNavSize(true);

cy.clickOnShadowEl(SIDE_NAV_LABEL, EXPAND_BUTTON_SELECTOR);
cy.checkSideNavSize(false);
});

it("renders collapsed and expanded when expanded state is externally controlled", () => {
mount(<DynamicExpandedSideNav />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ describe("IcToggleButtonGroup", () => {
getToggle(1).click({ force: true });
cy.get(WIN_CONSOLE_SPY).should(NOT_HAVE_BEEN_CALLED);
});
it("should tab through toggles and check focus and selection state", () => {
mount(<ToggleGroupSingle />);
cy.checkHydrated(IC_TOGGLE_BUTTON_GROUP);

getToggle(2).focus().realPress(["Shift", "Tab"]);
cy.get(IC_TOGGLE_BUTTON_GROUP).should(HAVE_FOCUS);
});
});
describe("screenshots", () => {
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"noImplicitAny": true,
"jsx": "react",
"allowJs": true,
"types": ["cypress", "node", "cypress-axe", "cypress-file-upload"]
"types": ["cypress", "node", "cypress-axe", "cypress-file-upload", "cypress-real-events"]
},
"include": ["src/**/*.ts", "src/**/*.tsx"],
"exclude": ["**/__tests__/**", "cypress/**", "src/component-tests/**/*"],
Expand Down

0 comments on commit 06d4a51

Please sign in to comment.