github.com/hernad/nomad@v1.6.112/ui/tests/utils/ember-power-select-extensions.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import { click, settled } from '@ember/test-helpers';
     7  // TODO: Contribute these helpers back upstream once we are on the latest version of
     8  // ember-power-select (4.x)
     9  //
    10  // selectOpen and selectOpenChoose splits the existing selectChoose helper into two pieces
    11  //   - selectOpen: open the select (await settled)
    12  //   - selectOpenChoose: choose an option (await settled)
    13  // Since the composite helper has two `await setted`s in it, the log changing tests can't use
    14  // them. These tests require a run.later(run, run.cancelTimers, ms) to be inserted between
    15  // these two moments. Doing it before opening means hanging on open not on select. Doing it
    16  // after means hanging after the select has occurred (too late).
    17  async function openIfClosedAndGetContentId(trigger) {
    18    let contentId =
    19      trigger.attributes['aria-owns'] &&
    20      `${trigger.attributes['aria-owns'].value}`;
    21    let content = contentId ? document.querySelector(`#${contentId}`) : undefined;
    22    // If the dropdown is closed, open it
    23    if (
    24      !content ||
    25      content.classList.contains('ember-basic-dropdown-content-placeholder')
    26    ) {
    27      await click(trigger);
    28  
    29      contentId = `${trigger.attributes['aria-owns'].value}`;
    30    }
    31    return contentId;
    32  }
    33  
    34  export async function selectOpen(cssPathOrTrigger) {
    35    let trigger;
    36    if (cssPathOrTrigger instanceof HTMLElement) {
    37      if (cssPathOrTrigger.classList.contains('ember-power-select-trigger')) {
    38        trigger = cssPathOrTrigger;
    39      } else {
    40        trigger = cssPathOrTrigger.querySelector('.ember-power-select-trigger');
    41      }
    42    } else {
    43      trigger = document.querySelector(
    44        `${cssPathOrTrigger} .ember-power-select-trigger`
    45      );
    46  
    47      if (!trigger) {
    48        trigger = document.querySelector(cssPathOrTrigger);
    49      }
    50  
    51      if (!trigger) {
    52        throw new Error(
    53          `You called "selectOpen('${cssPathOrTrigger}')" but no select was found using selector "${cssPathOrTrigger}"`
    54        );
    55      }
    56    }
    57  
    58    if (trigger.scrollIntoView) {
    59      trigger.scrollIntoView();
    60    }
    61  
    62    return await openIfClosedAndGetContentId(trigger);
    63  }
    64  
    65  export async function selectOpenChoose(
    66    contentId,
    67    valueOrSelector,
    68    optionIndex
    69  ) {
    70    let target;
    71    // Select the option with the given text
    72    let options = document.querySelectorAll(
    73      `#${contentId} .ember-power-select-option`
    74    );
    75    let potentialTargets = [].slice
    76      .apply(options)
    77      .filter((opt) => opt.textContent.indexOf(valueOrSelector) > -1);
    78    if (potentialTargets.length === 0) {
    79      potentialTargets = document.querySelectorAll(
    80        `#${contentId} ${valueOrSelector}`
    81      );
    82    }
    83    if (potentialTargets.length > 1) {
    84      let filteredTargets = [].slice
    85        .apply(potentialTargets)
    86        .filter((t) => t.textContent.trim() === valueOrSelector);
    87      if (optionIndex === undefined) {
    88        target = filteredTargets[0] || potentialTargets[0];
    89      } else {
    90        target = filteredTargets[optionIndex] || potentialTargets[optionIndex];
    91      }
    92    } else {
    93      target = potentialTargets[0];
    94    }
    95    if (!target) {
    96      throw new Error(
    97        `You called "selectOpenChoose('${valueOrSelector}')" but "${valueOrSelector}" didn't match any option`
    98      );
    99    }
   100    await click(target);
   101    return settled();
   102  }