@testing-library/user-event 14.2.5 → 14.2.6

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
@@ -548,15 +548,8 @@ function calculateNewValue(inputData, node, {
548
548
  };
549
549
  }
550
550
  function commitInput(config, element, newOffset, inputInit) {
551
- startTrackValue(element);
552
551
  dispatchUIEvent(config, element, "input", inputInit);
553
- if (endTrackValue(element)) {
554
- setSelection({
555
- focusNode: element,
556
- anchorOffset: newOffset,
557
- focusOffset: newOffset
558
- });
559
- }
552
+ commitValueAfterInput(element, newOffset);
560
553
  }
561
554
  function isValidNumberInput(value) {
562
555
  var _a, _b;
@@ -1327,9 +1320,9 @@ var defaultOptionsSetup = {
1327
1320
  };
1328
1321
 
1329
1322
  // src/utils/pointer/cssPointerEvents.ts
1330
- function hasPointerEvents(element) {
1323
+ function hasPointerEvents(config, element) {
1331
1324
  var _a;
1332
- return ((_a = closestPointerEventsDeclaration(element)) == null ? void 0 : _a.pointerEvents) !== "none";
1325
+ return ((_a = checkPointerEvents(config, element)) == null ? void 0 : _a.pointerEvents) !== "none";
1333
1326
  }
1334
1327
  function closestPointerEventsDeclaration(element) {
1335
1328
  const window = getWindow(element);
@@ -1343,18 +1336,22 @@ function closestPointerEventsDeclaration(element) {
1343
1336
  return void 0;
1344
1337
  }
1345
1338
  var PointerEventsCheck = Symbol("Last check for pointer-events");
1346
- function assertPointerEvents(config, element) {
1339
+ function checkPointerEvents(config, element) {
1347
1340
  const lastCheck = element[PointerEventsCheck];
1348
1341
  const needsCheck = config.pointerEventsCheck !== 0 /* Never */ && (!lastCheck || hasBitFlag(config.pointerEventsCheck, 2 /* EachApiCall */) && lastCheck[1 /* Call */] !== getLevelRef(config, 1 /* Call */) || hasBitFlag(config.pointerEventsCheck, 4 /* EachTrigger */) && lastCheck[2 /* Trigger */] !== getLevelRef(config, 2 /* Trigger */));
1349
1342
  if (!needsCheck) {
1350
- return;
1343
+ return lastCheck == null ? void 0 : lastCheck.result;
1351
1344
  }
1352
1345
  const declaration = closestPointerEventsDeclaration(element);
1353
1346
  element[PointerEventsCheck] = {
1354
1347
  [1 /* Call */]: getLevelRef(config, 1 /* Call */),
1355
1348
  [2 /* Trigger */]: getLevelRef(config, 2 /* Trigger */),
1356
- result: (declaration == null ? void 0 : declaration.pointerEvents) !== "none"
1349
+ result: declaration
1357
1350
  };
1351
+ return declaration;
1352
+ }
1353
+ function assertPointerEvents(config, element) {
1354
+ const declaration = checkPointerEvents(config, element);
1358
1355
  if ((declaration == null ? void 0 : declaration.pointerEvents) === "none") {
1359
1356
  throw new Error([
1360
1357
  `Unable to perform pointer interaction as the element ${declaration.tree.length > 1 ? "inherits" : "has"} \`pointer-events: none\`:`,
@@ -1831,6 +1828,9 @@ function getUISelection(element) {
1831
1828
  endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1832
1829
  };
1833
1830
  }
1831
+ function hasUISelection(element) {
1832
+ return !!element[UISelection];
1833
+ }
1834
1834
  function setUISelectionClean(element) {
1835
1835
  element[UISelection] = void 0;
1836
1836
  }
@@ -1843,7 +1843,7 @@ function valueInterceptor(v) {
1843
1843
  const isUI = typeof v === "object" && v[UIValue];
1844
1844
  if (isUI) {
1845
1845
  this[UIValue] = String(v);
1846
- setPreviousValue(this, String(this.value));
1846
+ startTrackValue(this);
1847
1847
  }
1848
1848
  return {
1849
1849
  applyNative: !!isUI,
@@ -1881,36 +1881,42 @@ function clearInitialValue(element) {
1881
1881
  function getInitialValue(element) {
1882
1882
  return element[InitialValue];
1883
1883
  }
1884
- function setPreviousValue(element, v) {
1885
- element[TrackChanges] = { ...element[TrackChanges], previousValue: v };
1884
+ function isReact17Element(element) {
1885
+ return Object.getOwnPropertyNames(element).some((k) => k.startsWith("__react")) && getWindow(element).REACT_VERSION === 17;
1886
1886
  }
1887
1887
  function startTrackValue(element) {
1888
+ if (!isReact17Element(element)) {
1889
+ return;
1890
+ }
1888
1891
  element[TrackChanges] = {
1889
- ...element[TrackChanges],
1890
- nextValue: String(element.value),
1892
+ previousValue: String(element.value),
1891
1893
  tracked: []
1892
1894
  };
1893
1895
  }
1894
1896
  function trackOrSetValue(element, v) {
1895
- var _a, _b, _c;
1897
+ var _a, _b;
1896
1898
  (_b = (_a = element[TrackChanges]) == null ? void 0 : _a.tracked) == null ? void 0 : _b.push(v);
1897
- if (!((_c = element[TrackChanges]) == null ? void 0 : _c.tracked)) {
1898
- setCleanValue(element, v);
1899
+ if (!element[TrackChanges]) {
1900
+ setUIValueClean(element);
1901
+ setUISelection(element, { focusOffset: v.length });
1899
1902
  }
1900
1903
  }
1901
- function setCleanValue(element, v) {
1902
- element[UIValue] = void 0;
1903
- setUISelection(element, { focusOffset: v.length });
1904
- }
1905
- function endTrackValue(element) {
1906
- var _a, _b;
1904
+ function commitValueAfterInput(element, cursorOffset) {
1905
+ var _a;
1907
1906
  const changes = element[TrackChanges];
1908
1907
  element[TrackChanges] = void 0;
1909
- 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;
1910
- if (((_b = changes == null ? void 0 : changes.tracked) == null ? void 0 : _b.length) && !isJustReactStateUpdate) {
1911
- setCleanValue(element, changes.tracked[changes.tracked.length - 1]);
1908
+ if (!((_a = changes == null ? void 0 : changes.tracked) == null ? void 0 : _a.length)) {
1909
+ return;
1910
+ }
1911
+ const isJustReactStateUpdate = changes.tracked.length === 2 && changes.tracked[0] === changes.previousValue && changes.tracked[1] === element.value;
1912
+ if (!isJustReactStateUpdate) {
1913
+ setUIValueClean(element);
1914
+ }
1915
+ if (hasUISelection(element)) {
1916
+ setUISelection(element, {
1917
+ focusOffset: isJustReactStateUpdate ? cursorOffset : element.value.length
1918
+ });
1912
1919
  }
1913
- return isJustReactStateUpdate;
1914
1920
  }
1915
1921
 
1916
1922
  // src/document/setRangeText.ts
@@ -2265,10 +2271,11 @@ async function pointerMove(config, { pointerName = "mouse", target, coords, node
2265
2271
  } = pointerState.position[pointerName];
2266
2272
  if (prevTarget && prevTarget !== target) {
2267
2273
  setLevelRef(config, 2 /* Trigger */);
2268
- assertPointerEvents(config, prevTarget);
2269
- fireMove(prevTarget, prevCoords);
2270
- if (!isDescendantOrSelf(target, prevTarget)) {
2271
- fireLeave(prevTarget, prevCoords);
2274
+ if (hasPointerEvents(config, prevTarget)) {
2275
+ fireMove(prevTarget, prevCoords);
2276
+ if (!isDescendantOrSelf(target, prevTarget)) {
2277
+ fireLeave(prevTarget, prevCoords);
2278
+ }
2272
2279
  }
2273
2280
  }
2274
2281
  setLevelRef(config, 2 /* Trigger */);
@@ -2672,6 +2679,7 @@ async function hover(element) {
2672
2679
  return this.pointer({ target: element });
2673
2680
  }
2674
2681
  async function unhover(element) {
2682
+ assertPointerEvents(this[Config], this[Config].pointerState.position.mouse.target);
2675
2683
  return this.pointer({ target: element.ownerDocument.body });
2676
2684
  }
2677
2685
 
@@ -2790,7 +2798,7 @@ async function selectOptionsBase(newValue, select, values) {
2790
2798
  if (isElementType(select, "select")) {
2791
2799
  if (select.multiple) {
2792
2800
  for (const option of selectedOptions) {
2793
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(option);
2801
+ const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], option);
2794
2802
  if (withPointerEvents) {
2795
2803
  this.dispatchUIEvent(option, "pointerover");
2796
2804
  this.dispatchUIEvent(select, "pointerenter");
@@ -2813,7 +2821,7 @@ async function selectOptionsBase(newValue, select, values) {
2813
2821
  await wait(this[Config]);
2814
2822
  }
2815
2823
  } else if (selectedOptions.length === 1) {
2816
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(select);
2824
+ const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], select);
2817
2825
  if (withPointerEvents) {
2818
2826
  await this.click(select);
2819
2827
  } else {
package/dist/index.mjs CHANGED
@@ -579,15 +579,8 @@ function calculateNewValue(inputData, node, {
579
579
  };
580
580
  }
581
581
  function commitInput(config, element, newOffset, inputInit) {
582
- startTrackValue(element);
583
582
  dispatchUIEvent(config, element, "input", inputInit);
584
- if (endTrackValue(element)) {
585
- setSelection({
586
- focusNode: element,
587
- anchorOffset: newOffset,
588
- focusOffset: newOffset
589
- });
590
- }
583
+ commitValueAfterInput(element, newOffset);
591
584
  }
592
585
  function isValidNumberInput(value) {
593
586
  var _a, _b;
@@ -1341,9 +1334,9 @@ var defaultOptionsSetup = __spreadProps(__spreadValues({}, defaultOptionsDirect)
1341
1334
  });
1342
1335
 
1343
1336
  // src/utils/pointer/cssPointerEvents.ts
1344
- function hasPointerEvents(element) {
1337
+ function hasPointerEvents(config, element) {
1345
1338
  var _a;
1346
- return ((_a = closestPointerEventsDeclaration(element)) == null ? void 0 : _a.pointerEvents) !== "none";
1339
+ return ((_a = checkPointerEvents(config, element)) == null ? void 0 : _a.pointerEvents) !== "none";
1347
1340
  }
1348
1341
  function closestPointerEventsDeclaration(element) {
1349
1342
  const window = getWindow(element);
@@ -1357,18 +1350,22 @@ function closestPointerEventsDeclaration(element) {
1357
1350
  return void 0;
1358
1351
  }
1359
1352
  var PointerEventsCheck = Symbol("Last check for pointer-events");
1360
- function assertPointerEvents(config, element) {
1353
+ function checkPointerEvents(config, element) {
1361
1354
  const lastCheck = element[PointerEventsCheck];
1362
1355
  const needsCheck = config.pointerEventsCheck !== 0 /* Never */ && (!lastCheck || hasBitFlag(config.pointerEventsCheck, 2 /* EachApiCall */) && lastCheck[1 /* Call */] !== getLevelRef(config, 1 /* Call */) || hasBitFlag(config.pointerEventsCheck, 4 /* EachTrigger */) && lastCheck[2 /* Trigger */] !== getLevelRef(config, 2 /* Trigger */));
1363
1356
  if (!needsCheck) {
1364
- return;
1357
+ return lastCheck == null ? void 0 : lastCheck.result;
1365
1358
  }
1366
1359
  const declaration = closestPointerEventsDeclaration(element);
1367
1360
  element[PointerEventsCheck] = {
1368
1361
  [1 /* Call */]: getLevelRef(config, 1 /* Call */),
1369
1362
  [2 /* Trigger */]: getLevelRef(config, 2 /* Trigger */),
1370
- result: (declaration == null ? void 0 : declaration.pointerEvents) !== "none"
1363
+ result: declaration
1371
1364
  };
1365
+ return declaration;
1366
+ }
1367
+ function assertPointerEvents(config, element) {
1368
+ const declaration = checkPointerEvents(config, element);
1372
1369
  if ((declaration == null ? void 0 : declaration.pointerEvents) === "none") {
1373
1370
  throw new Error([
1374
1371
  `Unable to perform pointer interaction as the element ${declaration.tree.length > 1 ? "inherits" : "has"} \`pointer-events: none\`:`,
@@ -1839,6 +1836,9 @@ function getUISelection(element) {
1839
1836
  endOffset: Math.max(sel.anchorOffset, sel.focusOffset)
1840
1837
  });
1841
1838
  }
1839
+ function hasUISelection(element) {
1840
+ return !!element[UISelection];
1841
+ }
1842
1842
  function setUISelectionClean(element) {
1843
1843
  element[UISelection] = void 0;
1844
1844
  }
@@ -1851,7 +1851,7 @@ function valueInterceptor(v) {
1851
1851
  const isUI = typeof v === "object" && v[UIValue];
1852
1852
  if (isUI) {
1853
1853
  this[UIValue] = String(v);
1854
- setPreviousValue(this, String(this.value));
1854
+ startTrackValue(this);
1855
1855
  }
1856
1856
  return {
1857
1857
  applyNative: !!isUI,
@@ -1889,35 +1889,42 @@ function clearInitialValue(element) {
1889
1889
  function getInitialValue(element) {
1890
1890
  return element[InitialValue];
1891
1891
  }
1892
- function setPreviousValue(element, v) {
1893
- element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), { previousValue: v });
1892
+ function isReact17Element(element) {
1893
+ return Object.getOwnPropertyNames(element).some((k) => k.startsWith("__react")) && getWindow(element).REACT_VERSION === 17;
1894
1894
  }
1895
1895
  function startTrackValue(element) {
1896
- element[TrackChanges] = __spreadProps(__spreadValues({}, element[TrackChanges]), {
1897
- nextValue: String(element.value),
1896
+ if (!isReact17Element(element)) {
1897
+ return;
1898
+ }
1899
+ element[TrackChanges] = {
1900
+ previousValue: String(element.value),
1898
1901
  tracked: []
1899
- });
1902
+ };
1900
1903
  }
1901
1904
  function trackOrSetValue(element, v) {
1902
- var _a, _b, _c;
1905
+ var _a, _b;
1903
1906
  (_b = (_a = element[TrackChanges]) == null ? void 0 : _a.tracked) == null ? void 0 : _b.push(v);
1904
- if (!((_c = element[TrackChanges]) == null ? void 0 : _c.tracked)) {
1905
- setCleanValue(element, v);
1907
+ if (!element[TrackChanges]) {
1908
+ setUIValueClean(element);
1909
+ setUISelection(element, { focusOffset: v.length });
1906
1910
  }
1907
1911
  }
1908
- function setCleanValue(element, v) {
1909
- element[UIValue] = void 0;
1910
- setUISelection(element, { focusOffset: v.length });
1911
- }
1912
- function endTrackValue(element) {
1913
- var _a, _b;
1912
+ function commitValueAfterInput(element, cursorOffset) {
1913
+ var _a;
1914
1914
  const changes = element[TrackChanges];
1915
1915
  element[TrackChanges] = void 0;
1916
- 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;
1917
- if (((_b = changes == null ? void 0 : changes.tracked) == null ? void 0 : _b.length) && !isJustReactStateUpdate) {
1918
- setCleanValue(element, changes.tracked[changes.tracked.length - 1]);
1916
+ if (!((_a = changes == null ? void 0 : changes.tracked) == null ? void 0 : _a.length)) {
1917
+ return;
1918
+ }
1919
+ const isJustReactStateUpdate = changes.tracked.length === 2 && changes.tracked[0] === changes.previousValue && changes.tracked[1] === element.value;
1920
+ if (!isJustReactStateUpdate) {
1921
+ setUIValueClean(element);
1922
+ }
1923
+ if (hasUISelection(element)) {
1924
+ setUISelection(element, {
1925
+ focusOffset: isJustReactStateUpdate ? cursorOffset : element.value.length
1926
+ });
1919
1927
  }
1920
- return isJustReactStateUpdate;
1921
1928
  }
1922
1929
 
1923
1930
  // src/document/setRangeText.ts
@@ -2284,10 +2291,11 @@ function pointerMove(_0, _1) {
2284
2291
  } = pointerState.position[pointerName];
2285
2292
  if (prevTarget && prevTarget !== target) {
2286
2293
  setLevelRef(config, 2 /* Trigger */);
2287
- assertPointerEvents(config, prevTarget);
2288
- fireMove(prevTarget, prevCoords);
2289
- if (!isDescendantOrSelf(target, prevTarget)) {
2290
- fireLeave(prevTarget, prevCoords);
2294
+ if (hasPointerEvents(config, prevTarget)) {
2295
+ fireMove(prevTarget, prevCoords);
2296
+ if (!isDescendantOrSelf(target, prevTarget)) {
2297
+ fireLeave(prevTarget, prevCoords);
2298
+ }
2291
2299
  }
2292
2300
  }
2293
2301
  setLevelRef(config, 2 /* Trigger */);
@@ -2701,6 +2709,7 @@ function hover(element) {
2701
2709
  }
2702
2710
  function unhover(element) {
2703
2711
  return __async(this, null, function* () {
2712
+ assertPointerEvents(this[Config], this[Config].pointerState.position.mouse.target);
2704
2713
  return this.pointer({ target: element.ownerDocument.body });
2705
2714
  });
2706
2715
  }
@@ -2835,7 +2844,7 @@ function selectOptionsBase(newValue, select, values) {
2835
2844
  if (isElementType(select, "select")) {
2836
2845
  if (select.multiple) {
2837
2846
  for (const option of selectedOptions) {
2838
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(option);
2847
+ const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], option);
2839
2848
  if (withPointerEvents) {
2840
2849
  this.dispatchUIEvent(option, "pointerover");
2841
2850
  this.dispatchUIEvent(select, "pointerenter");
@@ -2858,7 +2867,7 @@ function selectOptionsBase(newValue, select, values) {
2858
2867
  yield wait(this[Config]);
2859
2868
  }
2860
2869
  } else if (selectedOptions.length === 1) {
2861
- const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(select);
2870
+ const withPointerEvents = this[Config].pointerEventsCheck === 0 ? true : hasPointerEvents(this[Config], select);
2862
2871
  if (withPointerEvents) {
2863
2872
  yield this.click(select);
2864
2873
  } else {
@@ -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, clearInitialValue, } from './value';
8
+ export { getUIValue, setUIValue, commitValueAfterInput, clearInitialValue, } from './value';
9
9
  export { getUISelection, setUISelection } from './selection';
10
10
  export type { UISelectionRange } from './selection';
@@ -23,6 +23,7 @@ export declare function getUISelection(element: HTMLInputElement | HTMLTextAreaE
23
23
  anchorOffset: number;
24
24
  focusOffset: number;
25
25
  };
26
+ export declare function hasUISelection(element: HTMLInputElement | HTMLTextAreaElement): boolean;
26
27
  /** Flag the IDL selection as clean. This does not change the selection. */
27
28
  export declare function setUISelectionClean(element: HTMLInputElement | HTMLTextAreaElement): void;
28
29
  export {};
@@ -2,6 +2,9 @@ declare const UIValue: unique symbol;
2
2
  declare const InitialValue: unique symbol;
3
3
  declare const TrackChanges: unique symbol;
4
4
  declare global {
5
+ interface Window {
6
+ REACT_VERSION?: number;
7
+ }
5
8
  interface Element {
6
9
  [UIValue]?: string;
7
10
  [InitialValue]?: string;
@@ -19,9 +22,5 @@ export declare function getUIValue(element: HTMLInputElement | HTMLTextAreaEleme
19
22
  export declare function setUIValueClean(element: HTMLInputElement | HTMLTextAreaElement): void;
20
23
  export declare function clearInitialValue(element: HTMLInputElement | HTMLTextAreaElement): void;
21
24
  export declare function getInitialValue(element: HTMLInputElement | HTMLTextAreaElement): string | undefined;
22
- export declare function startTrackValue(element: HTMLInputElement | HTMLTextAreaElement): void;
23
- /**
24
- * @returns `true` if we recognize a React state reset and update
25
- */
26
- export declare function endTrackValue(element: HTMLInputElement | HTMLTextAreaElement): boolean;
25
+ export declare function commitValueAfterInput(element: HTMLInputElement | HTMLTextAreaElement, cursorOffset: number): void;
27
26
  export {};
@@ -1,13 +1,17 @@
1
1
  import { Config } from '../../setup';
2
2
  import { ApiLevel } from '..';
3
- export declare function hasPointerEvents(element: Element): boolean;
3
+ export declare function hasPointerEvents(config: Config, element: Element): boolean;
4
+ declare function closestPointerEventsDeclaration(element: Element): {
5
+ pointerEvents: string;
6
+ tree: Element[];
7
+ } | undefined;
4
8
  declare const PointerEventsCheck: unique symbol;
5
9
  declare global {
6
10
  interface Element {
7
11
  [PointerEventsCheck]?: {
8
12
  [k in ApiLevel]?: object;
9
13
  } & {
10
- result: boolean;
14
+ result: ReturnType<typeof closestPointerEventsDeclaration>;
11
15
  };
12
16
  }
13
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.2.5",
3
+ "version": "14.2.6",
4
4
  "description": "Fire events the same way the user does",
5
5
  "keywords": [
6
6
  "react-testing-library",