handleAssert function

Last updated: 2026-03-05T20:45:07.036Z

Metrics

LOC: 74 Complexity: 28 Params: 1

Signature

handleAssert(step: FlowStep): : StepResult

Architecture violations

View all

  • [warning] max-cyclomatic-complexity: 'handleAssert' has cyclomatic complexity 28 (max 10)

Source Code

function handleAssert(step: FlowStep): StepResult {
  if (!step.assertion) {
    return { status: "skipped", reason: "Assert step has no assertion config" };
  }

  const { operator, expected } = step.assertion;

  switch (operator) {
    case "visible": {
      const el = findElement(step);
      if (!el)
        return { status: "failed", error: "Element not visible (not found)" };
      if (el instanceof HTMLElement && el.offsetParent === null) {
        return { status: "failed", error: "Element not visible (hidden)" };
      }
      return { status: "success" };
    }

    case "hidden": {
      const el = findElement(step);
      if (!el) return { status: "success" }; // not found = hidden
      if (el instanceof HTMLElement && el.offsetParent === null) {
        return { status: "success" };
      }
      return { status: "failed", error: "Element is visible" };
    }

    case "exists": {
      const el = findElement(step);
      return el
        ? { status: "success" }
        : { status: "failed", error: "Element does not exist" };
    }

    case "equals": {
      const el = findElement(step);
      if (!el) return { status: "failed", error: "Element not found" };
      const text =
        el instanceof HTMLInputElement ? el.value : (el.textContent ?? "");
      return text === expected
        ? { status: "success" }
        : { status: "failed", error: `Expected "${expected}", got "${text}"` };
    }

    case "contains": {
      const el = findElement(step);
      if (!el) return { status: "failed", error: "Element not found" };
      const text =
        el instanceof HTMLInputElement ? el.value : (el.textContent ?? "");
      return expected && text.includes(expected)
        ? { status: "success" }
        : { status: "failed", error: `Text does not contain "${expected}"` };
    }

    case "url-equals":
      return window.location.href === expected
        ? { status: "success" }
        : {
            status: "failed",
            error: `URL: expected "${expected}", got "${window.location.href}"`,
          };

    case "url-contains":
      return expected && window.location.href.includes(expected)
        ? { status: "success" }
        : { status: "failed", error: `URL does not contain "${expected}"` };

    default:
      return {
        status: "skipped",
        reason: `Unknown assertion operator: ${operator}`,
      };
  }
}

Dependencies (Outgoing)

graph LR handleAssert["handleAssert"] findElement["findElement"] handleAssert -->|calls| findElement style handleAssert fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handleAssert "fb80a5184e1878d1.html" click findElement "db37f185eea489b4.html"
TargetType
findElement calls

Impact (Incoming)

graph LR handleAssert["handleAssert"] executeStep["executeStep"] executeStep -->|calls| handleAssert style handleAssert fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handleAssert "fb80a5184e1878d1.html" click executeStep "a26ccfb820921de2.html"
SourceType
executeStep calls