@testing-library/user-event 14.0.0 → 14.0.1

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.cjs CHANGED
@@ -511,11 +511,11 @@ function editInputElement(config, element, inputRange, data, inputType) {
511
511
  });
512
512
  if (isDateOrTime(element)) {
513
513
  if (isValidDateOrTimeValue(element, newValue)) {
514
- commitInput(config, element, oldValue, newValue, newOffset, {});
514
+ commitInput(config, element, newOffset, {});
515
515
  dispatchUIEvent(config, element, "change");
516
516
  }
517
517
  } else {
518
- commitInput(config, element, oldValue, newValue, newOffset, {
518
+ commitInput(config, element, newOffset, {
519
519
  data,
520
520
  inputType
521
521
  });
@@ -545,11 +545,10 @@ function calculateNewValue(inputData, node, {
545
545
  newOffset
546
546
  };
547
547
  }
548
- function commitInput(config, element, oldValue, newValue, newOffset, inputInit) {
548
+ function commitInput(config, element, newOffset, inputInit) {
549
549
  startTrackValue(element);
550
550
  dispatchUIEvent(config, element, "input", inputInit);
551
- const tracked = endTrackValue(element);
552
- if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
551
+ if (endTrackValue(element)) {
553
552
  setSelection({
554
553
  focusNode: element,
555
554
  anchorOffset: newOffset,
@@ -1440,7 +1439,7 @@ var behavior = {};
1440
1439
 
1441
1440
  // src/event/behavior/click.ts
1442
1441
  behavior.click = (event, target, config) => {
1443
- const context = target.closest("button,input,label,textarea");
1442
+ const context = target.closest("button,input,label,select,textarea");
1444
1443
  const control = context && isElementType(context, "label") && context.control;
1445
1444
  if (control) {
1446
1445
  return () => {
@@ -1734,9 +1733,6 @@ function getUISelection(element) {
1734
1733
  endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1735
1734
  });
1736
1735
  }
1737
- function clearUISelection(element) {
1738
- element[UISelection] = void 0;
1739
- }
1740
1736
 
1741
1737
  // src/document/value.ts
1742
1738
  var UIValue = Symbol("Displayed value in UI");
@@ -1744,17 +1740,23 @@ var InitialValue = Symbol("Initial value to compare on blur");
1744
1740
  var TrackChanges = Symbol("Track programmatic changes for React workaround");
1745
1741
  function valueInterceptor(v) {
1746
1742
  const isUI = typeof v === "object" && v[UIValue];
1747
- this[UIValue] = isUI ? String(v) : void 0;
1748
- if (!isUI) {
1749
- trackValue(this, String(v));
1750
- this[InitialValue] = String(v);
1751
- clearUISelection(this);
1743
+ if (isUI) {
1744
+ this[UIValue] = String(v);
1745
+ setPreviousValue(this, String(this.value));
1746
+ } else {
1747
+ trackOrSetValue(this, String(v));
1752
1748
  }
1753
1749
  return {
1754
1750
  applyNative: !!isUI,
1755
- realArgs: String(v)
1751
+ realArgs: sanitizeValue(this, v)
1756
1752
  };
1757
1753
  }
1754
+ function sanitizeValue(element, v) {
1755
+ if (isElementType(element, "input", { type: "number" }) && String(v) !== "" && !Number.isNaN(Number(v))) {
1756
+ return String(Number(v));
1757
+ }
1758
+ return String(v);
1759
+ }
1758
1760
  function prepareValueInterceptor(element) {
1759
1761
  prepareInterceptor(element, "value", valueInterceptor);
1760
1762
  }
@@ -1773,17 +1775,36 @@ function setInitialValue(element) {
1773
1775
  function getInitialValue(element) {
1774
1776
  return element[InitialValue];
1775
1777
  }
1778
+ function setPreviousValue(element, v) {
1779
+ element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), { previousValue: v });
1780
+ }
1776
1781
  function startTrackValue(element) {
1777
- element[TrackChanges] = [];
1782
+ element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), {
1783
+ nextValue: String(element.value),
1784
+ tracked: []
1785
+ });
1778
1786
  }
1779
- function trackValue(element, v) {
1780
- var _a;
1781
- (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
1787
+ function trackOrSetValue(element, v) {
1788
+ var _a, _b, _c;
1789
+ (_b = (_a = element[TrackChanges]) == null ? void 0 : _a.tracked) == null ? void 0 : _b.push(v);
1790
+ if (!((_c = element[TrackChanges]) == null ? void 0 : _c.tracked)) {
1791
+ setCleanValue(element, v);
1792
+ }
1793
+ }
1794
+ function setCleanValue(element, v) {
1795
+ element[UIValue] = void 0;
1796
+ element[InitialValue] = v;
1797
+ setUISelection(element, { focusOffset: v.length });
1782
1798
  }
1783
1799
  function endTrackValue(element) {
1784
- const tracked = element[TrackChanges];
1800
+ var _a, _b;
1801
+ const changes = element[TrackChanges];
1785
1802
  element[TrackChanges] = void 0;
1786
- return tracked;
1803
+ const isJustReactStateUpdate = ((_a = changes == null ? void 0 : changes.tracked) == null ? void 0 : _a.length) === 2 && changes.tracked[0] === changes.previousValue && changes.tracked[1] === changes.nextValue;
1804
+ if (((_b = changes == null ? void 0 : changes.tracked) == null ? void 0 : _b.length) && !isJustReactStateUpdate) {
1805
+ setCleanValue(element, changes.tracked[changes.tracked.length - 1]);
1806
+ }
1807
+ return isJustReactStateUpdate;
1787
1808
  }
1788
1809
 
1789
1810
  // src/document/index.ts
package/dist/index.mjs CHANGED
@@ -527,11 +527,11 @@ function editInputElement(config, element, inputRange, data, inputType) {
527
527
  });
528
528
  if (isDateOrTime(element)) {
529
529
  if (isValidDateOrTimeValue(element, newValue)) {
530
- commitInput(config, element, oldValue, newValue, newOffset, {});
530
+ commitInput(config, element, newOffset, {});
531
531
  dispatchUIEvent(config, element, "change");
532
532
  }
533
533
  } else {
534
- commitInput(config, element, oldValue, newValue, newOffset, {
534
+ commitInput(config, element, newOffset, {
535
535
  data,
536
536
  inputType
537
537
  });
@@ -561,11 +561,10 @@ function calculateNewValue(inputData, node, {
561
561
  newOffset
562
562
  };
563
563
  }
564
- function commitInput(config, element, oldValue, newValue, newOffset, inputInit) {
564
+ function commitInput(config, element, newOffset, inputInit) {
565
565
  startTrackValue(element);
566
566
  dispatchUIEvent(config, element, "input", inputInit);
567
- const tracked = endTrackValue(element);
568
- if ((tracked == null ? void 0 : tracked.length) === 2 && tracked[0] === oldValue && tracked[1] === newValue) {
567
+ if (endTrackValue(element)) {
569
568
  setSelection({
570
569
  focusNode: element,
571
570
  anchorOffset: newOffset,
@@ -1456,7 +1455,7 @@ var behavior = {};
1456
1455
 
1457
1456
  // src/event/behavior/click.ts
1458
1457
  behavior.click = (event, target, config) => {
1459
- const context = target.closest("button,input,label,textarea");
1458
+ const context = target.closest("button,input,label,select,textarea");
1460
1459
  const control = context && isElementType(context, "label") && context.control;
1461
1460
  if (control) {
1462
1461
  return () => {
@@ -1750,9 +1749,6 @@ function getUISelection(element) {
1750
1749
  endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1751
1750
  });
1752
1751
  }
1753
- function clearUISelection(element) {
1754
- element[UISelection] = void 0;
1755
- }
1756
1752
 
1757
1753
  // src/document/value.ts
1758
1754
  var UIValue = Symbol("Displayed value in UI");
@@ -1760,17 +1756,23 @@ var InitialValue = Symbol("Initial value to compare on blur");
1760
1756
  var TrackChanges = Symbol("Track programmatic changes for React workaround");
1761
1757
  function valueInterceptor(v) {
1762
1758
  const isUI = typeof v === "object" && v[UIValue];
1763
- this[UIValue] = isUI ? String(v) : void 0;
1764
- if (!isUI) {
1765
- trackValue(this, String(v));
1766
- this[InitialValue] = String(v);
1767
- clearUISelection(this);
1759
+ if (isUI) {
1760
+ this[UIValue] = String(v);
1761
+ setPreviousValue(this, String(this.value));
1762
+ } else {
1763
+ trackOrSetValue(this, String(v));
1768
1764
  }
1769
1765
  return {
1770
1766
  applyNative: !!isUI,
1771
- realArgs: String(v)
1767
+ realArgs: sanitizeValue(this, v)
1772
1768
  };
1773
1769
  }
1770
+ function sanitizeValue(element, v) {
1771
+ if (isElementType(element, "input", { type: "number" }) && String(v) !== "" && !Number.isNaN(Number(v))) {
1772
+ return String(Number(v));
1773
+ }
1774
+ return String(v);
1775
+ }
1774
1776
  function prepareValueInterceptor(element) {
1775
1777
  prepareInterceptor(element, "value", valueInterceptor);
1776
1778
  }
@@ -1789,17 +1791,36 @@ function setInitialValue(element) {
1789
1791
  function getInitialValue(element) {
1790
1792
  return element[InitialValue];
1791
1793
  }
1794
+ function setPreviousValue(element, v) {
1795
+ element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), { previousValue: v });
1796
+ }
1792
1797
  function startTrackValue(element) {
1793
- element[TrackChanges] = [];
1798
+ element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), {
1799
+ nextValue: String(element.value),
1800
+ tracked: []
1801
+ });
1794
1802
  }
1795
- function trackValue(element, v) {
1796
- var _a;
1797
- (_a = element[TrackChanges]) == null ? void 0 : _a.push(v);
1803
+ function trackOrSetValue(element, v) {
1804
+ var _a, _b, _c;
1805
+ (_b = (_a = element[TrackChanges]) == null ? void 0 : _a.tracked) == null ? void 0 : _b.push(v);
1806
+ if (!((_c = element[TrackChanges]) == null ? void 0 : _c.tracked)) {
1807
+ setCleanValue(element, v);
1808
+ }
1809
+ }
1810
+ function setCleanValue(element, v) {
1811
+ element[UIValue] = void 0;
1812
+ element[InitialValue] = v;
1813
+ setUISelection(element, { focusOffset: v.length });
1798
1814
  }
1799
1815
  function endTrackValue(element) {
1800
- const tracked = element[TrackChanges];
1816
+ var _a, _b;
1817
+ const changes = element[TrackChanges];
1801
1818
  element[TrackChanges] = void 0;
1802
- return tracked;
1819
+ const isJustReactStateUpdate = ((_a = changes == null ? void 0 : changes.tracked) == null ? void 0 : _a.length) === 2 && changes.tracked[0] === changes.previousValue && changes.tracked[1] === changes.nextValue;
1820
+ if (((_b = changes == null ? void 0 : changes.tracked) == null ? void 0 : _b.length) && !isJustReactStateUpdate) {
1821
+ setCleanValue(element, changes.tracked[changes.tracked.length - 1]);
1822
+ }
1823
+ return isJustReactStateUpdate;
1803
1824
  }
1804
1825
 
1805
1826
  // src/document/index.ts
@@ -23,5 +23,4 @@ export declare function getUISelection(element: HTMLInputElement | HTMLTextAreaE
23
23
  anchorOffset: number;
24
24
  focusOffset: number;
25
25
  };
26
- export declare function clearUISelection(element: HTMLInputElement | HTMLTextAreaElement): void;
27
26
  export {};
@@ -5,7 +5,11 @@ declare global {
5
5
  interface Element {
6
6
  [UIValue]?: string;
7
7
  [InitialValue]?: string;
8
- [TrackChanges]?: string[];
8
+ [TrackChanges]?: {
9
+ previousValue?: string;
10
+ tracked?: string[];
11
+ nextValue?: string;
12
+ };
9
13
  }
10
14
  }
11
15
  export declare function prepareValueInterceptor(element: HTMLInputElement): void;
@@ -14,5 +18,8 @@ export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaEleme
14
18
  export declare function setInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
15
19
  export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
16
20
  export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
17
- export declare function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement): string[] | undefined;
21
+ /**
22
+ * @returns `true` if we recognize a React state reset and update
23
+ */
24
+ export declare function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement): boolean;
18
25
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.0.0",
3
+ "version": "14.0.1",
4
4
  "description": "Fire events the same way the user does",
5
5
  "keywords": [
6
6
  "react-testing-library",