@testing-library/user-event 12.7.4 → 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 +5 -1
- package/dist/upload.js +28 -3
- package/package.json +1 -1
- package/typings/index.d.ts +5 -0
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'
|
|
@@ -685,6 +688,7 @@ Thanks goes to these people ([emoji key][emojis]):
|
|
|
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>
|
|
686
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>
|
|
687
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>
|
|
688
692
|
</tr>
|
|
689
693
|
</table>
|
|
690
694
|
|
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); //
|
|
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
package/typings/index.d.ts
CHANGED
|
@@ -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,
|