/ cypress / support / commands.ts
commands.ts
  1  import 'cypress-wait-until';
  2  // import { Provider } from '@ethersproject/providers';
  3  
  4  import { CustomizedBridge } from './tools/bridge';
  5  
  6  declare global {
  7    // eslint-disable-next-line @typescript-eslint/no-namespace
  8    namespace Cypress {
  9      interface Chainable {
 10        /**
 11         * This will set amount in Modal
 12         * @param amount number
 13         * @param max boolean optional
 14         * @example cy.setAmount('137')
 15         */
 16        setAmount(amount: number, max?: boolean): void;
 17        /**
 18         * This will make confirmation in Modal
 19         * @param hasApproval boolean
 20         * @param actionName string optional, verification button text
 21         * @param assetName string optional, verification asset name
 22         * @example cy.doConfirm(true)
 23         */
 24        doConfirm(hasApproval: boolean, actionName?: string, assetName?: string): void;
 25        /**
 26         * This will return borrowed asset row from Dashboard
 27         * @param assetName string
 28         * @param apyType string
 29         * @example cy.getDashBoardBorrowRow('ETH',constants.borrowAPYType.default)
 30         */
 31        getDashBoardBorrowedRow(
 32          assetName: string,
 33          apyType: string
 34        ): Cypress.Chainable<JQuery<HTMLElement>>;
 35        /**
 36         * This will return supplied asset row from Dashboard
 37         * @param assetName string
 38         * @param isCollateralType boolean optional
 39         * @example cy.getDashBoardSuppliedRow('ETH')
 40         */
 41        getDashBoardSuppliedRow(
 42          assetName: string,
 43          isCollateralType?: boolean
 44        ): Cypress.Chainable<JQuery<HTMLElement>>;
 45        /**
 46         * This will switch dashboard view to Borrow
 47         */
 48        doSwitchToDashboardBorrowView(): void;
 49        /**
 50         * This will switch dashboard view to Supply
 51         */
 52        doSwitchToDashboardSupplyView(): void;
 53        refresh(): void;
 54        /**
 55         * This will switch market
 56         * @param marketName string
 57         * @param isV3 boolean
 58         * @example cy.doSwitchMarket('mainnet', true)
 59         */
 60        doSwitchMarket(marketName: string, isV3: boolean): void;
 61      }
 62    }
 63  }
 64  
 65  Cypress.Commands.add('setAmount', (amount: number, max?: boolean) => {
 66    cy.get('[data-cy=Modal]').find('button:contains("Enter an amount")').should('be.disabled');
 67    if (max) {
 68      cy.wait(2000); //there is no way to know when real max amount will upload by UI
 69      cy.get('[data-cy=Modal]').find('button:contains("Max")').click();
 70    } else {
 71      cy.get('[data-cy=Modal] input').first().type(amount.toString());
 72    }
 73  });
 74  
 75  Cypress.Commands.add(
 76    'doConfirm',
 77    (hasApproval: boolean, actionName?: string, assetName?: string) => {
 78      cy.log(`${hasApproval ? 'One step process' : 'Two step process'}`);
 79      if (!hasApproval) {
 80        cy.get(`[data-cy=approvalButton]`, { timeout: 20000 })
 81          .last()
 82          .should('not.be.disabled')
 83          .click({ force: true });
 84      }
 85      cy.get('[data-cy=actionButton]', { timeout: 30000 })
 86        .last()
 87        .should('not.be.disabled')
 88        .then(($btn) => {
 89          if (assetName && actionName) {
 90            expect($btn.first()).to.contain(`${actionName} ${assetName}`);
 91          }
 92          if (assetName && !actionName) {
 93            expect($btn.first()).to.contain(`${actionName}`);
 94          }
 95        })
 96        .click({ force: true });
 97      cy.get("[data-cy=Modal] h2:contains('All done')").should('be.visible');
 98    }
 99  );
100  
101  Cypress.Commands.add('getDashBoardBorrowedRow', (assetName: string) => {
102    return cy.get(`[data-cy='dashboardBorrowedListItem_${assetName.toUpperCase()}']`).first();
103  });
104  
105  Cypress.Commands.add('getDashBoardSuppliedRow', (assetName: string, isCollateralType?: boolean) => {
106    if (isCollateralType) {
107      return cy
108        .get(`[data-cy='dashboardSuppliedListItem_${assetName.toUpperCase()}_Collateral']`)
109        .first();
110    } else {
111      return cy.get(`[data-cy='dashboardSuppliedListItem_${assetName.toUpperCase()}_NoCollateral']`);
112    }
113  });
114  
115  const switchDashboardView = (value: string, dashboardTitle: string) => {
116    cy.get('[role=group]')
117      .contains(value)
118      .then(($btn) => {
119        if (!$btn.is('disabled')) {
120          $btn.click();
121        }
122      });
123    cy.get(`*:contains("Your ${dashboardTitle.toLowerCase()}")`).should('be.visible');
124  };
125  
126  Cypress.Commands.add('doSwitchToDashboardBorrowView', () => {
127    switchDashboardView('Borrow', 'borrows');
128  });
129  
130  Cypress.Commands.add('doSwitchToDashboardSupplyView', () => {
131    switchDashboardView('Supply', 'supplies');
132  });
133  
134  Cypress.Commands.add('refresh', () => {
135    cy.wait(1000); // it's need for some cases where we reload page before full uplaoding page
136    cy.visit(window.url, {
137      onBeforeLoad(win) {
138        // eslint-disable-next-line
139        (win as any).ethereum = new CustomizedBridge(window.signer, window.provider);
140        win.localStorage.setItem('forkEnabled', 'true');
141        win.localStorage.setItem('forkNetworkId', '3030');
142        win.localStorage.setItem('forkBaseChainId', window.chainId);
143        win.localStorage.setItem('forkRPCUrl', window.rpc);
144        win.localStorage.setItem('walletProvider', 'injected');
145        win.localStorage.setItem('selectedAccount', window.address);
146        win.localStorage.setItem('selectedMarket', window.market);
147        win.localStorage.setItem('testnetsEnabled', window.testnetsEnabled);
148      },
149    });
150    cy.wait(1000); //give a time to upload recent data from blockchain
151  });
152  
153  Cypress.Commands.add('doSwitchMarket', (marketName: string, isV3: boolean) => {
154    cy.get('[data-cy="marketSelector"]').click();
155    cy.get(`[data-cy="markets_switch_button_${isV3 ? 'v3' : 'v2'}"]`).then(($btn) => {
156      if (!$btn.attr('aria-passed')) {
157        $btn.click();
158      }
159    });
160    cy.get(`[data-value="fork_proto_${isV3 ? marketName + '_v3' : marketName}"]`).click();
161  });
162  
163  export {};