@testing-library/user-event 14.0.2 → 14.1.0

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
@@ -509,7 +509,7 @@ function editContenteditable(config, element, inputRange, data, inputType) {
509
509
  function editInputElement(config, element, inputRange, data, inputType) {
510
510
  let dataToInsert = data;
511
511
  const spaceUntilMaxLength = getSpaceUntilMaxLength(element);
512
- if (spaceUntilMaxLength !== void 0) {
512
+ if (spaceUntilMaxLength !== void 0 && data.length > 0) {
513
513
  if (spaceUntilMaxLength > 0) {
514
514
  dataToInsert = data.substring(0, spaceUntilMaxLength);
515
515
  } else {
@@ -533,6 +533,7 @@ function editInputElement(config, element, inputRange, data, inputType) {
533
533
  if (isValidDateOrTimeValue(element, newValue)) {
534
534
  commitInput(config, element, newOffset, {});
535
535
  dispatchUIEvent(config, element, "change");
536
+ clearInitialValue(element);
536
537
  }
537
538
  } else {
538
539
  commitInput(config, element, newOffset, {
@@ -1217,8 +1218,15 @@ function getLevelRef(config, level) {
1217
1218
  }
1218
1219
 
1219
1220
  // src/utils/misc/wait.ts
1220
- function wait(time) {
1221
- return new Promise((resolve) => setTimeout(() => resolve(), time));
1221
+ function wait(config) {
1222
+ const delay = config.delay;
1223
+ if (typeof delay !== "number") {
1224
+ return;
1225
+ }
1226
+ return Promise.all([
1227
+ new Promise((resolve) => setTimeout(() => resolve(), delay)),
1228
+ config.advanceTimers(delay)
1229
+ ]);
1222
1230
  }
1223
1231
 
1224
1232
  // src/keyboard/keyMap.ts
@@ -1306,7 +1314,8 @@ var defaultOptionsDirect = {
1306
1314
  skipAutoClose: false,
1307
1315
  skipClick: false,
1308
1316
  skipHover: false,
1309
- writeToClipboard: false
1317
+ writeToClipboard: false,
1318
+ advanceTimers: () => Promise.resolve()
1310
1319
  };
1311
1320
  var defaultOptionsSetup = __spreadProps(__spreadValues({}, defaultOptionsDirect), {
1312
1321
  writeToClipboard: true
@@ -1678,13 +1687,18 @@ function prepareInterceptor(element, propName, interceptorImpl) {
1678
1687
  return;
1679
1688
  }
1680
1689
  function intercept(...args) {
1681
- const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
1690
+ const {
1691
+ applyNative = true,
1692
+ realArgs,
1693
+ then
1694
+ } = interceptorImpl.call(this, ...args);
1682
1695
  const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
1683
1696
  if (target === "set") {
1684
1697
  realFunc.call(this, realArgs);
1685
1698
  } else {
1686
1699
  realFunc.call(this, ...realArgs);
1687
1700
  }
1701
+ then == null ? void 0 : then();
1688
1702
  }
1689
1703
  ;
1690
1704
  intercept[Interceptor] = Interceptor;
@@ -1713,6 +1727,13 @@ function prepareSelectionInterceptor(element) {
1713
1727
  this[UISelection] = void 0;
1714
1728
  return { realArgs: v };
1715
1729
  });
1730
+ prepareInterceptor(element, "select", function interceptorImpl() {
1731
+ this[UISelection] = {
1732
+ anchorOffset: 0,
1733
+ focusOffset: getUIValue(element).length
1734
+ };
1735
+ return { realArgs: [] };
1736
+ });
1716
1737
  }
1717
1738
  function setUISelection(element, {
1718
1739
  focusOffset: focusOffsetParam,
@@ -1759,12 +1780,11 @@ function valueInterceptor(v) {
1759
1780
  if (isUI) {
1760
1781
  this[UIValue] = String(v);
1761
1782
  setPreviousValue(this, String(this.value));
1762
- } else {
1763
- trackOrSetValue(this, String(v));
1764
1783
  }
1765
1784
  return {
1766
1785
  applyNative: !!isUI,
1767
- realArgs: sanitizeValue(this, v)
1786
+ realArgs: sanitizeValue(this, v),
1787
+ then: isUI ? void 0 : () => trackOrSetValue(this, String(v))
1768
1788
  };
1769
1789
  }
1770
1790
  function sanitizeValue(element, v) {
@@ -1777,6 +1797,9 @@ function prepareValueInterceptor(element) {
1777
1797
  prepareInterceptor(element, "value", valueInterceptor);
1778
1798
  }
1779
1799
  function setUIValue(element, value) {
1800
+ if (element[InitialValue] === void 0) {
1801
+ element[InitialValue] = element.value;
1802
+ }
1780
1803
  element.value = {
1781
1804
  [UIValue]: UIValue,
1782
1805
  toString: () => value
@@ -1785,8 +1808,8 @@ function setUIValue(element, value) {
1785
1808
  function getUIValue(element) {
1786
1809
  return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1787
1810
  }
1788
- function setInitialValue(element) {
1789
- element[InitialValue] = element.value;
1811
+ function clearInitialValue(element) {
1812
+ element[InitialValue] = void 0;
1790
1813
  }
1791
1814
  function getInitialValue(element) {
1792
1815
  return element[InitialValue];
@@ -1809,7 +1832,6 @@ function trackOrSetValue(element, v) {
1809
1832
  }
1810
1833
  function setCleanValue(element, v) {
1811
1834
  element[UIValue] = void 0;
1812
- element[InitialValue] = v;
1813
1835
  setUISelection(element, { focusOffset: v.length });
1814
1836
  }
1815
1837
  function endTrackValue(element) {
@@ -1842,8 +1864,11 @@ function prepareDocument(document2) {
1842
1864
  document2.addEventListener("blur", (e) => {
1843
1865
  const el = e.target;
1844
1866
  const initialValue = getInitialValue(el);
1845
- if (typeof initialValue === "string" && el.value !== initialValue) {
1846
- dispatchUIEvent({}, el, "change");
1867
+ if (initialValue !== void 0) {
1868
+ if (el.value !== initialValue) {
1869
+ dispatchUIEvent({}, el, "change");
1870
+ }
1871
+ clearInitialValue(el);
1847
1872
  }
1848
1873
  }, {
1849
1874
  capture: true,
@@ -1852,9 +1877,6 @@ function prepareDocument(document2) {
1852
1877
  document2[isPrepared] = isPrepared;
1853
1878
  }
1854
1879
  function prepareElement(el) {
1855
- if ("value" in el) {
1856
- setInitialValue(el);
1857
- }
1858
1880
  if (el[isPrepared]) {
1859
1881
  return;
1860
1882
  }
@@ -1924,13 +1946,13 @@ function postKeyupBehavior(config, { key }, element) {
1924
1946
  async function keyboardAction(config, actions) {
1925
1947
  for (let i = 0; i < actions.length; i++) {
1926
1948
  await keyboardKeyAction(config, actions[i]);
1927
- if (typeof config.delay === "number" && i < actions.length - 1) {
1928
- await wait(config.delay);
1949
+ if (i < actions.length - 1) {
1950
+ await wait(config);
1929
1951
  }
1930
1952
  }
1931
1953
  }
1932
1954
  async function keyboardKeyAction(config, { keyDef, releasePrevious, releaseSelf, repeat }) {
1933
- const { document: document2, keyboardState, delay } = config;
1955
+ const { document: document2, keyboardState } = config;
1934
1956
  const getCurrentElement = () => getActive(document2);
1935
1957
  const pressed = keyboardState.pressed.find((p) => p.keyDef === keyDef);
1936
1958
  if (pressed) {
@@ -1943,8 +1965,8 @@ async function keyboardKeyAction(config, { keyDef, releasePrevious, releaseSelf,
1943
1965
  if (unpreventedDefault && hasKeyPress(keyDef, config)) {
1944
1966
  await keypress(keyDef, getCurrentElement, config);
1945
1967
  }
1946
- if (typeof delay === "number" && i < repeat) {
1947
- await wait(delay);
1968
+ if (i < repeat) {
1969
+ await wait(config);
1948
1970
  }
1949
1971
  }
1950
1972
  if (releaseSelf) {
@@ -2400,7 +2422,6 @@ function mousedownDefaultBehavior({
2400
2422
  node,
2401
2423
  offset
2402
2424
  }) {
2403
- focus(target);
2404
2425
  if (!targetIsDisabled) {
2405
2426
  const hasValue = isElementType(target, ["input", "textarea"]);
2406
2427
  const text = String(hasValue ? getUIValue(target) : target.textContent);
@@ -2435,6 +2456,7 @@ function mousedownDefaultBehavior({
2435
2456
  selection.addRange(range.cloneRange());
2436
2457
  }
2437
2458
  }
2459
+ focus(target);
2438
2460
  }
2439
2461
  function getTextRange(text, pos, clickCount) {
2440
2462
  if (clickCount % 3 === 1 || text.length === 0) {
@@ -2462,10 +2484,8 @@ async function pointerAction(config, actions) {
2462
2484
  const target = (_a3 = action.target) != null ? _a3 : getPrevTarget(pointerName, config.pointerState);
2463
2485
  const coords = (_b = action.coords) != null ? _b : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : void 0;
2464
2486
  await ("keyDef" in action ? pointerPress(config, __spreadProps(__spreadValues({}, action), { target, coords })) : pointerMove(config, __spreadProps(__spreadValues({}, action), { target, coords })));
2465
- if (typeof config.delay === "number") {
2466
- if (i < actions.length - 1) {
2467
- await wait(config.delay);
2468
- }
2487
+ if (i < actions.length - 1) {
2488
+ await wait(config);
2469
2489
  }
2470
2490
  }
2471
2491
  delete config.pointerState.activeClickCount;
package/dist/index.mjs CHANGED
@@ -525,7 +525,7 @@ function editContenteditable(config, element, inputRange, data, inputType) {
525
525
  function editInputElement(config, element, inputRange, data, inputType) {
526
526
  let dataToInsert = data;
527
527
  const spaceUntilMaxLength = getSpaceUntilMaxLength(element);
528
- if (spaceUntilMaxLength !== void 0) {
528
+ if (spaceUntilMaxLength !== void 0 && data.length > 0) {
529
529
  if (spaceUntilMaxLength > 0) {
530
530
  dataToInsert = data.substring(0, spaceUntilMaxLength);
531
531
  } else {
@@ -549,6 +549,7 @@ function editInputElement(config, element, inputRange, data, inputType) {
549
549
  if (isValidDateOrTimeValue(element, newValue)) {
550
550
  commitInput(config, element, newOffset, {});
551
551
  dispatchUIEvent(config, element, "change");
552
+ clearInitialValue(element);
552
553
  }
553
554
  } else {
554
555
  commitInput(config, element, newOffset, {
@@ -1233,8 +1234,15 @@ function getLevelRef(config, level) {
1233
1234
  }
1234
1235
 
1235
1236
  // src/utils/misc/wait.ts
1236
- function wait(time) {
1237
- return new Promise((resolve) => setTimeout(() => resolve(), time));
1237
+ function wait(config) {
1238
+ const delay = config.delay;
1239
+ if (typeof delay !== "number") {
1240
+ return;
1241
+ }
1242
+ return Promise.all([
1243
+ new Promise((resolve) => setTimeout(() => resolve(), delay)),
1244
+ config.advanceTimers(delay)
1245
+ ]);
1238
1246
  }
1239
1247
 
1240
1248
  // src/keyboard/keyMap.ts
@@ -1322,7 +1330,8 @@ var defaultOptionsDirect = {
1322
1330
  skipAutoClose: false,
1323
1331
  skipClick: false,
1324
1332
  skipHover: false,
1325
- writeToClipboard: false
1333
+ writeToClipboard: false,
1334
+ advanceTimers: () => Promise.resolve()
1326
1335
  };
1327
1336
  var defaultOptionsSetup = __spreadProps(__spreadValues({}, defaultOptionsDirect), {
1328
1337
  writeToClipboard: true
@@ -1694,13 +1703,18 @@ function prepareInterceptor(element, propName, interceptorImpl) {
1694
1703
  return;
1695
1704
  }
1696
1705
  function intercept(...args) {
1697
- const { applyNative = true, realArgs } = interceptorImpl.call(this, ...args);
1706
+ const {
1707
+ applyNative = true,
1708
+ realArgs,
1709
+ then
1710
+ } = interceptorImpl.call(this, ...args);
1698
1711
  const realFunc = (!applyNative && objectDescriptor || prototypeDescriptor)[target];
1699
1712
  if (target === "set") {
1700
1713
  realFunc.call(this, realArgs);
1701
1714
  } else {
1702
1715
  realFunc.call(this, ...realArgs);
1703
1716
  }
1717
+ then == null ? void 0 : then();
1704
1718
  }
1705
1719
  ;
1706
1720
  intercept[Interceptor] = Interceptor;
@@ -1729,6 +1743,13 @@ function prepareSelectionInterceptor(element) {
1729
1743
  this[UISelection] = void 0;
1730
1744
  return { realArgs: v };
1731
1745
  });
1746
+ prepareInterceptor(element, "select", function interceptorImpl() {
1747
+ this[UISelection] = {
1748
+ anchorOffset: 0,
1749
+ focusOffset: getUIValue(element).length
1750
+ };
1751
+ return { realArgs: [] };
1752
+ });
1732
1753
  }
1733
1754
  function setUISelection(element, {
1734
1755
  focusOffset: focusOffsetParam,
@@ -1775,12 +1796,11 @@ function valueInterceptor(v) {
1775
1796
  if (isUI) {
1776
1797
  this[UIValue] = String(v);
1777
1798
  setPreviousValue(this, String(this.value));
1778
- } else {
1779
- trackOrSetValue(this, String(v));
1780
1799
  }
1781
1800
  return {
1782
1801
  applyNative: !!isUI,
1783
- realArgs: sanitizeValue(this, v)
1802
+ realArgs: sanitizeValue(this, v),
1803
+ then: isUI ? void 0 : () => trackOrSetValue(this, String(v))
1784
1804
  };
1785
1805
  }
1786
1806
  function sanitizeValue(element, v) {
@@ -1793,6 +1813,9 @@ function prepareValueInterceptor(element) {
1793
1813
  prepareInterceptor(element, "value", valueInterceptor);
1794
1814
  }
1795
1815
  function setUIValue(element, value) {
1816
+ if (element[InitialValue] === void 0) {
1817
+ element[InitialValue] = element.value;
1818
+ }
1796
1819
  element.value = {
1797
1820
  [UIValue]: UIValue,
1798
1821
  toString: () => value
@@ -1801,8 +1824,8 @@ function setUIValue(element, value) {
1801
1824
  function getUIValue(element) {
1802
1825
  return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1803
1826
  }
1804
- function setInitialValue(element) {
1805
- element[InitialValue] = element.value;
1827
+ function clearInitialValue(element) {
1828
+ element[InitialValue] = void 0;
1806
1829
  }
1807
1830
  function getInitialValue(element) {
1808
1831
  return element[InitialValue];
@@ -1825,7 +1848,6 @@ function trackOrSetValue(element, v) {
1825
1848
  }
1826
1849
  function setCleanValue(element, v) {
1827
1850
  element[UIValue] = void 0;
1828
- element[InitialValue] = v;
1829
1851
  setUISelection(element, { focusOffset: v.length });
1830
1852
  }
1831
1853
  function endTrackValue(element) {
@@ -1858,8 +1880,11 @@ function prepareDocument(document2) {
1858
1880
  document2.addEventListener("blur", (e) => {
1859
1881
  const el = e.target;
1860
1882
  const initialValue = getInitialValue(el);
1861
- if (typeof initialValue === "string" && el.value !== initialValue) {
1862
- dispatchUIEvent({}, el, "change");
1883
+ if (initialValue !== void 0) {
1884
+ if (el.value !== initialValue) {
1885
+ dispatchUIEvent({}, el, "change");
1886
+ }
1887
+ clearInitialValue(el);
1863
1888
  }
1864
1889
  }, {
1865
1890
  capture: true,
@@ -1868,9 +1893,6 @@ function prepareDocument(document2) {
1868
1893
  document2[isPrepared] = isPrepared;
1869
1894
  }
1870
1895
  function prepareElement(el) {
1871
- if ("value" in el) {
1872
- setInitialValue(el);
1873
- }
1874
1896
  if (el[isPrepared]) {
1875
1897
  return;
1876
1898
  }
@@ -1941,15 +1963,15 @@ function keyboardAction(config, actions) {
1941
1963
  return __async(this, null, function* () {
1942
1964
  for (let i = 0; i < actions.length; i++) {
1943
1965
  yield keyboardKeyAction(config, actions[i]);
1944
- if (typeof config.delay === "number" && i < actions.length - 1) {
1945
- yield wait(config.delay);
1966
+ if (i < actions.length - 1) {
1967
+ yield wait(config);
1946
1968
  }
1947
1969
  }
1948
1970
  });
1949
1971
  }
1950
1972
  function keyboardKeyAction(_0, _1) {
1951
1973
  return __async(this, arguments, function* (config, { keyDef, releasePrevious, releaseSelf, repeat }) {
1952
- const { document: document2, keyboardState, delay } = config;
1974
+ const { document: document2, keyboardState } = config;
1953
1975
  const getCurrentElement = () => getActive(document2);
1954
1976
  const pressed = keyboardState.pressed.find((p) => p.keyDef === keyDef);
1955
1977
  if (pressed) {
@@ -1962,8 +1984,8 @@ function keyboardKeyAction(_0, _1) {
1962
1984
  if (unpreventedDefault && hasKeyPress(keyDef, config)) {
1963
1985
  yield keypress(keyDef, getCurrentElement, config);
1964
1986
  }
1965
- if (typeof delay === "number" && i < repeat) {
1966
- yield wait(delay);
1987
+ if (i < repeat) {
1988
+ yield wait(config);
1967
1989
  }
1968
1990
  }
1969
1991
  if (releaseSelf) {
@@ -2434,7 +2456,6 @@ function mousedownDefaultBehavior({
2434
2456
  node,
2435
2457
  offset
2436
2458
  }) {
2437
- focus(target);
2438
2459
  if (!targetIsDisabled) {
2439
2460
  const hasValue = isElementType(target, ["input", "textarea"]);
2440
2461
  const text = String(hasValue ? getUIValue(target) : target.textContent);
@@ -2469,6 +2490,7 @@ function mousedownDefaultBehavior({
2469
2490
  selection.addRange(range.cloneRange());
2470
2491
  }
2471
2492
  }
2493
+ focus(target);
2472
2494
  }
2473
2495
  function getTextRange(text, pos, clickCount) {
2474
2496
  if (clickCount % 3 === 1 || text.length === 0) {
@@ -2497,10 +2519,8 @@ function pointerAction(config, actions) {
2497
2519
  const target = (_a3 = action.target) != null ? _a3 : getPrevTarget(pointerName, config.pointerState);
2498
2520
  const coords = (_b = action.coords) != null ? _b : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : void 0;
2499
2521
  yield "keyDef" in action ? pointerPress(config, __spreadProps(__spreadValues({}, action), { target, coords })) : pointerMove(config, __spreadProps(__spreadValues({}, action), { target, coords }));
2500
- if (typeof config.delay === "number") {
2501
- if (i < actions.length - 1) {
2502
- yield wait(config.delay);
2503
- }
2522
+ if (i < actions.length - 1) {
2523
+ yield wait(config);
2504
2524
  }
2505
2525
  }
2506
2526
  delete config.pointerState.activeClickCount;
@@ -5,6 +5,6 @@ declare global {
5
5
  }
6
6
  }
7
7
  export declare function prepareDocument(document: Document): void;
8
- export { getUIValue, setUIValue, startTrackValue, endTrackValue } from './value';
8
+ export { getUIValue, setUIValue, startTrackValue, endTrackValue, clearInitialValue, } from './value';
9
9
  export { getUISelection, setUISelection } from './selection';
10
10
  export type { UISelectionRange } from './selection';
@@ -11,5 +11,6 @@ export declare function prepareInterceptor<ElementType extends Node, PropName ex
11
11
  */
12
12
  applyNative?: boolean;
13
13
  realArgs?: ImplReturn<ElementType[PropName]>;
14
+ then?: () => void;
14
15
  }): void;
15
16
  export {};
@@ -15,7 +15,7 @@ declare global {
15
15
  export declare function prepareValueInterceptor(element: HTMLInputElement): void;
16
16
  export declare function setUIValue(element: HTMLInputElement | HTMLTextAreaElement, value: string): void;
17
17
  export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaElement): string;
18
- export declare function setInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
18
+ export declare function clearInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
19
19
  export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
20
20
  export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
21
21
  /**
@@ -106,6 +106,12 @@ export interface Options {
106
106
  * Defaults to `true` when calling the APIs per `setup`.
107
107
  */
108
108
  writeToClipboard?: boolean;
109
+ /**
110
+ * A function to be called internally to advance your fake timers (if applicable)
111
+ *
112
+ * @example jest.advanceTimersByTime
113
+ */
114
+ advanceTimers?: ((delay: number) => Promise<void>) | ((delay: number) => void);
109
115
  }
110
116
  /**
111
117
  * Default options applied when API is called per `userEvent.anyApi()`
@@ -1 +1,2 @@
1
- export declare function wait(time?: number): Promise<void>;
1
+ import { Config } from '../../setup';
2
+ export declare function wait(config: Config): Promise<[void, void]> | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.0.2",
3
+ "version": "14.1.0",
4
4
  "description": "Fire events the same way the user does",
5
5
  "keywords": [
6
6
  "react-testing-library",