@testing-library/user-event 14.0.0-alpha.16 → 14.0.0-alpha.17
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/setup/directApi.d.ts +1 -2
- package/dist/setup/directApi.js +2 -2
- package/dist/utility/upload.d.ts +1 -1
- package/dist/utility/upload.js +9 -35
- package/dist/utils/dataTransfer/FileList.js +13 -3
- package/dist/utils/edit/setFiles.d.ts +12 -0
- package/dist/utils/edit/setFiles.js +72 -0
- package/package.json +1 -1
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { PointerOptions } from '../utils';
|
|
2
|
-
import type { uploadInit } from '../utility';
|
|
3
2
|
import type { PointerInput } from '../pointer';
|
|
4
3
|
import type { UserEventApi } from '.';
|
|
5
4
|
import { Config } from './config';
|
|
@@ -17,5 +16,5 @@ export declare function selectOptions(select: Element, values: HTMLElement | HTM
|
|
|
17
16
|
export declare function tripleClick(element: Element, options?: Partial<Config>): Promise<void>;
|
|
18
17
|
export declare function type(element: Element, text: string, options?: Partial<Config> & Parameters<UserEventApi['type']>[2]): Promise<void>;
|
|
19
18
|
export declare function unhover(element: Element, options?: PointerOptions): Promise<void>;
|
|
20
|
-
export declare function upload(element: HTMLElement, fileOrFiles: File | File[],
|
|
19
|
+
export declare function upload(element: HTMLElement, fileOrFiles: File | File[], options?: Partial<Config>): Promise<void>;
|
|
21
20
|
export declare function tab(options?: Partial<Config> & Parameters<UserEventApi['tab']>[0]): Promise<void>;
|
package/dist/setup/directApi.js
CHANGED
|
@@ -86,8 +86,8 @@ function unhover(element, options = {}) {
|
|
|
86
86
|
return instance.unhover(element);
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
-
function upload(element, fileOrFiles,
|
|
90
|
-
return (0, _setup.setupDirect)(options).upload(element, fileOrFiles
|
|
89
|
+
function upload(element, fileOrFiles, options = {}) {
|
|
90
|
+
return (0, _setup.setupDirect)(options).upload(element, fileOrFiles);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
function tab(options = {}) {
|
package/dist/utility/upload.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import { UserEvent } from '../setup';
|
|
|
2
2
|
export interface uploadInit {
|
|
3
3
|
changeInit?: EventInit;
|
|
4
4
|
}
|
|
5
|
-
export declare function upload(this: UserEvent, element: HTMLElement, fileOrFiles: File | File[]
|
|
5
|
+
export declare function upload(this: UserEvent, element: HTMLElement, fileOrFiles: File | File[]): Promise<void>;
|
package/dist/utility/upload.js
CHANGED
|
@@ -11,7 +11,9 @@ var _utils = require("../utils");
|
|
|
11
11
|
|
|
12
12
|
var _setup = require("../setup");
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
var _setFiles = require("#src/utils/edit/setFiles");
|
|
15
|
+
|
|
16
|
+
async function upload(element, fileOrFiles) {
|
|
15
17
|
var _input$files;
|
|
16
18
|
|
|
17
19
|
const input = (0, _utils.isElementType)(element, 'label') ? element.control : element;
|
|
@@ -36,41 +38,13 @@ async function upload(element, fileOrFiles, init) {
|
|
|
36
38
|
return f === ((_input$files2 = input.files) == null ? void 0 : _input$files2.item(i));
|
|
37
39
|
})) {
|
|
38
40
|
return;
|
|
39
|
-
}
|
|
40
|
-
// but a new Event with a type set to "input" and "change"
|
|
41
|
-
// Kinda odd...
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const inputFiles = { ...files,
|
|
45
|
-
length: files.length,
|
|
46
|
-
item: index => files[index],
|
|
47
|
-
|
|
48
|
-
[Symbol.iterator]() {
|
|
49
|
-
let i = 0;
|
|
50
|
-
return {
|
|
51
|
-
next: () => ({
|
|
52
|
-
done: i >= files.length,
|
|
53
|
-
value: files[i++]
|
|
54
|
-
})
|
|
55
|
-
};
|
|
56
|
-
}
|
|
41
|
+
}
|
|
57
42
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
bubbles: true,
|
|
64
|
-
cancelable: false,
|
|
65
|
-
composed: true
|
|
66
|
-
}));
|
|
67
|
-
|
|
68
|
-
_dom.fireEvent.change(input, {
|
|
69
|
-
target: {
|
|
70
|
-
files: inputFiles
|
|
71
|
-
},
|
|
72
|
-
...(init == null ? void 0 : init.changeInit)
|
|
73
|
-
});
|
|
43
|
+
(0, _setFiles.setFiles)(input, (0, _utils.createFileList)(files));
|
|
44
|
+
|
|
45
|
+
_dom.fireEvent.input(input);
|
|
46
|
+
|
|
47
|
+
_dom.fireEvent.change(input);
|
|
74
48
|
}
|
|
75
49
|
|
|
76
50
|
function isAcceptableFile(file, accept) {
|
|
@@ -7,7 +7,17 @@ exports.createFileList = createFileList;
|
|
|
7
7
|
|
|
8
8
|
// FileList can not be created per constructor.
|
|
9
9
|
function createFileList(files) {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
const list = { ...files,
|
|
11
|
+
length: files.length,
|
|
12
|
+
item: index => list[index],
|
|
13
|
+
[Symbol.iterator]: function* nextFile() {
|
|
14
|
+
for (let i = 0; i < list.length; i++) {
|
|
15
|
+
yield list[i];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
list.constructor = FileList;
|
|
20
|
+
Object.setPrototypeOf(list, FileList.prototype);
|
|
21
|
+
Object.freeze(list);
|
|
22
|
+
return list;
|
|
13
23
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const fakeFiles: unique symbol;
|
|
2
|
+
declare global {
|
|
3
|
+
interface HTMLInputElement {
|
|
4
|
+
[fakeFiles]?: {
|
|
5
|
+
restore: () => void;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export declare function setFiles(el: HTMLInputElement & {
|
|
10
|
+
type: 'file';
|
|
11
|
+
}, files: FileList): void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.setFiles = setFiles;
|
|
7
|
+
// It is not possible to create a real FileList programmatically.
|
|
8
|
+
// Therefore assigning `files` property with a programmatically created FileList results in an error.
|
|
9
|
+
// Just assigning the property (as per fireEvent) breaks the interweaving with the `value` property.
|
|
10
|
+
const fakeFiles = Symbol('files and value properties are mocked');
|
|
11
|
+
|
|
12
|
+
function setFiles(el, files) {
|
|
13
|
+
var _el$fakeFiles;
|
|
14
|
+
|
|
15
|
+
(_el$fakeFiles = el[fakeFiles]) == null ? void 0 : _el$fakeFiles.restore();
|
|
16
|
+
const objectDescriptors = Object.getOwnPropertyDescriptors(el);
|
|
17
|
+
const prototypeDescriptors = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(el));
|
|
18
|
+
|
|
19
|
+
function restore() {
|
|
20
|
+
Object.defineProperties(el, {
|
|
21
|
+
files: { ...prototypeDescriptors.files,
|
|
22
|
+
...objectDescriptors.files
|
|
23
|
+
},
|
|
24
|
+
value: { ...prototypeDescriptors.value,
|
|
25
|
+
...objectDescriptors.value
|
|
26
|
+
},
|
|
27
|
+
type: { ...prototypeDescriptors.type,
|
|
28
|
+
...objectDescriptors.type
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
el[fakeFiles] = {
|
|
34
|
+
restore
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperties(el, {
|
|
37
|
+
files: { ...prototypeDescriptors.files,
|
|
38
|
+
...objectDescriptors.files,
|
|
39
|
+
get: () => files
|
|
40
|
+
},
|
|
41
|
+
value: { ...prototypeDescriptors.value,
|
|
42
|
+
...objectDescriptors.value,
|
|
43
|
+
get: () => files.length ? `C:\\fakepath\\${files[0].name}` : '',
|
|
44
|
+
|
|
45
|
+
set(v) {
|
|
46
|
+
if (v === '') {
|
|
47
|
+
restore();
|
|
48
|
+
} else {
|
|
49
|
+
var _objectDescriptors$va;
|
|
50
|
+
|
|
51
|
+
(_objectDescriptors$va = objectDescriptors.value.set) == null ? void 0 : _objectDescriptors$va.call(el, v);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
},
|
|
56
|
+
// eslint-disable-next-line accessor-pairs
|
|
57
|
+
type: { ...prototypeDescriptors.type,
|
|
58
|
+
...objectDescriptors.type,
|
|
59
|
+
|
|
60
|
+
set(v) {
|
|
61
|
+
if (v !== 'file') {
|
|
62
|
+
restore() // In the browser the value will be empty.
|
|
63
|
+
// In Jsdom the value will be the same as
|
|
64
|
+
// before this element became file input - which might be empty.
|
|
65
|
+
;
|
|
66
|
+
el.type = v;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|