Skip to content

Commit

Permalink
chore: convert Cypress files to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
haideralsh committed Sep 25, 2023
1 parent 5602b99 commit f80b33a
Show file tree
Hide file tree
Showing 26 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe("Alert", () => {
before(() => {
beforeEach(() => {
cy.renderFromStorybook("alert--with-a-close-button");
});

Expand All @@ -10,7 +10,7 @@ describe("Alert", () => {

it("hides the alert when closed", () => {
cy.get('[aria-label="Close"]').click();
cy.get('[role="alert"]').should("not.be.visible");
cy.get('[role="alert"]').should("not.exist");
cy.get('[role="alert"]').should("not.exist");
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("Radio", () => {
radio().should("be.checked");
});
});

describe("Checked", () => {
beforeEach(() => {
cy.renderFromStorybook("radio--set-to-default-checked");
Expand All @@ -19,6 +20,7 @@ describe("Radio", () => {
radio().should("be.checked");
});
});

describe("Disabled", () => {
beforeEach(() => {
cy.renderFromStorybook("radio--set-to-disabled");
Expand All @@ -32,6 +34,7 @@ describe("Radio", () => {
radio().eq(1).should("be.disabled");
});
});

describe("Controlled", () => {
beforeEach(() => {
cy.renderFromStorybook("radio--controlled");
Expand All @@ -47,6 +50,7 @@ describe("Radio", () => {
radio().eq(1).should("not.be.checked");
});
});

describe("Set Focus", () => {
beforeEach(() => {
cy.renderFromStorybook("radio--using-ref-to-control-focus");
Expand Down Expand Up @@ -77,6 +81,7 @@ describe("Radio Group", () => {
radio().eq(1).should("not.be.checked");
});
});

describe("All Props", () => {
beforeEach(() => {
cy.renderFromStorybook("radiogroup--radio-group-with-all-props");
Expand All @@ -87,6 +92,7 @@ describe("Radio Group", () => {
radio().eq(2).should("not.be.checked");
});
});

describe("Disabled", () => {
beforeEach(() => {
cy.renderFromStorybook("radiogroup--set-to-disabled");
Expand All @@ -102,15 +108,18 @@ describe("Radio Group", () => {
radio().eq(2).should("be.disabled");
});
});

describe("Controlled", () => {
beforeEach(() => {
cy.renderFromStorybook("radiogroup--controlled");
});

it("has the correct initial values", () => {
radio().eq(0).should("be.checked");
radio().eq(1).should("not.be.checked");
radio().eq(2).should("not.be.checked");
});

it("values are not updated on click", () => {
radio().eq(0).check();
radio().eq(1).check();
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,35 @@ describe("Sidebar", () => {
beforeEach(() => {
cy.renderFromStorybook("sidebar--sidebar");
});

it("is hidden", () => {
Sidebar().should("not.be.visible");
});

it("slides in when triggered", () => {
trigger().click();
Sidebar().should("be.visible");
});

it("slides out", () => {
trigger().click();
overlay().should("be.visible");
overlay().click({ force: true });
Sidebar().should("not.be.visible");
});
});

describe("Accessibility", () => {
beforeEach(() => {
cy.renderFromStorybook("sidebar--sidebar");
});

it("focuses the close button when opened", () => {
trigger().click();
cy.focused().type("{enter}");
Sidebar().should("not.be.visible");
});

it("focuses the trigger when closed", () => {
trigger().click();
cy.focused().type("{enter}");
Expand All @@ -39,27 +45,33 @@ describe("Sidebar", () => {
Sidebar().should("be.visible");
});
});

describe("Open by default", () => {
beforeEach(() => {
cy.renderFromStorybook("sidebar--open-by-default");
});

it("is shown", () => {
Sidebar().should("be.visible");
});

it("slides out when overlay clicked", () => {
overlay().click({ force: true });
Sidebar().should("not.be.visible");
});

it("slides out when close button clicked", () => {
closeButton().click({ force: true });
Sidebar().should("not.be.visible");
});

it("slides in", () => {
overlay().click({ force: true });
trigger().click();
Sidebar().should("be.visible");
});
});

describe("Custom offset", () => {
beforeEach(() => {
cy.renderFromStorybook("sidebar--with-custom-offset");
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe("Tabs", () => {
});

it("renders both scroll indicators when tabs are hidden on the left and right", () => {
cy.get(".tab-container").scrollTo(50);
cy.get(".tab-container").scrollTo(50, 0);

cy.get("svg.nds-icon--leftArrow").should("not.exist");
cy.get("svg.nds-icon--rightArrow").should("exist");
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ describe("Tooltip", () => {
beforeEach(() => {
cy.renderFromStorybook("tooltip--tooltip");
});

it("hides content without interaction", () => {
cy.get('[role="tooltip"]').should("not.be.visible");
cy.get('[role="tooltip"]').should("not.exist");
});

it("shows content on hover", () => {
cy.get('[aria-haspopup="true"]').trigger("mouseover");
cy.get('[role="tooltip"]').should("be.visible");
cy.isInViewport('[role="tooltip"]');
});
});

describe("can be open by default", () => {
beforeEach(() => {
cy.renderFromStorybook("tooltip--open-by-default");
});

it("shows content initially", () => {
cy.get('[aria-haspopup="true"]').trigger("mouseover");
cy.get('[role="tooltip"]').should("be.visible");
cy.isInViewport('[role="tooltip"]');
});

it("hides content on hover", () => {
cy.get('[aria-haspopup="true"]').trigger("mouseover");
cy.get('[aria-haspopup="true"]').trigger("mouseout");
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion cypress/support/commands.js → cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Cypress.Commands.add("clickOutsideElement", () => {

Cypress.Commands.add("isInViewport", (element) => {
cy.get(element).then(($el) => {
// @ts-ignore
const bottom = Cypress.$(cy.state("window")).height();
const rect = $el[0].getBoundingClientRect();

Expand All @@ -58,7 +59,7 @@ Cypress.Commands.add("isNotInViewport", (element) => {
});
});

Cypress.Commands.add("paste", ({ destinationSelector, pastePayload, pasteType }) => {
Cypress.Commands.add("paste", ({ destinationSelector, pastePayload }) => {
// https://developer.mozilla.org/en-US/docs/Web/API/Element/paste_event
cy.get(destinationSelector).then((destination) => {
const pasteEvent = Object.assign(new Event("paste", { bubbles: true, cancelable: true }), {
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions cypress/support/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference types="cypress" />

declare namespace Cypress {
interface Chainable {
isNotInViewport(element: string): Chainable<Element>;
isInViewport(element: string): Chainable<Element>;
renderFromStorybook(element: string): Chainable<Element>;
pressEscapeKey(): Chainable<Element>;
clickOutsideElement(): Chainable<Element>;
tab(): Chainable<Element>;
paste({
destinationSelector,
pastePayload,
}: {
destinationSelector: string;
pastePayload: string;
}): Chainable<Element>;
}
}
8 changes: 8 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node", "./support"]
},
"include": ["**/*.ts"]
}
3 changes: 3 additions & 0 deletions src/DropdownMenu/DropdownMenu.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const customColors = {

export default {
title: "Components/DropdownMenu",
chromatic: {
diffThreshold: 0.4,
},
};

export const _DropdownMenu = () => (
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noImplicitAny": false,
"types": ["cypress", "node"],
"jsx": "react"
},
"include": ["src/**/*"],
Expand Down

0 comments on commit f80b33a

Please sign in to comment.