github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/utils/ember-power-select-extensions.js (about)

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