@redrockswebdevelopment/formstack-form-testing 0.1.22 → 0.1.24

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
@@ -904,12 +904,46 @@ export function buildLiveFormAddressValue(value, current = {}) {
904
904
  export function buildLiveFormChoiceValue(value, otherValue = "") {
905
905
  return { value, otherValue };
906
906
  }
907
+ export function readLiveFormChoiceValue(value) {
908
+ if (typeof value === "string")
909
+ return value;
910
+ if (!value || typeof value !== "object")
911
+ return null;
912
+ const selected = value.value;
913
+ return typeof selected === "string" && selected.length > 0 ? selected : null;
914
+ }
915
+ export function normalizeLiveFormChoiceValue(value, otherValue = "") {
916
+ if (typeof value === "string")
917
+ return buildLiveFormChoiceValue(value, otherValue);
918
+ return buildLiveFormChoiceValue(value.value, typeof value.otherValue === "string" ? value.otherValue : otherValue);
919
+ }
907
920
  export function buildLiveFormSelectValue(value) {
908
921
  return { value };
909
922
  }
910
923
  export function buildLiveFormCheckboxValue(values, otherValue = null) {
911
924
  return Object.fromEntries([...values.map((value, index) => [String(index), value]), ["otherValue", otherValue]]);
912
925
  }
926
+ export function readLiveFormCheckboxValues(value) {
927
+ if (Array.isArray(value)) {
928
+ return value.filter((entry) => typeof entry === "string");
929
+ }
930
+ if (!value || typeof value !== "object")
931
+ return [];
932
+ const record = value;
933
+ if (Array.isArray(record.values)) {
934
+ return record.values.filter((entry) => typeof entry === "string");
935
+ }
936
+ return Object.entries(record)
937
+ .filter(([key, entry]) => /^\d+$/.test(key) && typeof entry === "string" && entry.length > 0)
938
+ .sort(([left], [right]) => Number(left) - Number(right))
939
+ .map(([, entry]) => entry);
940
+ }
941
+ export function normalizeLiveFormCheckboxValue(value, otherValue = null) {
942
+ const explicitOtherValue = value && typeof value === "object" && !Array.isArray(value) && "otherValue" in value
943
+ ? value.otherValue
944
+ : otherValue;
945
+ return buildLiveFormCheckboxValue(readLiveFormCheckboxValues(value), typeof explicitOtherValue === "string" ? explicitOtherValue : explicitOtherValue === null ? null : otherValue);
946
+ }
913
947
  export function buildLiveFormDateTimeValue(value, current = {}) {
914
948
  return {
915
949
  ...(current && typeof current === "object" ? current : {}),
@@ -1696,6 +1730,158 @@ export async function waitForLiveFormDomFieldState(page, options) {
1696
1730
  }
1697
1731
  return latest;
1698
1732
  }
1733
+ export async function readLiveFormCheckboxState(page, options) {
1734
+ if (!hasEvaluate(page)) {
1735
+ throw new Error("readLiveFormCheckboxState requires a Playwright page with evaluate().");
1736
+ }
1737
+ return page.evaluate((stateOptions) => {
1738
+ const controlSnapshot = (control) => ({
1739
+ tagName: control.tagName.toLowerCase(),
1740
+ type: control.getAttribute("type"),
1741
+ id: control.getAttribute("id"),
1742
+ name: control.getAttribute("name"),
1743
+ value: control.getAttribute("value"),
1744
+ currentValue: control.value,
1745
+ checked: control.checked,
1746
+ });
1747
+ const root = stateOptions.selector != null
1748
+ ? document.querySelector(stateOptions.selector)
1749
+ : stateOptions.internalLabel != null
1750
+ ? document.querySelector(`[data-internal-label="${CSS.escape(stateOptions.internalLabel)}"]`)
1751
+ : stateOptions.fieldId == null
1752
+ ? null
1753
+ : document.querySelector(`#fsCell${CSS.escape(String(stateOptions.fieldId))}, #fsFieldCell${CSS.escape(String(stateOptions.fieldId))}, [data-testid="field-${CSS.escape(String(stateOptions.fieldId))}"]`);
1754
+ const controls = root
1755
+ ? Array.from(root.querySelectorAll("input[type=\"checkbox\"]")).map(controlSnapshot)
1756
+ : [];
1757
+ const selectedValues = controls
1758
+ .filter((control) => control.checked === true)
1759
+ .map((control) => control.currentValue ?? control.value ?? "")
1760
+ .filter((value) => value.length > 0);
1761
+ const allValues = controls
1762
+ .map((control) => control.currentValue ?? control.value ?? "")
1763
+ .filter((value) => value.length > 0);
1764
+ const expectedValues = [...(stateOptions.expectedValues ?? [])];
1765
+ const missingValues = expectedValues.filter((value) => !selectedValues.includes(value));
1766
+ const unexpectedValues = selectedValues.filter((value) => !expectedValues.includes(value));
1767
+ const exact = stateOptions.exact !== false;
1768
+ return {
1769
+ fieldMatched: Boolean(root),
1770
+ selectedValues,
1771
+ allValues,
1772
+ expectedValues,
1773
+ missingValues,
1774
+ unexpectedValues,
1775
+ exact,
1776
+ matched: Boolean(root) && missingValues.length === 0 && (!exact || unexpectedValues.length === 0),
1777
+ controls,
1778
+ };
1779
+ }, options);
1780
+ }
1781
+ export async function waitForLiveFormCheckboxState(page, options) {
1782
+ const timeoutMs = options.timeoutMs ?? 10_000;
1783
+ const intervalMs = options.intervalMs ?? 100;
1784
+ const startedAt = Date.now();
1785
+ let latest = await readLiveFormCheckboxState(page, options);
1786
+ while (!latest.matched && Date.now() - startedAt < timeoutMs) {
1787
+ await delay(intervalMs);
1788
+ latest = await readLiveFormCheckboxState(page, options);
1789
+ }
1790
+ return latest;
1791
+ }
1792
+ export async function diagnoseLiveFormCommitControl(page, options) {
1793
+ if (!hasEvaluate(page)) {
1794
+ throw new Error("diagnoseLiveFormCommitControl requires a Playwright page with evaluate().");
1795
+ }
1796
+ return page.evaluate((commitOptions) => {
1797
+ const element = document.querySelector(commitOptions.selector);
1798
+ if (!(element instanceof HTMLElement)) {
1799
+ return {
1800
+ selector: commitOptions.selector,
1801
+ exists: false,
1802
+ visible: null,
1803
+ disabled: null,
1804
+ ariaDisabled: null,
1805
+ text: null,
1806
+ reason: "missing",
1807
+ ready: false,
1808
+ };
1809
+ }
1810
+ const style = getComputedStyle(element);
1811
+ const rect = element.getBoundingClientRect();
1812
+ const disabled = "disabled" in element ? Boolean(element.disabled) : element.getAttribute("disabled") !== null;
1813
+ const ariaDisabled = element.getAttribute("aria-disabled") === "true";
1814
+ const visible = style.display !== "none" && style.visibility !== "hidden" && Number(style.opacity) !== 0 && rect.width > 0 && rect.height > 0;
1815
+ const reason = !visible ? (style.display === "none" ? "display-none" : style.visibility === "hidden" ? "visibility-hidden" : rect.width <= 0 || rect.height <= 0 ? "zero-size" : "transparent")
1816
+ : disabled ? "disabled"
1817
+ : ariaDisabled ? "aria-disabled"
1818
+ : null;
1819
+ return {
1820
+ selector: commitOptions.selector,
1821
+ exists: true,
1822
+ visible,
1823
+ disabled,
1824
+ ariaDisabled,
1825
+ text: element.textContent?.replace(/\s+/g, " ").trim() ?? "",
1826
+ reason,
1827
+ ready: visible && !disabled && !ariaDisabled,
1828
+ };
1829
+ }, options);
1830
+ }
1831
+ export async function readLiveFormChoiceState(page, options) {
1832
+ if (!hasEvaluate(page)) {
1833
+ throw new Error("readLiveFormChoiceState requires a Playwright page with evaluate().");
1834
+ }
1835
+ return page.evaluate((stateOptions) => {
1836
+ const controlSnapshot = (control) => ({
1837
+ tagName: control.tagName.toLowerCase(),
1838
+ type: control.getAttribute("type"),
1839
+ id: control.getAttribute("id"),
1840
+ name: control.getAttribute("name"),
1841
+ value: control.getAttribute("value"),
1842
+ currentValue: control.value,
1843
+ checked: control.checked,
1844
+ });
1845
+ const root = stateOptions.selector != null
1846
+ ? document.querySelector(stateOptions.selector)
1847
+ : stateOptions.internalLabel != null
1848
+ ? document.querySelector(`[data-internal-label="${CSS.escape(stateOptions.internalLabel)}"]`)
1849
+ : stateOptions.fieldId == null
1850
+ ? null
1851
+ : document.querySelector(`#fsCell${CSS.escape(String(stateOptions.fieldId))}, #fsFieldCell${CSS.escape(String(stateOptions.fieldId))}, [data-testid="field-${CSS.escape(String(stateOptions.fieldId))}"]`);
1852
+ const controls = root
1853
+ ? Array.from(root.querySelectorAll("input[type=\"radio\"]")).map(controlSnapshot)
1854
+ : [];
1855
+ const selectedControls = controls.filter((control) => control.checked === true);
1856
+ const selectedValue = selectedControls.length === 1
1857
+ ? selectedControls[0]?.currentValue ?? selectedControls[0]?.value ?? null
1858
+ : null;
1859
+ const allValues = controls
1860
+ .map((control) => control.currentValue ?? control.value ?? "")
1861
+ .filter((value) => value.length > 0);
1862
+ return {
1863
+ fieldMatched: Boolean(root),
1864
+ selectedValue,
1865
+ allValues,
1866
+ ...(stateOptions.expectedValue === undefined ? {} : { expectedValue: stateOptions.expectedValue }),
1867
+ matched: Boolean(root) &&
1868
+ selectedControls.length === 1 &&
1869
+ (stateOptions.expectedValue === undefined || selectedValue === stateOptions.expectedValue),
1870
+ controls,
1871
+ };
1872
+ }, options);
1873
+ }
1874
+ export async function waitForLiveFormChoiceState(page, options) {
1875
+ const timeoutMs = options.timeoutMs ?? 10_000;
1876
+ const intervalMs = options.intervalMs ?? 100;
1877
+ const startedAt = Date.now();
1878
+ let latest = await readLiveFormChoiceState(page, options);
1879
+ while (!latest.matched && Date.now() - startedAt < timeoutMs) {
1880
+ await delay(intervalMs);
1881
+ latest = await readLiveFormChoiceState(page, options);
1882
+ }
1883
+ return latest;
1884
+ }
1699
1885
  export async function dispatchLiveFormFieldDomEvents(page, options) {
1700
1886
  if (!hasEvaluate(page)) {
1701
1887
  throw new Error("dispatchLiveFormFieldDomEvents requires a Playwright page with evaluate().");
@@ -3231,21 +3417,145 @@ export async function performLiveFormFieldTransaction(page, options) {
3231
3417
  afterQuiet: options.afterQuiet ?? true,
3232
3418
  });
3233
3419
  }
3234
- export async function answerProgressiveLiveFormField(page, options) {
3235
- const validationOptions = options.afterValidation === undefined ? undefined : typeof options.afterValidation === "object" ? options.afterValidation : {};
3420
+ export async function setLiveFormCheckboxValues(page, options) {
3421
+ const payload = normalizeLiveFormCheckboxValue(options.values);
3422
+ const values = readLiveFormCheckboxValues(payload);
3236
3423
  const transaction = await performLiveFormFieldTransaction(page, {
3237
3424
  write: {
3238
3425
  ...(options.write ?? {}),
3239
3426
  target: options.target,
3240
- value: options.value,
3427
+ value: payload,
3241
3428
  notify: options.write?.notify ?? true,
3242
3429
  },
3243
- ...(options.fieldKind ? { fieldKind: options.fieldKind } : {}),
3430
+ fieldKind: "checkbox",
3244
3431
  waitForEvent: options.waitForEvent ?? true,
3245
3432
  dispatchDomEvents: options.dispatchDomEvents ?? "auto",
3246
- afterQuiet: options.afterQuiet ?? true,
3247
- ...(validationOptions ? { afterValidation: validationOptions } : {}),
3433
+ afterQuiet: options.afterQuiet ?? { quietMs: 200, timeoutMs: 5_000 },
3434
+ ...(options.afterValidation === undefined ? {} : { afterValidation: options.afterValidation }),
3248
3435
  });
3436
+ const dom = await waitForLiveFormCheckboxState(page, {
3437
+ ...options.target,
3438
+ expectedValues: values,
3439
+ exact: options.exactDomSelection ?? true,
3440
+ ...(options.write?.timeoutMs === undefined ? {} : { timeoutMs: options.write.timeoutMs }),
3441
+ });
3442
+ let commitControl;
3443
+ let clickedCommit = false;
3444
+ if (options.commitSelector) {
3445
+ const deadline = Date.now() + (options.commitTimeoutMs ?? 5_000);
3446
+ commitControl = await diagnoseLiveFormCommitControl(page, { selector: options.commitSelector });
3447
+ while (!commitControl.ready && Date.now() < deadline) {
3448
+ await delay(100);
3449
+ commitControl = await diagnoseLiveFormCommitControl(page, { selector: options.commitSelector });
3450
+ }
3451
+ if (options.clickCommit === true) {
3452
+ if (!commitControl.ready) {
3453
+ throw new Error(`Checkbox commit control "${options.commitSelector}" is not ready after selecting ${values.length} value(s): ${commitControl.reason ?? "unknown"}.`);
3454
+ }
3455
+ await page.locator(options.commitSelector).first().click({ timeout: options.commitTimeoutMs ?? 5_000 });
3456
+ clickedCommit = true;
3457
+ }
3458
+ }
3459
+ return {
3460
+ payload,
3461
+ values,
3462
+ transaction,
3463
+ dom,
3464
+ ...(commitControl ? { commitControl } : {}),
3465
+ clickedCommit,
3466
+ ok: transaction.write.valueMatched &&
3467
+ (transaction.event?.matched ?? true) &&
3468
+ (transaction.domDispatch ? transaction.domDispatch.matchedControlCount >= values.length : true) &&
3469
+ (transaction.quiet?.quiet ?? true) &&
3470
+ ((transaction.validation?.inlineErrors.length ?? 0) === 0) &&
3471
+ dom.matched &&
3472
+ (commitControl?.ready ?? true),
3473
+ };
3474
+ }
3475
+ export async function setLiveFormChoiceValue(page, options) {
3476
+ const payload = normalizeLiveFormChoiceValue(options.value);
3477
+ const value = payload.value;
3478
+ const transaction = await performLiveFormFieldTransaction(page, {
3479
+ write: {
3480
+ ...(options.write ?? {}),
3481
+ target: options.target,
3482
+ value: payload,
3483
+ notify: options.write?.notify ?? true,
3484
+ },
3485
+ fieldKind: "radio",
3486
+ waitForEvent: options.waitForEvent ?? true,
3487
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
3488
+ afterQuiet: options.afterQuiet ?? { quietMs: 200, timeoutMs: 5_000 },
3489
+ ...(options.afterValidation === undefined ? {} : { afterValidation: options.afterValidation }),
3490
+ });
3491
+ const dom = await waitForLiveFormChoiceState(page, {
3492
+ ...options.target,
3493
+ expectedValue: value,
3494
+ ...(options.write?.timeoutMs === undefined ? {} : { timeoutMs: options.write.timeoutMs }),
3495
+ });
3496
+ const visibilityTargets = [
3497
+ ...(options.expectVisible ?? []).map((target) => ({ ...target, visible: true })),
3498
+ ...(options.expectHidden ?? []).map((target) => ({ ...target, visible: false })),
3499
+ ];
3500
+ const visibility = visibilityTargets.length > 0
3501
+ ? await waitForLiveFormVisibility(page, { targets: visibilityTargets })
3502
+ : undefined;
3503
+ return {
3504
+ payload,
3505
+ value,
3506
+ transaction,
3507
+ dom,
3508
+ ...(visibility ? { visibility } : {}),
3509
+ ok: transaction.write.valueMatched &&
3510
+ (transaction.event?.matched ?? true) &&
3511
+ (transaction.domDispatch ? transaction.domDispatch.matchedControlCount === 1 : true) &&
3512
+ (transaction.quiet?.quiet ?? true) &&
3513
+ ((transaction.validation?.inlineErrors.length ?? 0) === 0) &&
3514
+ dom.matched &&
3515
+ (visibility?.matched ?? true),
3516
+ };
3517
+ }
3518
+ export async function answerProgressiveLiveFormField(page, options) {
3519
+ const validationOptions = options.afterValidation === undefined ? undefined : typeof options.afterValidation === "object" ? options.afterValidation : {};
3520
+ const checkboxTransaction = options.fieldKind === "checkbox"
3521
+ ? await setLiveFormCheckboxValues(page, {
3522
+ target: options.target,
3523
+ values: options.value,
3524
+ ...(options.write === undefined ? {} : { write: options.write }),
3525
+ waitForEvent: options.waitForEvent ?? true,
3526
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
3527
+ afterQuiet: options.afterQuiet ?? true,
3528
+ ...(validationOptions ? { afterValidation: validationOptions } : {}),
3529
+ })
3530
+ : undefined;
3531
+ const choiceTransaction = !checkboxTransaction && options.fieldKind === "radio"
3532
+ ? await setLiveFormChoiceValue(page, {
3533
+ target: options.target,
3534
+ value: options.value,
3535
+ ...(options.write === undefined ? {} : { write: options.write }),
3536
+ waitForEvent: options.waitForEvent ?? true,
3537
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
3538
+ afterQuiet: options.afterQuiet ?? true,
3539
+ ...(validationOptions ? { afterValidation: validationOptions } : {}),
3540
+ })
3541
+ : undefined;
3542
+ const transaction = checkboxTransaction
3543
+ ? checkboxTransaction.transaction
3544
+ : choiceTransaction
3545
+ ? choiceTransaction.transaction
3546
+ : await performLiveFormFieldTransaction(page, {
3547
+ write: {
3548
+ ...(options.write ?? {}),
3549
+ target: options.target,
3550
+ value: options.value,
3551
+ notify: options.write?.notify ?? true,
3552
+ },
3553
+ ...(options.fieldKind ? { fieldKind: options.fieldKind } : {}),
3554
+ waitForEvent: options.waitForEvent ?? true,
3555
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
3556
+ afterQuiet: options.afterQuiet ?? true,
3557
+ ...(validationOptions ? { afterValidation: validationOptions } : {}),
3558
+ });
3249
3559
  const visibilityTargets = [
3250
3560
  ...(options.expectVisible ?? []),
3251
3561
  ...(options.expectHidden ?? []).map((target) => ({ ...target, visible: false })),
@@ -3448,13 +3758,10 @@ export function normalizeLiveFormSampleValue(kind = "unknown", value) {
3448
3758
  if (value instanceof Date)
3449
3759
  return value.toISOString().slice(0, 10);
3450
3760
  if (["radio", "rating", "select"].includes(kind) && typeof value === "string") {
3451
- return { value };
3452
- }
3453
- if (kind === "checkbox" && Array.isArray(value)) {
3454
- return buildLiveFormCheckboxValue(value.filter((entry) => typeof entry === "string"));
3761
+ return kind === "radio" ? normalizeLiveFormChoiceValue(value) : { value };
3455
3762
  }
3456
- if (kind === "checkbox" && value && typeof value === "object" && Array.isArray(value.values)) {
3457
- return buildLiveFormCheckboxValue(value.values.filter((entry) => typeof entry === "string"));
3763
+ if (kind === "checkbox" && (Array.isArray(value) || (value && typeof value === "object"))) {
3764
+ return normalizeLiveFormCheckboxValue(value);
3458
3765
  }
3459
3766
  if (kind === "checkbox" && typeof value === "boolean") {
3460
3767
  return { checked: value };
@@ -4670,9 +4977,33 @@ export const liveFormFieldRecipes = {
4670
4977
  }),
4671
4978
  setNameApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormNameValue(value), options),
4672
4979
  setAddressApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormAddressValue(value), options),
4673
- setChoiceApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormChoiceValue(value), options),
4980
+ setChoiceApi: (name, internalLabel, value, options = {}) => ({
4981
+ name,
4982
+ run: async (harness) => {
4983
+ await setLiveFormChoiceValue(harness.page, {
4984
+ target: { internalLabel },
4985
+ value,
4986
+ write: { notify: options.notify ?? true },
4987
+ waitForEvent: options.waitForEvent ?? (options.notify ?? true),
4988
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
4989
+ afterQuiet: options.waitForQuiet ?? true,
4990
+ });
4991
+ },
4992
+ }),
4674
4993
  setSelectApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormSelectValue(value), options),
4675
- setCheckboxesApi: (name, internalLabel, values, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormCheckboxValue(values), options),
4994
+ setCheckboxesApi: (name, internalLabel, values, options = {}) => ({
4995
+ name,
4996
+ run: async (harness) => {
4997
+ await setLiveFormCheckboxValues(harness.page, {
4998
+ target: { internalLabel },
4999
+ values,
5000
+ write: { notify: options.notify ?? true },
5001
+ waitForEvent: options.waitForEvent ?? (options.notify ?? true),
5002
+ dispatchDomEvents: options.dispatchDomEvents ?? "auto",
5003
+ afterQuiet: options.waitForQuiet ?? true,
5004
+ });
5005
+ },
5006
+ }),
4676
5007
  setDateTimeApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormDateTimeValue(value), options),
4677
5008
  setMatrixApi: (name, internalLabel, selections, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormMatrixValue(selections), options),
4678
5009
  setRatingApi: (name, internalLabel, value, options) => apiRecipeStep(name, { internalLabel }, buildLiveFormRatingValue(value), options),