All files / test-support/execution_context helpers.js

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
import {
  throwBetterError
} from '../better-errors';
 
import $ from '-jquery';
 
/**
 * @private
 *
 * Fills inputs, textareas, or contenteditable elements with the passed-in content.
 *
 * @param {jQuery} $selection              jQuery object containing collection of DOM elements to fill in
 * @param {string} content                 Content to be inserted into the target element(s)
 * @param {Object} options                 Options for error reporting
 * @param {string} options.selector        jQuery selector used to target element(s) to fill in
 * @param {Ceibo} options.pageObjectNode   PageObject node containing the method which, when invoked, resulted in this call to `fillElement`
 * @param {string} options.pageObjectKey   Key of method on PageObject which, when invoked, resulted in this call to `fillElement`
 * @return
 *
 * @throws Will throw an error if called on a contenteditable element that has `contenteditable="false"`
 */
export function fillElement(selection, content, { selector, pageObjectNode, pageObjectKey }) {
  const $selection = $(selection);
 
  if ($selection.is('[contenteditable][contenteditable!="false"]')) {
    $selection.html(content);
  } else if ($selection.is('[contenteditable="false"]')) {
    throwBetterError(
      pageObjectNode,
      pageObjectKey,
      'Element cannot be filled because it has `contenteditable="false"`.', {
        selector
      }
    );
  } else {
    $selection.val(content);
  }
}
 
/**
 * @private
 *
 * Given an element, asserts that element is focusable/blurable
 *
 * @param {Element} element - the element to check
 */
export function assertFocusable(element, { selector, pageObjectNode, pageObjectKey }) {
  let $element = $(element);
 
  let error;
 
  if ($element.is(':hidden')) {
    error = 'hidden';
  } else if ($element.is(':disabled')) {
    error = 'disabled';
  } else if ($element.is('[contenteditable="false"]')) {
    error = 'contenteditable="false"';
  } else if (!$element.is(':input, a[href], area[href], iframe, [contenteditable], [tabindex]')) {
    error = 'not a link, input, form element, contenteditable, iframe, or an element with tabindex';
  }
 
  if (error) {
    throwBetterError(
      pageObjectNode,
      pageObjectKey,
      `Element is not focusable because it is ${error}`, {
        selector
      }
    );
  }
}