UNPKG

orphic-cypress

Version:

Set of utilities and typescript transformers to cover storybook stories with cypress component tests

63 lines 2.25 kB
"use strict"; /** * @module cypress */ Object.defineProperty(exports, "__esModule", { value: true }); exports.dataCy = void 0; /** * Select an html element by its data-cy attribute. * * ```ts * cy.dataCy('something').click() // -> cy.get("[data-cy='something']").click() * ``` * * Accepts children which will then become further selectors * * ```ts * cy.dataCy('something', '.nested').click() * // -> cy.get("[data-cy='something'] .nested").click() * ``` * * And is chainable such that it will use the previous element as its scope * * ```ts * cy.dataCy('something').dataCy("other").click() * // -> cy.get("[data-cy='something']").within(() => cy.get("[data-cy='other']").click()) * cy.get('.top').dataCy('something') // same idea as above * ``` * * Finally, it supports taking the first signature literally and passing in * a subject directly. That's very likely an unnecessary step and convention/obvious errors * would prevent it, but going for complete here * * ```ts * cy.get(".first-selector").then(($first) => * cy.dataCy($first, "second-selector").should("contain", 0) * ); * ``` * * Note: this is not added to cypress commands by default. Pass to {@link addCommands} * or a custom function this should be added as a cypress command. */ const dataCy = (...args) => { // handle the unlikely scenario someone takes the first signature literally const unlikelyArgs = args; if (!unlikelyArgs[0] && typeof unlikelyArgs[1] !== "string") { // type coerce here due to build intentionally not including the added types return cy.wrap(unlikelyArgs[1]).dataCy(unlikelyArgs[2], unlikelyArgs[3], unlikelyArgs[4]); } const [subject, selector, children, options] = args; return subject ? cy.wrap(subject).within(() => // again, type coerce here due to build intentionally not including the added types cy.dataCy(selector, children, options)) : cy.get(`[data-cy="${selector}"]${children ? ` ${children}` : ""}`, options); }; exports.dataCy = dataCy; // unnecessary IIFE, just avoiding typedoc thinking this is a namespace (() => { exports.dataCy.commandOptions = { prevSubject: "optional", }; })(); //# sourceMappingURL=data-cy.js.map