github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/ui-v2/tests/lib/page-object/visitable.js (about)

     1  // import { assign } from '../-private/helpers';
     2  const assign = Object.assign;
     3  import { getExecutionContext } from 'ember-cli-page-object/-private/execution_context';
     4  
     5  import $ from '-jquery';
     6  
     7  function fillInDynamicSegments(path, params, encoder) {
     8    return path
     9      .split('/')
    10      .map(function(segment) {
    11        let match = segment.match(/^:(.+)$/);
    12  
    13        if (match) {
    14          let [, key] = match;
    15          let value = params[key];
    16  
    17          if (typeof value === 'undefined') {
    18            throw new Error(`Missing parameter for '${key}'`);
    19          }
    20  
    21          // Remove dynamic segment key from params
    22          delete params[key];
    23          return encoder(value);
    24        }
    25  
    26        return segment;
    27      })
    28      .join('/');
    29  }
    30  
    31  function appendQueryParams(path, queryParams) {
    32    if (Object.keys(queryParams).length) {
    33      path += `?${$.param(queryParams)}`;
    34    }
    35  
    36    return path;
    37  }
    38  /**
    39   * Custom implementation of `visitable`
    40   * Currently aims to be compatible and as close as possible to the
    41   * actual `ember-cli-page-object` version
    42   *
    43   * Additions:
    44   * 1. Injectable encoder, for when you don't want your segments to be encoded
    45   *    or you have specific encoding needs
    46   *    Specifically in my case for KV urls where the `Key`/Slug shouldn't be encoded,
    47   *    defaults to the browsers `encodeURIComponent` for compatibility and ease.
    48   * 2. `path` can be an array of (string) paths OR a string for compatibility.
    49   *    If a path cannot be generated due to a lack of properties on the
    50   *    dynamic segment params, if will keep trying 'path' in the array
    51   *    until it finds one that it can construct. This follows the same thinking
    52   *    as 'if you don't specify an item, then we are looking to create one'
    53   */
    54  export function visitable(path, encoder = encodeURIComponent) {
    55    return {
    56      isDescriptor: true,
    57  
    58      value(dynamicSegmentsAndQueryParams = {}) {
    59        let executionContext = getExecutionContext(this);
    60  
    61        return executionContext.runAsync(context => {
    62          var params;
    63          let fullPath = (function _try(paths) {
    64            const path = paths.shift();
    65            params = assign({}, dynamicSegmentsAndQueryParams);
    66            var fullPath;
    67            try {
    68              fullPath = fillInDynamicSegments(path, params, encoder);
    69            } catch (e) {
    70              if (paths.length > 0) {
    71                fullPath = _try(paths);
    72              } else {
    73                throw e;
    74              }
    75            }
    76            return fullPath;
    77          })(typeof path === 'string' ? [path] : path.slice(0));
    78          fullPath = appendQueryParams(fullPath, params);
    79  
    80          return context.visit(fullPath);
    81        });
    82      },
    83    };
    84  }