@testing-library/user-event 11.3.1 β†’ 11.4.2

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/README.md CHANGED
@@ -23,7 +23,7 @@
23
23
  [![MIT License][license-badge]][license]
24
24
 
25
25
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
26
- [![All Contributors](https://img.shields.io/badge/all_contributors-41-orange.svg?style=flat-square)](#contributors-)
26
+ [![All Contributors](https://img.shields.io/badge/all_contributors-43-orange.svg?style=flat-square)](#contributors-)
27
27
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
28
28
  [![PRs Welcome][prs-badge]][prs]
29
29
  [![Code of Conduct][coc-badge]][coc]
@@ -50,7 +50,6 @@ change the state of the checkbox.
50
50
  <!-- START doctoc generated TOC please keep comment here to allow auto update -->
51
51
  <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
52
52
 
53
-
54
53
  - [Installation](#installation)
55
54
  - [API](#api)
56
55
  - [`click(element)`](#clickelement)
@@ -61,6 +60,8 @@ change the state of the checkbox.
61
60
  - [`selectOptions(element, values)`](#selectoptionselement-values)
62
61
  - [`toggleSelectOptions(element, values)`](#toggleselectoptionselement-values)
63
62
  - [`tab({shift, focusTrap})`](#tabshift-focustrap)
63
+ - [`async hover(element)`](#async-hoverelement)
64
+ - [`async unhover(element)`](#async-unhoverelement)
64
65
  - [Issues](#issues)
65
66
  - [πŸ› Bugs](#-bugs)
66
67
  - [πŸ’‘ Feature Requests](#-feature-requests)
@@ -74,13 +75,13 @@ change the state of the checkbox.
74
75
  With NPM:
75
76
 
76
77
  ```sh
77
- npm install @testing-library/user-event --save-dev
78
+ npm install @testing-library/user-event @testing-library/dom --save-dev
78
79
  ```
79
80
 
80
81
  With Yarn:
81
82
 
82
83
  ```sh
83
- yarn add @testing-library/user-event --dev
84
+ yarn add @testing-library/user-event @testing-library/dom --dev
84
85
  ```
85
86
 
86
87
  Now simply import it in your tests:
@@ -398,6 +399,37 @@ it('should cycle elements in document tab order', () => {
398
399
  })
399
400
  ```
400
401
 
402
+ ### `async hover(element)`
403
+
404
+ Hovers over `element`.
405
+
406
+ ```jsx
407
+ import React from 'react'
408
+ import {render, screen} from '@testing-library/react'
409
+ import userEvent from '@testing-library/user-event'
410
+ import Tooltip from '../tooltip'
411
+
412
+ test('hover', async () => {
413
+ const messageText = 'Hello'
414
+ render(
415
+ <Tooltip messageText={messageText}>
416
+ <TrashIcon aria-label="Delete" />
417
+ </Tooltip>,
418
+ )
419
+
420
+ await userEvent.hover(screen.getByLabelText(/delete/i))
421
+ expect(screen.getByText(messageText)).toBeInTheDocument()
422
+ await userEvent.unhover(screen.getByLabelText(/delete/i))
423
+ expect(screen.queryByText(messageText)).not.toBeInTheDocument()
424
+ })
425
+ ```
426
+
427
+ ### `async unhover(element)`
428
+
429
+ Unhovers out of `element`.
430
+
431
+ > See [above](#async-hoverelement) for an example
432
+
401
433
  ## Issues
402
434
 
403
435
  _Looking to contribute? Look for the [Good First Issue][good-first-issue]
@@ -476,6 +508,10 @@ Thanks goes to these people ([emoji key][emojis]):
476
508
  <td align="center"><a href="https://github.com/nanivijay"><img src="https://avatars0.githubusercontent.com/u/5945591?v=4" width="100px;" alt=""/><br /><sub><b>Vijay Kumar Otti</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Ananivijay" title="Bug reports">πŸ›</a></td>
477
509
  <td align="center"><a href="http://tompicton.com"><img src="https://avatars2.githubusercontent.com/u/12588098?v=4" width="100px;" alt=""/><br /><sub><b>Tom Picton</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Atpict" title="Bug reports">πŸ›</a> <a href="https://github.com/testing-library/user-event/commits?author=tpict" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/user-event/commits?author=tpict" title="Tests">⚠️</a></td>
478
510
  <td align="center"><a href="https://hung.dev"><img src="https://avatars3.githubusercontent.com/u/8603085?v=4" width="100px;" alt=""/><br /><sub><b>Hung Viet Nguyen</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Anvh95" title="Bug reports">πŸ›</a></td>
511
+ <td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="#projectManagement-nickmccurdy" title="Project Management">πŸ“†</a> <a href="#question-nickmccurdy" title="Answering Questions">πŸ’¬</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/user-event/commits?author=nickmccurdy" title="Tests">⚠️</a></td>
512
+ </tr>
513
+ <tr>
514
+ <td align="center"><a href="http://timdeschryver.dev"><img src="https://avatars1.githubusercontent.com/u/28659384?v=4" width="100px;" alt=""/><br /><sub><b>Tim Deschryver</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=timdeschryver" title="Tests">⚠️</a></td>
479
515
  </tr>
480
516
  </table>
481
517
 
package/dist/index.js CHANGED
@@ -9,6 +9,8 @@ var _dom = require("@testing-library/dom");
9
9
 
10
10
  var _type = require("./type");
11
11
 
12
+ var _tick = require("./tick");
13
+
12
14
  function isMousePressEvent(event) {
13
15
  return event === 'mousedown' || event === 'mouseup' || event === 'click' || event === 'dblclick';
14
16
  }
@@ -431,15 +433,35 @@ function tab({
431
433
  const tabIndexB = b.el.getAttribute('tabindex');
432
434
  const diff = tabIndexA - tabIndexB;
433
435
  return diff === 0 ? a.idx - b.idx : diff;
434
- });
435
- const index = orderedElements.findIndex(({
436
+ }).map(({
436
437
  el
437
- }) => el === el.ownerDocument.activeElement);
438
+ }) => el);
439
+ if (shift) orderedElements.reverse(); // keep only the checked or first element in each radio group
440
+
441
+ const prunedElements = [];
442
+
443
+ for (const el of orderedElements) {
444
+ if (el.type === 'radio' && el.name) {
445
+ const replacedIndex = prunedElements.findIndex(({
446
+ name
447
+ }) => name === el.name);
448
+
449
+ if (replacedIndex === -1) {
450
+ prunedElements.push(el);
451
+ } else if (el.checked) {
452
+ prunedElements.splice(replacedIndex, 1);
453
+ prunedElements.push(el);
454
+ }
455
+ } else {
456
+ prunedElements.push(el);
457
+ }
458
+ }
459
+
460
+ if (shift) prunedElements.reverse();
461
+ const index = prunedElements.findIndex(el => el === el.ownerDocument.activeElement);
438
462
  const nextIndex = shift ? index - 1 : index + 1;
439
- const defaultIndex = shift ? orderedElements.length - 1 : 0;
440
- const {
441
- el: next
442
- } = orderedElements[nextIndex] || orderedElements[defaultIndex];
463
+ const defaultIndex = shift ? prunedElements.length - 1 : 0;
464
+ const next = prunedElements[nextIndex] || prunedElements[defaultIndex];
443
465
 
444
466
  if (next.getAttribute('tabindex') === null) {
445
467
  next.setAttribute('tabindex', '0'); // jsdom requires tabIndex=0 for an item to become 'document.activeElement'
@@ -451,6 +473,34 @@ function tab({
451
473
  }
452
474
  }
453
475
 
476
+ async function hover(element, init) {
477
+ await (0, _tick.tick)();
478
+
479
+ _dom.fireEvent.mouseOver(element, getMouseEventOptions('mouseover', init));
480
+
481
+ await (0, _tick.tick)();
482
+
483
+ _dom.fireEvent.mouseEnter(element, getMouseEventOptions('mouseenter', init));
484
+
485
+ await (0, _tick.tick)();
486
+
487
+ _dom.fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init));
488
+ }
489
+
490
+ async function unhover(element, init) {
491
+ await (0, _tick.tick)();
492
+
493
+ _dom.fireEvent.mouseMove(element, getMouseEventOptions('mousemove', init));
494
+
495
+ await (0, _tick.tick)();
496
+
497
+ _dom.fireEvent.mouseOut(element, getMouseEventOptions('mouseout', init));
498
+
499
+ await (0, _tick.tick)();
500
+
501
+ _dom.fireEvent.mouseLeave(element, getMouseEventOptions('mouseleave', init));
502
+ }
503
+
454
504
  const userEvent = {
455
505
  click,
456
506
  dblClick,
@@ -459,7 +509,9 @@ const userEvent = {
459
509
  clear,
460
510
  type: _type.type,
461
511
  upload,
462
- tab
512
+ tab,
513
+ hover,
514
+ unhover
463
515
  };
464
516
  var _default = userEvent;
465
517
  /*
package/dist/type.js CHANGED
@@ -252,15 +252,20 @@ async function typeImpl(element, text, {
252
252
  }
253
253
 
254
254
  const eventOverrides = {};
255
+ let prevWasMinus, prevWasPeriod;
255
256
 
256
257
  for (const callback of eventCallbacks) {
257
258
  if (delay > 0) await wait(delay);
258
259
 
259
260
  if (!currentElement().disabled) {
260
261
  const returnValue = await callback({
262
+ prevWasMinus,
263
+ prevWasPeriod,
261
264
  eventOverrides
262
265
  });
263
266
  Object.assign(eventOverrides, returnValue == null ? void 0 : returnValue.eventOverrides);
267
+ prevWasMinus = returnValue == null ? void 0 : returnValue.prevWasMinus;
268
+ prevWasPeriod = returnValue == null ? void 0 : returnValue.prevWasPeriod;
264
269
  }
265
270
  }
266
271
  }
@@ -270,7 +275,9 @@ async function typeImpl(element, text, {
270
275
  newSelectionStart,
271
276
  eventOverrides
272
277
  }) {
273
- if (!currentElement().readOnly && newValue !== currentValue()) {
278
+ const prevValue = currentValue();
279
+
280
+ if (!currentElement().readOnly && newValue !== prevValue) {
274
281
  await (0, _tick.tick)();
275
282
 
276
283
  _dom.fireEvent.input(currentElement(), {
@@ -285,6 +292,10 @@ async function typeImpl(element, text, {
285
292
  newSelectionStart
286
293
  });
287
294
  }
295
+
296
+ return {
297
+ prevValue
298
+ };
288
299
  } // yes, calculateNewBackspaceValue and calculateNewValue look extremely similar
289
300
  // and you may be tempted to create a shared abstraction.
290
301
  // If you, brave soul, decide to so endevor, please increment this count
@@ -381,11 +392,14 @@ async function typeImpl(element, text, {
381
392
  }
382
393
 
383
394
  async function typeCharacter(char, {
395
+ prevWasMinus = false,
396
+ prevWasPeriod = false,
384
397
  eventOverrides
385
398
  }) {
386
399
  const key = char; // TODO: check if this also valid for characters with diacritic markers e.g. ΓΊΓ© etc
387
400
 
388
401
  const keyCode = char.charCodeAt(0);
402
+ let nextPrevWasMinus, nextPrevWasPeriod;
389
403
 
390
404
  const keyDownDefaultNotPrevented = _dom.fireEvent.keyDown(currentElement(), {
391
405
  key,
@@ -405,9 +419,43 @@ async function typeImpl(element, text, {
405
419
  });
406
420
 
407
421
  if (keyPressDefaultNotPrevented) {
408
- await fireInputEventIfNeeded({ ...calculateNewValue(key),
409
- eventOverrides
410
- });
422
+ let newEntry = char;
423
+
424
+ if (prevWasMinus) {
425
+ newEntry = `-${char}`;
426
+ } else if (prevWasPeriod) {
427
+ newEntry = `.${char}`;
428
+ }
429
+
430
+ const {
431
+ prevValue
432
+ } = await fireInputEventIfNeeded({ ...calculateNewValue(newEntry),
433
+ eventOverrides: {
434
+ data: key,
435
+ inputType: 'insertText',
436
+ ...eventOverrides
437
+ }
438
+ }); // typing "-" into a number input will not actually update the value
439
+ // so for the next character we type, the value should be set to
440
+ // `-${newEntry}`
441
+ // we also preserve the prevWasMinus when the value is unchanged due
442
+ // to typing an invalid character (typing "-a3" results in "-3")
443
+
444
+ if (currentElement().type === 'number') {
445
+ const newValue = currentValue();
446
+
447
+ if (newValue === prevValue && newEntry !== '-') {
448
+ nextPrevWasMinus = prevWasMinus;
449
+ } else {
450
+ nextPrevWasMinus = newEntry === '-';
451
+ }
452
+
453
+ if (newValue === prevValue && newEntry !== '.') {
454
+ nextPrevWasPeriod = prevWasPeriod;
455
+ } else {
456
+ nextPrevWasPeriod = newEntry === '.';
457
+ }
458
+ }
411
459
  }
412
460
  }
413
461
 
@@ -419,6 +467,11 @@ async function typeImpl(element, text, {
419
467
  which: keyCode,
420
468
  ...eventOverrides
421
469
  });
470
+
471
+ return {
472
+ prevWasMinus: nextPrevWasMinus,
473
+ prevWasPeriod: nextPrevWasPeriod
474
+ };
422
475
  }
423
476
 
424
477
  function modifier({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "11.3.1",
3
+ "version": "11.4.2",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "typings": "typings/index.d.ts",
@@ -43,11 +43,8 @@
43
43
  "devDependencies": {
44
44
  "@testing-library/dom": "^7.9.0",
45
45
  "@testing-library/jest-dom": "^5.9.0",
46
- "@testing-library/react": "^10.2.0",
47
46
  "is-ci": "^2.0.0",
48
- "kcd-scripts": "^6.2.0",
49
- "react": "^16.13.1",
50
- "react-dom": "^16.13.1"
47
+ "kcd-scripts": "^6.2.3"
51
48
  },
52
49
  "peerDependencies": {
53
50
  "@testing-library/dom": ">=7.9.0"
@@ -45,6 +45,8 @@ declare const userEvent: {
45
45
  userOpts?: ITypeOpts,
46
46
  ) => Promise<void>
47
47
  tab: (userOpts?: ITabUserOptions) => void
48
+ hover: (element: TargetElement, init?: MouseEventInit) => Promise<void>
49
+ unhover: (element: TargetElement, init?: MouseEventInit) => Promise<void>
48
50
  }
49
51
 
50
52
  export default userEvent