All files / test-support/properties alias.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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
import { throwBetterError } from '../better-errors';
import {
  getProperty,
  objectHasProperty
} from '../helpers';
import { getExecutionContext } from '../execution_context';
 
const ALIASED_PROP_NOT_FOUND = 'PageObject does not contain aliased property';
 
/**
 * Returns the value of some other property on the PageObject.
 *
 * @example
 *
 * import { create } from 'ember-cli-page-object';
 * import { alias } from 'ember-cli-page-object/macros';
 *
 * const page = create({
 *   submitButton: {
 *     scope: '.submit-button'
 *   },
 *   submit: alias('submitButton.click')
 * });
 *
 * // calls `page.submitButton.click`
 * page.submit();
 *
 * @example
 *
 * import { create } from 'ember-cli-page-object';
 * import { alias } from 'ember-cli-page-object/macros';
 *
 * const page = create({
 *   submitButton: {
 *     scope: '.submit-button'
 *   },
 *   isSubmitButtonVisible: alias('submitButton.isVisible')
 * });
 *
 * // checks value of `page.submitButton.isVisible`
 * assert.ok(page.isSubmitButtonVisible);
 *
 * @example
 *
 * import { create } from 'ember-cli-page-object';
 * import { alias } from 'ember-cli-page-object/macros';
 *
 * const page = create({
 *   form: {
 *     input: {
 *       scope: 'input'
 *     },
 *     submitButton: {
 *       scope: '.submit-button'
 *     }
 *   },
 *   fillFormInput: alias('form.input.fillIn', { chainable: true }),
 *   submitForm: alias('form.submitButton.click', { chainable: true })
 * });
 *
 * // executes `page.form.input.fillIn` then `page.form.submitButton.click`
 * // and causes both methods to return `page` (instead of `page.form.input`
 * // and `page.form.submitButton` respectively) so that the aliased methods
 * // can be chained off `page`.
 * page
 *   .fillFormInput('foo')
 *   .submitForm();
 *
 * @public
 *
 * @param {string} pathToProp - dot-separated path to a property specified on the PageObject
 * @param {Object} options
 * @param {Boolean} options.chainable - when this is true, an aliased
 * method returns the PageObject node on which the alias is defined, rather
 * than the PageObject node on which the aliased property is defined.
 * @return {Descriptor}
 *
 * @throws Will throw an error if the PageObject does not have the specified property.
 */
export function alias(pathToProp, options = {}) {
  return {
    isDescriptor: true,
 
    get(key) {
      if (!objectHasProperty(this, pathToProp)) {
        throwBetterError(this, key, `${ALIASED_PROP_NOT_FOUND} \`${pathToProp}\`.`);
      }
 
      const value = getProperty(this, pathToProp);
 
      if (typeof value !== 'function' || !options.chainable) {
        return value;
      }
 
      return function(...args) {
        // We can't just return value(...args) here because if the alias points
        // to a property on a child node, then the return value would be that
        // child node rather than this node.
        value(...args);
        return getExecutionContext(this).chainable();
      };
    }
  };
}