All files / test-support/execution_context rfc268.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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195                                                                                                                                                                                                                                                                                                                                                                                                     
import $ from '-jquery';
import { resolve } from 'rsvp';
import {
  guardMultiple,
  buildSelector,
  findClosestValue,
  getRoot
} from '../helpers';
import {
  getContext,
  visit,
  click,
  fillIn,
  triggerEvent,
  focus,
  blur
} from '../compatibility';
import {
  ELEMENT_NOT_FOUND,
  throwBetterError
} from '../better-errors';
import Ceibo from 'ceibo';
 
export default function ExecutionContext(pageObjectNode) {
  this.pageObjectNode = pageObjectNode;
}
 
ExecutionContext.prototype = {
  run(cb) {
    return cb(this);
  },
 
  runAsync(cb) {
    let root = getRoot(this.pageObjectNode);
    let isChained = !root._chainedTree;
 
    if (isChained) {
      // Already chained, so our root is the root of the chained tree, and we
      // need to wait on its promise if it has one so the previous call can
      // resolve before we run ours.
      root._promise = resolve(root._promise).then(() => cb(this));
    } else {
      // Not a chained call, so store our method's return on the chained root
      // so that chained calls can find it to wait on it.
      root._chainedTree._promise = cb(this);
    }
 
    return this.chainable();
  },
 
  chainable() {
    // See explanation in `create.js` -- here instead of returning the node on
    // which our method was invoked, we find and return our node's mirror in the
    // chained tree so calls to it can be recognized as chained calls, and
    // trigger the chained-call waiting behavior.
    let root = getRoot(this.pageObjectNode);
    let isChained = !root._chainedTree;
 
    if (isChained) {
      // Already chained, so our node is in the chained tree
      return this.pageObjectNode;
    } else {
      // Not already chained, so we need to look up our equivalent node in the
      // chained tree and return that. We do it by walking up the tree
      // collecting node keys to build a path to our node, and then use that
      // to walk back down the chained tree to our mirror node.
      let path = [];
      let node;
 
      for (node = this.pageObjectNode; node; node = Ceibo.parent(node)) {
        path.unshift(Ceibo.meta(node).key);
      }
      // The path will end with the root's key, 'root', so shift that back off
      path.shift();
 
      node = root._chainedTree;
      path.forEach((key) => {
        // Normally an item's key is just its property name, but collection
        // items' keys also include their index. Collection item keys look like
        // `foo[2]` and legacy collection item keys look like `foo(2)`.
        let match;
        if ((match = /\[(\d+)\]$/.exec(key))) {
          // This is a collection item
          let [ indexStr, index ] = match;
          let name = key.slice(0, -indexStr.length);
          node = node[name].objectAt(parseInt(index, 10));
        } else if ((match = /\((\d+)\)$/.exec(key))) {
          // This is a legacy collection item
          let [ indexStr, index ] = match;
          let name = key.slice(0, -indexStr.length);
          node = node[name](parseInt(index, 10));
        } else {
          node = node[key];
        }
      });
      return node;
    }
  },
 
  visit(path) {
    return visit(path);
  },
 
  click(selector, container, options) {
    return this.invokeHelper(selector, options, click);
  },
 
  fillIn(selector, container, options, content) {
    return this.invokeHelper(selector, options, fillIn, content);
  },
 
  triggerEvent(selector, container, options, eventName, eventOptions) {
    // `keyCode` is a deprecated property.
    // @see: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
    // Due to this deprecation `ember-native-dom-helpers` doesn't accept `keyCode` as a `KeyboardEvent` option.
    if (typeof eventOptions.key === 'undefined' && typeof eventOptions.keyCode !== 'undefined') {
      eventOptions.key = eventOptions.keyCode.toString();
      delete eventOptions.keyCode;
    }
 
    return this.invokeHelper(selector, options, triggerEvent, eventName, eventOptions);
  },
 
  focus(selector, options) {
    selector = buildSelector(this.pageObjectNode, selector, options);
    return this.invokeHelper(selector, options, focus);
  },
 
  blur(selector, options) {
    selector = buildSelector(this.pageObjectNode, selector, options);
    return this.invokeHelper(selector, options, blur);
  },
 
  assertElementExists(selector, options) {
    let result = this.getElements(selector, options);
 
    if (result.length === 0) {
      throwBetterError(
        this.pageObjectNode,
        options.pageObjectKey,
        ELEMENT_NOT_FOUND,
        { selector }
      );
    }
  },
 
  find(selector, options) {
    selector = buildSelector(this.pageObjectNode, selector, options);
    let result = this.getElements(selector, options);
 
    guardMultiple(result, selector, options.multiple);
 
    return result;
  },
 
  findWithAssert(selector, options) {
    selector = buildSelector(this.pageObjectNode, selector, options);
    let result = this.getElements(selector, options);
 
    guardMultiple(result, selector, options.multiple);
 
    if (result.length === 0) {
      throwBetterError(
        this.pageObjectNode,
        options.pageObjectKey,
        ELEMENT_NOT_FOUND,
        { selector }
      );
    }
 
    return result;
  },
 
  getElements(selector, options) {
    let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');
    if (container) {
      return $(selector, container);
    } else {
      return $(selector, getContext().element);
    }
  },
 
  invokeHelper(selector, options, helper, ...args) {
    let element = this.getElements(selector, options)[0];
    return helper(element, ...args).catch((e) => {
      throwBetterError(
        this.pageObjectNode,
        options.pageObjectKey,
        e.message || e.toString(),
        { selector }
      );
    });
  }
};