remix-demo

react router (remix) demo

git clone https://9o.is/git/remix-demo.git

commands.ts

(2723B)


      1 import { faker } from "@faker-js/faker";
      2 
      3 declare global {
      4   namespace Cypress {
      5     interface Chainable {
      6       /**
      7        * Logs in with a random user. Yields the user and adds an alias to the user
      8        *
      9        * @returns {typeof login}
     10        * @memberof Chainable
     11        * @example
     12        *    cy.login()
     13        * @example
     14        *    cy.login({ email: 'whatever@example.com' })
     15        */
     16       login: typeof login;
     17 
     18       /**
     19        * Deletes the current @user
     20        *
     21        * @returns {typeof cleanupUser}
     22        * @memberof Chainable
     23        * @example
     24        *    cy.cleanupUser()
     25        * @example
     26        *    cy.cleanupUser({ email: 'whatever@example.com' })
     27        */
     28       cleanupUser: typeof cleanupUser;
     29 
     30       /**
     31        * Extends the standard visit command to wait for the page to load
     32        *
     33        * @returns {typeof visitAndCheck}
     34        * @memberof Chainable
     35        * @example
     36        *    cy.visitAndCheck('/')
     37        *  @example
     38        *    cy.visitAndCheck('/', 500)
     39        */
     40       visitAndCheck: typeof visitAndCheck;
     41     }
     42   }
     43 }
     44 
     45 function login({
     46   email = faker.internet.email(undefined, undefined, "example.com"),
     47   name = faker.name.firstName(),
     48 }: {
     49   email?: string;
     50   name?: string;
     51 } = {}) {
     52   cy.then(() => ({ email })).as("user");
     53   cy.exec(
     54     `npx ts-node --require tsconfig-paths/register ./cypress/support/create-user.ts "${name}" "${email}"`
     55   ).then(({ stdout }) => {
     56     const cookieValue = stdout
     57       .replace(/.*<cookie>(?<cookieValue>.*)<\/cookie>.*/s, "$<cookieValue>")
     58       .trim();
     59     cy.setCookie("__session", cookieValue);
     60   });
     61   return cy.get("@user");
     62 }
     63 
     64 function cleanupUser({ email }: { email?: string } = {}) {
     65   if (email) {
     66     deleteUserByEmail(email);
     67   } else {
     68     cy.get("@user").then((user) => {
     69       const email = (user as { email?: string }).email;
     70       if (email) {
     71         deleteUserByEmail(email);
     72       }
     73     });
     74   }
     75   cy.clearCookie("__session");
     76 }
     77 
     78 function deleteUserByEmail(email: string) {
     79   cy.exec(
     80     `npx ts-node --require tsconfig-paths/register ./cypress/support/delete-user.ts "${email}"`
     81   );
     82   cy.clearCookie("__session");
     83 }
     84 
     85 // We're waiting a second because of this issue happen randomly
     86 // https://github.com/cypress-io/cypress/issues/7306
     87 // Also added custom types to avoid getting detached
     88 // https://github.com/cypress-io/cypress/issues/7306#issuecomment-1152752612
     89 // ===========================================================
     90 function visitAndCheck(url: string, waitTime: number = 1000) {
     91   cy.visit(url);
     92   cy.location("pathname").should("contain", url).wait(waitTime);
     93 }
     94 
     95 Cypress.Commands.add("login", login);
     96 Cypress.Commands.add("cleanupUser", cleanupUser);
     97 Cypress.Commands.add("visitAndCheck", visitAndCheck);