<commit_before>import 'cypress-real-events';

// This is the timeout duration between each event. You can overwrite this in your test if you want it to move faster or slower.
// eslint-disable-next-line prefer-const
let timeoutDuration = 0;

// Utility to remove ID selector (in case it's included)
const leadingHashRegex = /^#/;
const removeLeadingHash = name => name.replace(leadingHashRegex, '');

/**
 * This command is used by other commands to select form values. You can call this yourself if you want to move focus to a specific radio value while the radio field is focused. The value is the value of the input/option.
 */
Cypress.Commands.add('findOption', value => {
  cy.get(':focus').then($el => {
    if ($el.val() !== value) {
      cy.realPress('ArrowDown', { pressDelay: timeoutDuration });
      cy.findOption(value);
    }
  });
});

/**
 * This command is used by other commands to select form values by typing in the desired characters. This is done instead of arrow keys because doing this on Mac is currently not possible. You can call this yourself if you want to move focus to a specific select value while the select field is focused. The text is the text that shows to the user on the select option.
 */
Cypress.Commands.add('findSelectOptionByTyping', text => {
  cy.get(':focus :selected').then($el => {
    if ($el.text() !== text) {
      for (const character of text) {
        cy.realPress(character);
      }

      cy.findSelectOptionByTyping(text);
    }
  });
});

/**
 * @callback chooseSelectOptionUsingValue
 * @param {String} value - The option value to search for
 */
/**
 * This command will target the focused select and then find and select the option value (not text, like `chooseSelectOptionByTyping`) that matches, and then selects it
 * @param {String} name - Name of the new command
 * @param {chooseSelectOptionUsingValue} callbackFunction - The callback that uses the value to find the matching option, and then selects it by typing (because Arrow down does not work on Mac)
 */
Cypress.Commands.add('chooseSelectOptionUsingValue', value => {
  cy.get(':focus')
    .find('option')
    .each($option => {
      if (value === $option[0].value) {
        cy.findSelectOptionByTyping($option.text());
      }
    });
});

/**
 * This command is used to select a specific radio input when one of the radios in a group is focused. The value is the value of the input.
 */
Cypress.Commands.add('chooseRadio', value => {
  if (typeof value === 'undefined') {
    return;
  }
  cy.get(':focus').then($el => {
    cy.get(`input[name='${$el.attr('name')}'][value='${value}']`).should(
      'exist',
    );
    cy.findOption(value);
    cy.realPress('Space');
  });
});

/**
 * This command is used to select a specific select option when a select box is focused. The value is the value of the input
 */
Cypress.Commands.add('chooseSelectOptionByTyping', text => {
  if (typeof text === 'undefined') {
    return;
  }

  cy.findSelectOptionByTyping(text);
});

/**
 * This command types in the focused input or textarea.
 * */
Cypress.Commands.add('typeInFocused', text => {
  cy.get(':focus').type(text, { delay: timeoutDuration });
});

/**
 * @callback moveToElement
 * @param {String} selector - The CSS element selector.
 * @param {Boolean} [forward=true] - A boolean that will tab for or backward towards the element.
 * @param {Boolean} [checkIfExists=true] - A boolean for when this command recursively calls itself, you don't need to call this
 */

/**
 * This command tabs to the element on the page based on the passed in selector.
 * @param {String} name - Name of the new command.
 * @param {moveToElement} callbackFunction - The callback that handles moving to the element.
 */
Cypress.Commands.add(
  'tabToElement',
  (selector, forward = true, checkIfExists = true) => {
    if (checkIfExists) {
      cy.get(selector).should('exist');
    }

    const key = forward ? 'Tab' : ['Shift', 'Tab'];

    cy.realPress(key, { pressDelay: timeoutDuration }).then(() => {
      cy.get(':focus').then($el => {
        if (!$el.is(selector)) {
          cy.tabToElement(selector, forward, false);
        }
      });
    });
  },
);

// Target & use the "Start" form button on the Introduction page
Cypress.Commands.add('tabToStartForm', () => {
  // Same button selector as tabToSubmitForm, or action link
  cy.tabToElement(
    'button[id$="continueButton"].usa-button-primary, .vads-c-action-link--green',
  );
  cy.realPress('Space');
});

// Target & use the "Continue" button on a form page
Cypress.Commands.add('tabToContinueForm', () => {
  cy.tabToElement('button[type="submit"]');
  cy.realPress('Space');
});

// Target & use the "Submit" form button on the review & submit page
Cypress.Commands.add('tabToSubmitForm', () => {
  // Form submit button is a button type?
  cy.tabToElement('button[id$="continueButton"].usa-button-primary');
  cy.realPress('Space');
});

/**
 * @callback tabToInputWithLabel
 * @param {String} - Text within a label associated with an input you want to focus
 */
