fill method infrastructure ✓ 90.3%

Last updated: 2026-03-04T23:21:38.386Z

Metrics

LOC: 88 Complexity: 15 Params: 2 Coverage: 90.3% (28/31 lines, 0x executed)

Signature

fill(wrapper: HTMLElement, value: string): : Promise<boolean>

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'fill' has cyclomatic complexity 15 (max 10)
  • [warning] max-lines: 'fill' has 88 lines (max 80)

Source Code

  async fill(wrapper: HTMLElement, value: string): Promise<boolean> {
    const isMultiple = wrapper.classList.contains("ant-select-multiple");
    const wrapperSelector = getUniqueSelector(wrapper);

    const combobox = wrapper.querySelector<HTMLInputElement>(
      "input[role='combobox'], .ant-select-selection-search-input, .ant-select-input",
    );

    // Old antd v5 classic: has .ant-select-selector wrapping the search input.
    // New antd v5 CSS-var / v5.17+: no .ant-select-selector; the input is a direct
    // child of .ant-select-content and IS the toggle trigger.
    // IMPORTANT: for the new structure, we must NOT fire mousedown on BOTH the
    // input and its parent — two consecutive mousedowns on the same React handler
    // chain cause an open/close toggle leaving the dropdown closed.
    const selectorEl = wrapper.querySelector<HTMLElement>(
      ".ant-select-selector",
    );

    if (!selectorEl && !combobox) {
      log.warn(`Container do select não encontrado em: ${wrapperSelector}`);
      return false;
    }

    if (selectorEl) {
      // Old structure: focus + mousedown on search input, then simulateClick selector.
      if (combobox) {
        combobox.focus();
        combobox.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
      }
      simulateClick(selectorEl);
    } else {
      // New CSS-var structure: dispatch mousedown ONLY on the input.
      // A full simulateClick (mousedown → mouseup → click) would cause React to
      // process the 'click' handler and toggle the dropdown closed immediately
      // after the 'mousedown' handler opened it.
      if (combobox) {
        combobox.focus();
        combobox.dispatchEvent(
          new MouseEvent("mousedown", { bubbles: true, cancelable: true }),
        );
      } else {
        // Fallback: trigger via the content wrapper
        const contentEl = wrapper.querySelector<HTMLElement>(
          ".ant-select-content",
        );
        if (contentEl) {
          contentEl.dispatchEvent(
            new MouseEvent("mousedown", { bubbles: true, cancelable: true }),
          );
        }
      }
    }

    // Wait for the dropdown to render
    const dropdown = await waitForElement(
      ".ant-select-dropdown:not(.ant-select-dropdown-hidden)",
      800,
    );

    if (!dropdown) {
      // Last attempt: pointerdown on the direct trigger (single event, no double-fire)
      const triggerEl = selectorEl ?? combobox ?? wrapper;
      triggerEl.dispatchEvent(
        new PointerEvent("pointerdown", { bubbles: true, cancelable: true }),
      );
      await new Promise((r) => setTimeout(r, 300));

      const retryDropdown = document.querySelector<HTMLElement>(
        ".ant-select-dropdown:not(.ant-select-dropdown-hidden)",
      );
      if (!retryDropdown) {
        log.warn(
          `Dropdown .ant-select-dropdown não apareceu para: ${wrapperSelector}`,
        );
        return false;
      }
    }

    // Extract listboxId here (before passing to helpers) so both single and
    // multiple paths can scope their dropdown queries to THIS wrapper's portal.
    const listboxId = combobox?.getAttribute("aria-controls") ?? null;

    if (isMultiple) {
      return await selectMultipleOptions(wrapper, value, listboxId);
    }

    return await selectOption(wrapper, value);
  },

Dependencies (Outgoing)

graph LR fill["fill"] simulateClick["simulateClick"] waitForElement["waitForElement"] selectMultipleOptions["selectMultipleOptions"] selectOption["selectOption"] fill -->|calls| simulateClick fill -->|calls| waitForElement fill -->|calls| selectMultipleOptions fill -->|calls| selectOption style fill fill:#dbeafe,stroke:#2563eb,stroke-width:2px click fill "90d6eb0f98a487a1.html" click simulateClick "7c6c21320aaee4c4.html" click waitForElement "230bbf0882ca7c81.html" click selectMultipleOptions "e53f83dda6e2588d.html" click selectOption "fe5ab67a02962060.html"
TargetType
simulateClick calls
waitForElement calls
selectMultipleOptions calls
selectOption calls

No incoming dependencies.