> ## Documentation Index
> Fetch the complete documentation index at: https://trydent.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# testCreator

> /client/utils/testCreator.ts

## Overview

This section of the documentation provides an overview of the utility functions used in the extension. These functions are mainly responsible for generating the Cypress test code based on the user interactions recorded by the extension.

## Event Listeners

### switchCase

The `switchCase` function takes an event object and generates a piece of Cypress test code for the action performed in the event. It supports the following actions: click, change (input), and navigate (click on a link).

### describeCreator

The `describeCreator` function takes a describe object and generates a complete Cypress test suite. The describe object contains the test URL, description, write-up, and an array of it statement objects.

### itCreator

The `itCreator` function takes an array of it statement objects and a URL, and generates a string of it statements. Each it statement object contains an it statement and an array of event objects.

### actionCreator

The `actionCreator` function takes an it statement object and a URL, and generates a string of actions for the it statement. The it statement object contains an it statement and an array of event objects.

<CodeGroup>
  ```javascript switchCase theme={null}
  function switchCase(event: EventObj): string {
  let { selector, action, input, URL, a, href} = event;
  if (a == true){
    action = 'navigate'
  }
  switch (action) {
    case 'click':
      return `cy.xpath('${selector}').should('exist');
      cy.xpath('${selector}').click({force:true});`;
      break;
    case 'change':
      return `cy.xpath('${selector}').type('${input}');`;
      break;
    case 'navigate':
      return `cy.xpath('${selector}').click();
      cy.location('pathname').should('eq','${href}');`;
      break;
    case 'assertion':
      let finalText = '';
      finalText += `cy.xpath('${selector}').should('exist');`
      // if contains a inner text or outerText
      if (href){
        finalText += `cy.xpath('${selector}').contains("a").should("have.attr", "href", "${href}");`
      }
      if (input.innerHTML !== '' && input.innerHTML){
        finalText += `cy.xpath('${selector}').should('have.html',${JSON.stringify(input.innerHTML)}).and('be.visible');`
      }
      if (input.id !== '' && input.id){
        finalText += `cy.xpath('${selector}').should('have.id', '${input.id}');`
      }
      if (input.className !== '' && input.className){
        finalText += `cy.xpath('${selector}').should('have.attr', '${input.className}');`
      }
      return finalText;
    default:
      return 'didnt input a valid action';
  }
  }
  ```

  ```javascript describeCreator theme={null}
  /**
  * Mother function, creates a textblock for an entire describe suite in Cypress.
  *
  * @param {object} describeObj - Full object that comes from user
  * @returns {string} Full cypress test suite to be sent to user
  */
  export function describeCreator(obj: Describe): string {
  // destructuring the 'describe' object
  const { URL, description, writeUp, itStatements } = obj;
  let resultStr;
  //create line for 'describe' statement in which we call the itCreator?
  // ###TO-DO: push 'it' statement down to itCreator
  return (resultStr = `
    //${writeUp}
    describe('${description}', () => {
      beforeEach(() => {
        cy.visit('${URL}')
      })
        
      ${itCreator(itStatements, URL)}
    })`);
  }
  ```

  ```javascript itCreator theme={null}
  /**
   * Separate itStatements function to make a 'describe' with multiple 'it's
   *
   * @param {array} itStatementsArr - Array containing 'it' statement objects.
   * @param {string} URL - URL of the page to be tested.
   * @returns {string} - Concatenated string of 'it' statements.
   */
  // ###TO-DO: Fully convert to TypeScript
  function itCreator(itStatementsArr: itObject[], URL: string): string {
    // Initialize empty array to push formatted 'it' statements into
    const formattedItStatments = [];
    // Initialize empty string to push formatted 'it' statements into
    let itText = '';

    itStatementsArr.forEach((itState) => {
      // Concatenate evaluated result of each itState into 'itText
      itText += `
      it(${actionCreator(itState, URL)})`;
    });

    return itText;
  }
  ```

  ```javascript actionCreator theme={null}
  /**
   * Separate action function to make an 'it' statement with multiple actions
   *
   * @param {itObject} eObj - Event Object containing 'it' statement and array of events.
   * @param {string} URL - URL of the page to be tested.
   * @returns {string} - Concatenated string of actions within 'it' statement.
   */
  function actionCreator(eObj: itObject, URL: string): string {
    const { itStatement, eventArr } = eObj;
    let textBlock = '';

    // Parse through eventArr to look at each event individually
    eventArr.forEach((event) => {
      // switchCase imported from switchCase.ts
      if (textBlock !== '')
        textBlock += `
          `;
      textBlock += switchCase(event);
    });
    // Return a block of text that includes each event text, statment, visit location
    let resultText = `'${itStatement}', () => {
          ${textBlock}
        }`;
    return resultText;
  }

  ```
</CodeGroup>
