@testing-library/user-event 12.7.1 → 12.7.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
@@ -683,6 +683,7 @@ Thanks goes to these people ([emoji key][emojis]):
683
683
  <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
684
  <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
685
  <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>
686
+ <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>
686
687
  </tr>
687
688
  </table>
688
689
 
package/dist/upload.js CHANGED
@@ -17,14 +17,19 @@ function upload(element, fileOrFiles, init) {
17
17
  if (element.disabled) return;
18
18
  (0, _click.click)(element, init);
19
19
  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
20
+ const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter(file => isAcceptableFile(file, element.accept)).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
21
21
 
22
22
  (0, _blur.blur)(element, init); // focus fires when they make their selection
23
23
 
24
- (0, _focus.focus)(element, init); // the event fired in the browser isn't actually an "input" or "change" event
24
+ (0, _focus.focus)(element, init); // treat empty array as if the user just closed the file upload dialog
25
+
26
+ if (files.length === 0) {
27
+ return;
28
+ } // the event fired in the browser isn't actually an "input" or "change" event
25
29
  // but a new Event with a type set to "input" and "change"
26
30
  // Kinda odd...
27
31
 
32
+
28
33
  const inputFiles = {
29
34
  length: files.length,
30
35
  item: index => files[index],
@@ -46,4 +51,22 @@ function upload(element, fileOrFiles, init) {
46
51
  },
47
52
  ...init
48
53
  });
54
+ }
55
+
56
+ function isAcceptableFile(file, accept) {
57
+ if (!accept) {
58
+ return true;
59
+ }
60
+
61
+ const wildcards = ['audio/*', 'image/*', 'video/*'];
62
+ return accept.split(',').some(acceptToken => {
63
+ if (acceptToken[0] === '.') {
64
+ // tokens starting with a dot represent a file extension
65
+ return file.name.endsWith(acceptToken);
66
+ } else if (wildcards.includes(acceptToken)) {
67
+ return file.type.startsWith(acceptToken.substr(0, acceptToken.length - 1));
68
+ }
69
+
70
+ return file.type === acceptToken;
71
+ });
49
72
  }
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.7.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",