@testing-library/user-event 12.7.1 → 12.8.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/README.md CHANGED
@@ -271,12 +271,15 @@ test('types into the input', () => {
271
271
  })
272
272
  ```
273
273
 
274
- ### `upload(element, file, [{ clickInit, changeInit }])`
274
+ ### `upload(element, file, [{ clickInit, changeInit }], [options])`
275
275
 
276
276
  Uploads file to an `<input>`. For uploading multiple files use `<input>` with
277
277
  `multiple` attribute and the second `upload` argument must be array then. Also
278
278
  it's possible to initialize click or change event with using third argument.
279
279
 
280
+ If `options.applyAccept` is set to `true` and there is an `accept` attribute on
281
+ the element, files that don't match will be discarded.
282
+
280
283
  ```jsx
281
284
  import React from 'react'
282
285
  import {render, screen} from '@testing-library/react'
@@ -683,6 +686,9 @@ Thanks goes to these people ([emoji key][emojis]):
683
686
  <td align="center"><a href="https://github.com/vasilii-kovalev"><img src="https://avatars0.githubusercontent.com/u/10310491?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vasilii Kovalev</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=vasilii-kovalev" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=vasilii-kovalev" title="Documentation">📖</a></td>
684
687
  <td align="center"><a href="https://www.daleseo.com"><img src="https://avatars1.githubusercontent.com/u/5466341?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dale Seo</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=daleseo" title="Documentation">📖</a></td>
685
688
  <td align="center"><a href="http://www.alex-boyce.me/"><img src="https://avatars.githubusercontent.com/u/4050934?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alex Boyce</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=curiosity26" title="Code">💻</a></td>
689
+ <td align="center"><a href="https://benadamstyles.com"><img src="https://avatars.githubusercontent.com/u/4380655?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ben Styles</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=benadamstyles" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=benadamstyles" title="Tests">⚠️</a></td>
690
+ <td align="center"><a href="http://laurabeatris.com"><img src="https://avatars.githubusercontent.com/u/48022589?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Laura Beatris</b></sub></a><br /><a href="https://github.com/testing-library/user-event/commits?author=LauraBeatris" title="Code">💻</a> <a href="https://github.com/testing-library/user-event/commits?author=LauraBeatris" title="Tests">⚠️</a></td>
691
+ <td align="center"><a href="https://twitter.com/boriscoder"><img src="https://avatars.githubusercontent.com/u/812240?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Boris Serdiuk</b></sub></a><br /><a href="https://github.com/testing-library/user-event/issues?q=author%3Ajust-boris" title="Bug reports">🐛</a></td>
686
692
  </tr>
687
693
  </table>
688
694
 
package/dist/click.js CHANGED
@@ -71,11 +71,13 @@ function clickElement(element, init, {
71
71
  if (!element.disabled) {
72
72
  const continueDefaultHandling = _dom.fireEvent.mouseDown(element, (0, _utils.getMouseEventOptions)('mousedown', init, clickCount));
73
73
 
74
- if (continueDefaultHandling && element !== element.ownerDocument.activeElement) {
75
- if (previousElement && !(0, _utils.isFocusable)(element)) {
74
+ if (continueDefaultHandling) {
75
+ const closestFocusable = findClosest(element, _utils.isFocusable);
76
+
77
+ if (previousElement && !closestFocusable) {
76
78
  (0, _blur.blur)(previousElement, init);
77
- } else {
78
- (0, _focus.focus)(element, init);
79
+ } else if (closestFocusable) {
80
+ (0, _focus.focus)(closestFocusable, init);
79
81
  }
80
82
  }
81
83
  }
@@ -92,6 +94,18 @@ function clickElement(element, init, {
92
94
  }
93
95
  }
94
96
 
97
+ function findClosest(el, callback) {
98
+ do {
99
+ if (callback(el)) {
100
+ return el;
101
+ }
102
+
103
+ el = el.parentElement;
104
+ } while (el && el !== document.body);
105
+
106
+ return undefined;
107
+ }
108
+
95
109
  function click(element, init, {
96
110
  skipHover = false,
97
111
  clickCount = 0
package/dist/upload.js CHANGED
@@ -13,18 +13,25 @@ var _blur = require("./blur");
13
13
 
14
14
  var _focus = require("./focus");
15
15
 
16
- function upload(element, fileOrFiles, init) {
16
+ function upload(element, fileOrFiles, init, {
17
+ applyAccept = false
18
+ } = {}) {
17
19
  if (element.disabled) return;
18
20
  (0, _click.click)(element, init);
19
21
  const input = element.tagName === 'LABEL' ? element.control : element;
20
- const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
22
+ const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter(file => !applyAccept || isAcceptableFile(file, element.accept)).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
21
23
 
22
24
  (0, _blur.blur)(element, init); // focus fires when they make their selection
23
25
 
24
- (0, _focus.focus)(element, init); // the event fired in the browser isn't actually an "input" or "change" event
26
+ (0, _focus.focus)(element, init); // treat empty array as if the user just closed the file upload dialog
27
+
28
+ if (files.length === 0) {
29
+ return;
30
+ } // the event fired in the browser isn't actually an "input" or "change" event
25
31
  // but a new Event with a type set to "input" and "change"
26
32
  // Kinda odd...
27
33
 
34
+
28
35
  const inputFiles = {
29
36
  length: files.length,
30
37
  item: index => files[index],
@@ -46,4 +53,22 @@ function upload(element, fileOrFiles, init) {
46
53
  },
47
54
  ...init
48
55
  });
56
+ }
57
+
58
+ function isAcceptableFile(file, accept) {
59
+ if (!accept) {
60
+ return true;
61
+ }
62
+
63
+ const wildcards = ['audio/*', 'image/*', 'video/*'];
64
+ return accept.split(',').some(acceptToken => {
65
+ if (acceptToken[0] === '.') {
66
+ // tokens starting with a dot represent a file extension
67
+ return file.name.endsWith(acceptToken);
68
+ } else if (wildcards.includes(acceptToken)) {
69
+ return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1));
70
+ }
71
+
72
+ return file.type === acceptToken;
73
+ });
49
74
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "12.7.1",
3
+ "version": "12.8.0",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "typings": "typings/index.d.ts",
@@ -26,6 +26,10 @@ export interface IClickOptions {
26
26
  clickCount?: number
27
27
  }
28
28
 
29
+ export interface IUploadOptions {
30
+ applyAccept?: boolean
31
+ }
32
+
29
33
  declare const userEvent: {
30
34
  clear: (element: TargetElement) => void
31
35
  click: (
@@ -52,6 +56,7 @@ declare const userEvent: {
52
56
  element: TargetElement,
53
57
  files: FilesArgument,
54
58
  init?: UploadInitArgument,
59
+ options?: IUploadOptions,
55
60
  ) => void
56
61
  type: <T extends ITypeOpts>(
57
62
  element: TargetElement,