@testspectra/react 1.2.3 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -199,6 +199,18 @@ function mockResponseBodyText(body) {
199
199
  function parseElementSentinel(selector) {
200
200
  const closest = /^(.*)::closest\((.+)\)$/.exec(selector);
201
201
  if (closest) return { kind: "closest", base: closest[1], arg: closest[2] };
202
+ const children = /^(.*)::children\((.*)\)$/.exec(selector);
203
+ if (children) return { kind: "children", base: children[1], arg: children[2].trim() || null };
204
+ const bareChildren = /^(.*)::children$/.exec(selector);
205
+ if (bareChildren) return { kind: "children", base: bareChildren[1], arg: null };
206
+ const siblings = /^(.*)::siblings\((.*)\)$/.exec(selector);
207
+ if (siblings) return { kind: "siblings", base: siblings[1], arg: siblings[2].trim() || null };
208
+ const bareSiblings = /^(.*)::siblings$/.exec(selector);
209
+ if (bareSiblings) return { kind: "siblings", base: bareSiblings[1], arg: null };
210
+ const next = /^(.*)::next\((.*)\)$/.exec(selector);
211
+ if (next) return { kind: "next", base: next[1], arg: next[2].trim() || null };
212
+ const bareNext = /^(.*)::next$/.exec(selector);
213
+ if (bareNext) return { kind: "next", base: bareNext[1], arg: null };
202
214
  const parent = /^(.*)::parent$/.exec(selector);
203
215
  if (parent) return { kind: "parent", base: parent[1] };
204
216
  return null;
@@ -401,23 +413,50 @@ function resolveEl(selector, index) {
401
413
  function selectorText(target) {
402
414
  return typeof target === "string" ? target : target.selector;
403
415
  }
404
- function resolveSentinel(raw, parent) {
416
+ function pickFromList(list, index) {
417
+ if (index === null || index === void 0) return list[0] ?? null;
418
+ if (index >= 0) return list[index] ?? null;
419
+ return list[list.length + index] ?? null;
420
+ }
421
+ function matchesSelector(el, selector) {
422
+ if (!selector) return true;
423
+ try {
424
+ return el.matches(selector);
425
+ } catch {
426
+ return false;
427
+ }
428
+ }
429
+ function resolveSentinelList(raw, parent) {
405
430
  const sentinel = parseElementSentinel(raw);
406
431
  if (!sentinel) return void 0;
407
432
  const baseTarget = parent ? { selector: sentinel.base, index: null, parent } : sentinel.base;
408
433
  const base = resolveScopedEl(baseTarget, null);
409
- if (!base) return null;
410
- if (sentinel.kind === "parent") return base.parentElement ?? null;
411
- return base.closest(sentinel.arg) ?? null;
434
+ if (sentinel.kind === "parent") return base?.parentElement ? [base.parentElement] : [];
435
+ if (sentinel.kind === "closest") {
436
+ const ancestor = base?.closest(sentinel.arg) ?? null;
437
+ return ancestor ? [ancestor] : [];
438
+ }
439
+ if (!base) return [];
440
+ if (sentinel.kind === "children") {
441
+ return Array.from(base.children).filter((child) => matchesSelector(child, sentinel.arg));
442
+ }
443
+ if (sentinel.kind === "siblings") {
444
+ const parentEl = base.parentElement;
445
+ if (!parentEl) return [];
446
+ return Array.from(parentEl.children).filter((child) => child !== base && matchesSelector(child, sentinel.arg));
447
+ }
448
+ let sibling = base.nextElementSibling;
449
+ while (sibling && !matchesSelector(sibling, sentinel.arg)) sibling = sibling.nextElementSibling;
450
+ return sibling ? [sibling] : [];
412
451
  }
413
452
  function resolveScopedEl(target, fallbackIndex) {
414
453
  if (typeof target === "string") {
415
- const sentinel = resolveSentinel(target, void 0);
416
- if (sentinel !== void 0) return sentinel;
454
+ const sentinelList2 = resolveSentinelList(target, void 0);
455
+ if (sentinelList2 !== void 0) return pickFromList(sentinelList2, fallbackIndex);
417
456
  return resolveEl(target, fallbackIndex);
418
457
  }
419
- const childSentinel = resolveSentinel(target.selector, target.parent);
420
- if (childSentinel !== void 0) return childSentinel;
458
+ const sentinelList = resolveSentinelList(target.selector, target.parent);
459
+ if (sentinelList !== void 0) return pickFromList(sentinelList, target.index ?? fallbackIndex);
421
460
  const parentEl = target.parent ? resolveScopedEl(target.parent, target.parent.index) : document.documentElement;
422
461
  if (!parentEl) return null;
423
462
  const css = resolveSelector(target.selector);
@@ -758,8 +797,12 @@ var DomDriverBridge = class {
758
797
  }
759
798
  async count(selector) {
760
799
  if (typeof selector === "string") {
800
+ const sentinelList2 = resolveSentinelList(selector, void 0);
801
+ if (sentinelList2 !== void 0) return sentinelList2.length;
761
802
  return document.querySelectorAll(resolveSelector(selector)).length;
762
803
  }
804
+ const sentinelList = resolveSentinelList(selector.selector, selector.parent);
805
+ if (sentinelList !== void 0) return sentinelList.length;
763
806
  const parentEl = selector.parent ? resolveScopedEl(selector.parent, selector.parent.index) : document.documentElement;
764
807
  if (!parentEl) return 0;
765
808
  return parentEl.querySelectorAll(resolveSelector(selector.selector)).length;