All files / addon-test-support/properties visitable.js

100% Statements 20/20
100% Branches 7/7
100% Functions 5/5
100% Lines 20/20

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            48x 124x   124x 21x 21x   21x 3x       18x   18x     103x         45x 6x     45x                                                                                                                       33x       48x   48x 48x 48x   45x   45x          
import { assign } from '../-private/helpers';
import { getExecutionContext } from '../-private/execution_context';
 
import $ from '-jquery';
 
function fillInDynamicSegments(path, params) {
  return path.split('/').map(function(segment) {
    let match = segment.match(/^:(.+)$/);
 
    if (match) {
      let [, key] = match;
      let value = params[key];
 
      if (typeof (value) === 'undefined') {
        throw new Error(`Missing parameter for '${key}'`);
      }
 
      // Remove dynamic segment key from params
      delete params[key];
 
      return encodeURIComponent(value);
    }
 
    return segment;
  }).join('/');
}
 
function appendQueryParams(path, queryParams) {
  if (Object.keys(queryParams).length) {
    path += `?${$.param(queryParams)}`;
  }
 
  return path;
}
 
/**
 * @public
 *
 * Loads a given route.
 *
 * The resulting descriptor can be called with dynamic segments and parameters.
 *
 * @example
 *
 * import { create, visitable } from 'ember-cli-page-object';
 *
 * const page = create({
 *   visit: visitable('/users')
 * });
 *
 * // visits '/users'
 * page.visit();
 *
 * @example
 *
 * import { create, visitable } from 'ember-cli-page-object';
 *
 * const page = create({
 *   visit: visitable('/users/:user_id')
 * });
 *
 * // visits '/users/10'
 * page.visit({ user_id: 10 });
 *
 * @example
 *
 * import { create, visitable } from 'ember-cli-page-object';
 *
 * const page = create({
 *   visit: visitable('/users')
 * });
 *
 * // visits '/users?name=john'
 * page.visit({ name: 'john' });
 *
 * @example
 *
 * import { create, visitable } from 'ember-cli-page-object';
 *
 * const page = create({
 *   visit: visitable('/users/:user_id')
 * });
 *
 * // visits '/users/1?name=john'
 * page.visit({ user_id: 1, name: 'john' });
 *
 * @param {string} path - Full path of the route to visit
 * @return {Descriptor}
 *
 * @throws Will throw an error if dynamic segments are not filled
 */
export function visitable(path) {
  return {
    isDescriptor: true,
 
    value(dynamicSegmentsAndQueryParams = {}) {
      let executionContext = getExecutionContext(this);
 
      return executionContext.runAsync((context) => {
        let params = assign({}, dynamicSegmentsAndQueryParams);
        let fullPath = fillInDynamicSegments(path, params);
 
        fullPath = appendQueryParams(fullPath, params);
 
        return context.visit(fullPath);
      });
    }
  };
}