/**
 * This command allows you to search for an input using the label text. The focus will be moved to that element, if the label text is found. Modified from Cypress docs: https://glebbahmutov.com/cypress-examples/6.5.0/recipes/form-input-by-label.html#reusable-function<commit_msg>Press Enter for button & action links (#21155)<commit_after>import 'cypress-real-events';

// This is the timeout duration between each event. You can overwrite this in your test if you want it to move faster or slower.
// eslint-disable-next-line prefer-const
let timeoutDuration = 0;

// Utility to remove ID selector (in case it's included)
const leadingHashRegex = /^#/;
const removeLeadingHash = name => name.replace(leadingHashRegex, '');

/**
 * This command is used by other commands to select form values. You can call this yourself if you want to move focus to a specific radio value while the radio field is focused. The value is the value of the input/option.
 */
Cypress.Commands.add('findOption', value => {
  cy.get(':focus').then($el => {
    if ($el.val() !== value) {
      cy.realPress('ArrowDown', { pressDelay: timeoutDuration });
      cy.findOption(value);
    }
  });
});

/**
 * This command is used by other commands to select form values by typing in the desired characters. This is done instead of arrow keys because doing this on Mac is currently not possible. You can call this yourself if you want to move focus to a specific select value while the select field is focused. The text is the text that shows to the user on the select option.
 */
Cypress.Commands.add('findSelectOptionByTyping', text => {
  cy.get(':focus :selected').then($el => {
    if ($el.text() !== text) {
      for (const character of text) {
        cy.realPress(character);
      }

      cy.findSelectOptionByTyping(text);
    }
  });
});

/**
 * @callback chooseSelectOptionUsingValue
 * @param {String} value - The option value to search for
 */
/**
 * This command will target the focused select and then find and select the option value (not text, like `chooseSelectOptionByTyping`) that matches, and then selects it
 * @param {String} name - Name of the new command
 * @param {chooseSelectOptionUsingValue} callbackFunction - The callback that uses the value to find the matching option, and then selects it by typing (because Arrow down does not work on Mac)
 */
Cypress.Commands.add('chooseSelectOptionUsingValue', value => {
  cy.get(':focus')
    .find('option')
    .each($option => {
      if (value === $option[0].value) {
        cy.findSelectOptionByTyping($option.text());
      }
    });
});

/**
 * This command is used to select a specific radio input when one of the radios in a group is focused. The value is the value of the input.
 */
Cypress.Commands.add('chooseRadio', value => {
  if (typeof value === 'undefined') {
    return;
  }
  cy.get(':focus').then($el => {
    cy.get(`input[name='${$el.attr('name')}'][value='${value}']`).should(
      'exist',
    );
    cy.findOption(value);
    cy.realPress('Space');
  });
});

/**
 * This command is used to select a specific select option when a select box is focused. The value is the value of the input
 */
Cypress.Commands.add('chooseSelectOptionByTyping', text => {
  if (typeof text === 'undefined') {
    return;
  }

  cy.findSelectOptionByTyping(text);
});

/**
 * This command types in the focused input or textarea.
 * */
Cypress.Commands.add('typeInFocused', text => {
  cy.get(':focus').type(text, { delay: timeoutDuration });
});

/**
 * @callback moveToElement
 * @param {String} selector - The CSS element selector.
 * @param {Boolean} [forward=true] - A boolean that will tab for or backward towards the element.
 * @param {Boolean} [checkIfExists=true] - A boolean for when this command recursively calls itself, you don't need to call this
 */

/**
 * This command tabs to the element on the page based on the passed in selector.
 * @param {String} name - Name of the new command.
 * @param {moveToElement} callbackFunction - The callback that handles moving to the element.
 */
Cypress.Commands.add(
  'tabToElement',
  (selector, forward = true, checkIfExists = true) => {
    if (checkIfExists) {
      cy.get(selector).should('exist');
    }

    const key = forward ? 'Tab' : ['Shift', 'Tab'];

    cy.realPress(key, { pressDelay: timeoutDuration }).then(() => {
      cy.get(':focus').then($el => {
        if (!$el.is(selector)) {
          cy.tabToElement(selector, forward, false);
        }
      });
    });
  },
);

// Target & use the "Start" form button on the Introduction page
Cypress.Commands.add('tabToStartForm', () => {
  // Same button selector as tabToSubmitForm, or action link
  cy.tabToElement(
    'button[id$="continueButton"].usa-button-primary, .vads-c-action-link--green',
  );
  cy.realPress('Enter');
});

// Target & use the "Continue" button on a form page
Cypress.Commands.add('tabToContinueForm', () => {
  cy.tabToElement('button[type="submit"]');
  cy.realPress('Space');
});

// Target & use the "Submit" form button on the review & submit page
Cypress.Commands.add('tabToSubmitForm', () => {
  // Form submit button is a button type?
  cy.tabToElement('button[id$="continueButton"].usa-button-primary');
  cy.realPress('Space');
});

/**
 * @callback tabToInputWithLabel
 * @param {String} - Text within a label associated with an input you want to focus
 */
/**
 * This command allows you to search for an input using the label text. The focus will be moved to that element, if the label text is found. Modified from Cypress docs: https://glebbahmutov.com/cypress-examples/6.5.0/recipes/form-input-by-label.html#reusable-function
{'fraction_non_alphanumeric': 0.13470340962167213, 'fraction_numerical': 0.0014012143858010276, 'mean_word_length': 3.6386481802426345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6649041', 'n_tokens_mistral': 3141, 'n_tokens_neox': 3111, 'n_words': 1109}