@testing-library/user-event 14.0.4 → 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;
@@ -1766,12 +1780,11 @@ function valueInterceptor(v) {
1766
1780
  if (isUI) {
1767
1781
  this[UIValue] = String(v);
1768
1782
  setPreviousValue(this, String(this.value));
1769
- } else {
1770
- trackOrSetValue(this, String(v));
1771
1783
  }
1772
1784
  return {
1773
1785
  applyNative: !!isUI,
1774
- realArgs: sanitizeValue(this, v)
1786
+ realArgs: sanitizeValue(this, v),
1787
+ then: isUI ? void 0 : () => trackOrSetValue(this, String(v))
1775
1788
  };
1776
1789
  }
1777
1790
  function sanitizeValue(element, v) {
@@ -1784,6 +1797,9 @@ function prepareValueInterceptor(element) {
1784
1797
  prepareInterceptor(element, "value", valueInterceptor);
1785
1798
  }
1786
1799
  function setUIValue(element, value) {
1800
+ if (element[InitialValue] === void 0) {
1801
+ element[InitialValue] = element.value;
1802
+ }
1787
1803
  element.value = {
1788
1804
  [UIValue]: UIValue,
1789
1805
  toString: () => value
@@ -1792,8 +1808,8 @@ function setUIValue(element, value) {
1792
1808
  function getUIValue(element) {
1793
1809
  return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1794
1810
  }
1795
- function setInitialValue(element) {
1796
- element[InitialValue] = element.value;
1811
+ function clearInitialValue(element) {
1812
+ element[InitialValue] = void 0;
1797
1813
  }
1798
1814
  function getInitialValue(element) {
1799
1815
  return element[InitialValue];
@@ -1816,7 +1832,6 @@ function trackOrSetValue(element, v) {
1816
1832
  }
1817
1833
  function setCleanValue(element, v) {
1818
1834
  element[UIValue] = void 0;
1819
- element[InitialValue] = v;
1820
1835
  setUISelection(element, { focusOffset: v.length });
1821
1836
  }
1822
1837
  function endTrackValue(element) {
@@ -1849,8 +1864,11 @@ function prepareDocument(document2) {
1849
1864
  document2.addEventListener("blur", (e) => {
1850
1865
  const el = e.target;
1851
1866
  const initialValue = getInitialValue(el);
1852
- if (typeof initialValue === "string" && el.value !== initialValue) {
1853
- dispatchUIEvent({}, el, "change");
1867
+ if (initialValue !== void 0) {
1868
+ if (el.value !== initialValue) {
1869
+ dispatchUIEvent({}, el, "change");
1870
+ }
1871
+ clearInitialValue(el);
1854
1872
  }
1855
1873
  }, {
1856
1874
  capture: true,
@@ -1859,9 +1877,6 @@ function prepareDocument(document2) {
1859
1877
  document2[isPrepared] = isPrepared;
1860
1878
  }
1861
1879
  function prepareElement(el) {
1862
- if ("value" in el) {
1863
- setInitialValue(el);
1864
- }
1865
1880
  if (el[isPrepared]) {
1866
1881
  return;
1867
1882
  }
@@ -1931,13 +1946,13 @@ function postKeyupBehavior(config, { key }, element) {
1931
1946
  async function keyboardAction(config, actions) {
1932
1947
  for (let i = 0; i < actions.length; i++) {
1933
1948
  await keyboardKeyAction(config, actions[i]);
1934
- if (typeof config.delay === "number" && i < actions.length - 1) {
1935
- await wait(config.delay);
1949
+ if (i < actions.length - 1) {
1950
+ await wait(config);
1936
1951
  }
1937
1952
  }
1938
1953
  }
1939
1954
  async function keyboardKeyAction(config, { keyDef, releasePrevious, releaseSelf, repeat }) {
1940
- const { document: document2, keyboardState, delay } = config;
1955
+ const { document: document2, keyboardState } = config;
1941
1956
  const getCurrentElement = () => getActive(document2);
1942
1957
  const pressed = keyboardState.pressed.find((p) => p.keyDef === keyDef);
1943
1958
  if (pressed) {
@@ -1950,8 +1965,8 @@ async function keyboardKeyAction(config, { keyDef, releasePrevious, releaseSelf,
1950
1965
  if (unpreventedDefault && hasKeyPress(keyDef, config)) {
1951
1966
  await keypress(keyDef, getCurrentElement, config);
1952
1967
  }
1953
- if (typeof delay === "number" && i < repeat) {
1954
- await wait(delay);
1968
+ if (i < repeat) {
1969
+ await wait(config);
1955
1970
  }
1956
1971
  }
1957
1972
  if (releaseSelf) {
@@ -2469,10 +2484,8 @@ async function pointerAction(config, actions) {
2469
2484
  const target = (_a3 = action.target) != null ? _a3 : getPrevTarget(pointerName, config.pointerState);
2470
2485
  const coords = (_b = action.coords) != null ? _b : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : void 0;
2471
2486
  await ("keyDef" in action ? pointerPress(config, __spreadProps(__spreadValues({}, action), { target, coords })) : pointerMove(config, __spreadProps(__spreadValues({}, action), { target, coords })));
2472
- if (typeof config.delay === "number") {
2473
- if (i < actions.length - 1) {
2474
- await wait(config.delay);
2475
- }
2487
+ if (i < actions.length - 1) {
2488
+ await wait(config);
2476
2489
  }
2477
2490
  }
2478
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;
@@ -1782,12 +1796,11 @@ function valueInterceptor(v) {
1782
1796
  if (isUI) {
1783
1797
  this[UIValue] = String(v);
1784
1798
  setPreviousValue(this, String(this.value));
1785
- } else {
1786
- trackOrSetValue(this, String(v));
1787
1799
  }
1788
1800
  return {
1789
1801
  applyNative: !!isUI,
1790
- realArgs: sanitizeValue(this, v)
1802
+ realArgs: sanitizeValue(this, v),
1803
+ then: isUI ? void 0 : () => trackOrSetValue(this, String(v))
1791
1804
  };
1792
1805
  }
1793
1806
  function sanitizeValue(element, v) {
@@ -1800,6 +1813,9 @@ function prepareValueInterceptor(element) {
1800
1813
  prepareInterceptor(element, "value", valueInterceptor);
1801
1814
  }
1802
1815
  function setUIValue(element, value) {
1816
+ if (element[InitialValue] === void 0) {
1817
+ element[InitialValue] = element.value;
1818
+ }
1803
1819
  element.value = {
1804
1820
  [UIValue]: UIValue,
1805
1821
  toString: () => value
@@ -1808,8 +1824,8 @@ function setUIValue(element, value) {
1808
1824
  function getUIValue(element) {
1809
1825
  return element[UIValue] === void 0 ? element.value : String(element[UIValue]);
1810
1826
  }
1811
- function setInitialValue(element) {
1812
- element[InitialValue] = element.value;
1827
+ function clearInitialValue(element) {
1828
+ element[InitialValue] = void 0;
1813
1829
  }
1814
1830
  function getInitialValue(element) {
1815
1831
  return element[InitialValue];
@@ -1832,7 +1848,6 @@ function trackOrSetValue(element, v) {
1832
1848
  }
1833
1849
  function setCleanValue(element, v) {
1834
1850
  element[UIValue] = void 0;
1835
- element[InitialValue] = v;
1836
1851
  setUISelection(element, { focusOffset: v.length });
1837
1852
  }
1838
1853
  function endTrackValue(element) {
@@ -1865,8 +1880,11 @@ function prepareDocument(document2) {
1865
1880
  document2.addEventListener("blur", (e) => {
1866
1881
  const el = e.target;
1867
1882
  const initialValue = getInitialValue(el);
1868
- if (typeof initialValue === "string" && el.value !== initialValue) {
1869
- dispatchUIEvent({}, el, "change");
1883
+ if (initialValue !== void 0) {
1884
+ if (el.value !== initialValue) {
1885
+ dispatchUIEvent({}, el, "change");
1886
+ }
1887
+ clearInitialValue(el);
1870
1888
  }
1871
1889
  }, {
1872
1890
  capture: true,
@@ -1875,9 +1893,6 @@ function prepareDocument(document2) {
1875
1893
  document2[isPrepared] = isPrepared;
1876
1894
  }
1877
1895
  function prepareElement(el) {
1878
- if ("value" in el) {
1879
- setInitialValue(el);
1880
- }
1881
1896
  if (el[isPrepared]) {
1882
1897
  return;
1883
1898
  }
@@ -1948,15 +1963,15 @@ function keyboardAction(config, actions) {
1948
1963
  return __async(this, null, function* () {
1949
1964
  for (let i = 0; i < actions.length; i++) {
1950
1965
  yield keyboardKeyAction(config, actions[i]);
1951
- if (typeof config.delay === "number" && i < actions.length - 1) {
1952
- yield wait(config.delay);
1966
+ if (i < actions.length - 1) {
1967
+ yield wait(config);
1953
1968
  }
1954
1969
  }
1955
1970
  });
1956
1971
  }
1957
1972
  function keyboardKeyAction(_0, _1) {
1958
1973
  return __async(this, arguments, function* (config, { keyDef, releasePrevious, releaseSelf, repeat }) {
1959
- const { document: document2, keyboardState, delay } = config;
1974
+ const { document: document2, keyboardState } = config;
1960
1975
  const getCurrentElement = () => getActive(document2);
1961
1976
  const pressed = keyboardState.pressed.find((p) => p.keyDef === keyDef);
1962
1977
  if (pressed) {
@@ -1969,8 +1984,8 @@ function keyboardKeyAction(_0, _1) {
1969
1984
  if (unpreventedDefault && hasKeyPress(keyDef, config)) {
1970
1985
  yield keypress(keyDef, getCurrentElement, config);
1971
1986
  }
1972
- if (typeof delay === "number" && i < repeat) {
1973
- yield wait(delay);
1987
+ if (i < repeat) {
1988
+ yield wait(config);
1974
1989
  }
1975
1990
  }
1976
1991
  if (releaseSelf) {
@@ -2504,10 +2519,8 @@ function pointerAction(config, actions) {
2504
2519
  const target = (_a3 = action.target) != null ? _a3 : getPrevTarget(pointerName, config.pointerState);
2505
2520
  const coords = (_b = action.coords) != null ? _b : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : void 0;
2506
2521
  yield "keyDef" in action ? pointerPress(config, __spreadProps(__spreadValues({}, action), { target, coords })) : pointerMove(config, __spreadProps(__spreadValues({}, action), { target, coords }));
2507
- if (typeof config.delay === "number") {
2508
- if (i < actions.length - 1) {
2509
- yield wait(config.delay);
2510
- }
2522
+ if (i < actions.length - 1) {
2523
+ yield wait(config);
2511
2524
  }
2512
2525
  }
2513
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.4",
3
+ "version": "14.1.0",
4
4
  "description": "Fire events the same way the user does",
5
5
  "keywords": [
6
6
  "react-testing-library",