@testing-library/user-event 14.0.0-alpha.13 → 14.0.0-alpha.14

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.
@@ -0,0 +1,11 @@
1
+ import type { UserEvent } from '../setup';
2
+ export interface copyOptions {
3
+ document?: Document;
4
+ writeToClipboard?: boolean;
5
+ }
6
+ export declare function copy(this: UserEvent, options: Omit<copyOptions, 'writeToClipboard'> & {
7
+ writeToClipboard: true;
8
+ }): Promise<DataTransfer>;
9
+ export declare function copy(this: UserEvent, options?: Omit<copyOptions, 'writeToClipboard'> & {
10
+ writeToClipboard?: boolean;
11
+ }): DataTransfer;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.copy = copy;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ function copy(options) {
13
+ var _options$document, _doc$activeElement;
14
+
15
+ const doc = (_options$document = options == null ? void 0 : options.document) != null ? _options$document : document;
16
+ const target = (_doc$activeElement = doc.activeElement) != null ? _doc$activeElement :
17
+ /* istanbul ignore next */
18
+ doc.body;
19
+ const clipboardData = (0, _utils.copySelection)(target);
20
+
21
+ if (clipboardData.items.length === 0) {
22
+ return;
23
+ }
24
+
25
+ _dom.fireEvent.copy(target, {
26
+ clipboardData
27
+ });
28
+
29
+ return options != null && options.writeToClipboard ? (0, _utils.writeDataTransferToClipboard)(doc, clipboardData).then(() => clipboardData) : clipboardData;
30
+ }
@@ -0,0 +1,11 @@
1
+ import type { UserEvent } from '../setup';
2
+ export interface cutOptions {
3
+ document?: Document;
4
+ writeToClipboard?: boolean;
5
+ }
6
+ export declare function cut(this: UserEvent, options: Omit<cutOptions, 'writeToClipboard'> & {
7
+ writeToClipboard: true;
8
+ }): Promise<DataTransfer>;
9
+ export declare function cut(this: UserEvent, options?: Omit<cutOptions, 'writeToClipboard'> & {
10
+ writeToClipboard?: boolean;
11
+ }): DataTransfer;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.cut = cut;
7
+
8
+ var _dom = require("@testing-library/dom");
9
+
10
+ var _utils = require("../utils");
11
+
12
+ function cut(options) {
13
+ var _options$document, _doc$activeElement;
14
+
15
+ const doc = (_options$document = options == null ? void 0 : options.document) != null ? _options$document : document;
16
+ const target = (_doc$activeElement = doc.activeElement) != null ? _doc$activeElement :
17
+ /* istanbul ignore next */
18
+ doc.body;
19
+ const clipboardData = (0, _utils.copySelection)(target);
20
+
21
+ if (clipboardData.items.length === 0) {
22
+ return;
23
+ }
24
+
25
+ _dom.fireEvent.cut(target, {
26
+ clipboardData
27
+ });
28
+
29
+ if ((0, _utils.isEditable)(target)) {
30
+ var _prepareInput;
31
+
32
+ (_prepareInput = (0, _utils.prepareInput)('', target, 'deleteByCut')) == null ? void 0 : _prepareInput.commit();
33
+ }
34
+
35
+ return options != null && options.writeToClipboard ? (0, _utils.writeDataTransferToClipboard)(doc, clipboardData).then(() => clipboardData) : clipboardData;
36
+ }
@@ -0,0 +1,3 @@
1
+ export * from './copy';
2
+ export * from './cut';
3
+ export * from './paste';
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _copy = require("./copy");
8
+
9
+ Object.keys(_copy).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _copy[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _copy[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _cut = require("./cut");
21
+
22
+ Object.keys(_cut).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _cut[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _cut[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _paste = require("./paste");
34
+
35
+ Object.keys(_paste).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _paste[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _paste[key];
42
+ }
43
+ });
44
+ });
@@ -1,4 +1,4 @@
1
- import type { UserEvent } from './setup';
1
+ import type { UserEvent } from '../setup';
2
2
  export interface pasteOptions {
3
3
  document?: Document;
4
4
  }
@@ -7,7 +7,7 @@ exports.paste = paste;
7
7
 
8
8
  var _dom = require("@testing-library/dom");
9
9
 
10
- var _utils = require("./utils");
10
+ var _utils = require("../utils");
11
11
 
12
12
  function paste(clipboardData, options) {
13
13
  var _options$document, _doc$activeElement;
@@ -17,7 +17,9 @@ function paste(clipboardData, options) {
17
17
  /* istanbul ignore next */
18
18
  doc.body;
19
19
  const data = typeof clipboardData === 'string' ? getClipboardDataFromString(clipboardData) : clipboardData;
20
- return data ? pasteImpl(target, data) : readClipboardDataFromClipboardApi(doc).then(dt => pasteImpl(target, dt));
20
+ return data ? pasteImpl(target, data) : (0, _utils.readDataTransferFromClipboard)(doc).then(dt => pasteImpl(target, dt), () => {
21
+ throw new Error('`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.');
22
+ });
21
23
  }
22
24
 
23
25
  function pasteImpl(target, clipboardData) {
@@ -39,26 +41,5 @@ function pasteImpl(target, clipboardData) {
39
41
  function getClipboardDataFromString(text) {
40
42
  const dt = (0, _utils.createDataTransfer)();
41
43
  dt.setData('text', text);
42
- return dt;
43
- }
44
-
45
- async function readClipboardDataFromClipboardApi(document) {
46
- var _document$defaultView;
47
-
48
- const clipboard = (_document$defaultView = document.defaultView) == null ? void 0 : _document$defaultView.navigator.clipboard;
49
- const items = clipboard && (await clipboard.read());
50
-
51
- if (!items) {
52
- throw new Error('`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.');
53
- }
54
-
55
- const dt = (0, _utils.createDataTransfer)();
56
-
57
- for (const item of items) {
58
- for (const type of item.types) {
59
- dt.setData(type, await item.getType(type).then(b => (0, _utils.readBlobText)(b)));
60
- }
61
- }
62
-
63
44
  return dt;
64
45
  }
package/dist/setup.d.ts CHANGED
@@ -3,7 +3,7 @@ import { click, clickOptions, dblClick, tripleClick } from './click';
3
3
  import { hover, unhover } from './hover';
4
4
  import { keyboard, keyboardOptions } from './keyboard';
5
5
  import type { keyboardState } from './keyboard/types';
6
- import { paste } from './paste';
6
+ import { copy, copyOptions, cut, cutOptions, paste, pasteOptions } from './clipboard';
7
7
  import { pointer } from './pointer';
8
8
  import type { pointerOptions, pointerState } from './pointer/types';
9
9
  import { deselectOptions, selectOptions } from './selectOptions';
@@ -14,6 +14,8 @@ import { PointerOptions } from './utils';
14
14
  export declare const userEventApis: {
15
15
  clear: typeof clear;
16
16
  click: typeof click;
17
+ copy: typeof copy;
18
+ cut: typeof cut;
17
19
  dblClick: typeof dblClick;
18
20
  deselectOptions: typeof deselectOptions;
19
21
  hover: typeof hover;
@@ -33,12 +35,14 @@ export declare type UserEvent = UserEventApis & {
33
35
  setup: setup;
34
36
  };
35
37
  declare type ClickOptions = Omit<clickOptions, 'clickCount'>;
38
+ interface ClipboardOptions extends copyOptions, cutOptions, pasteOptions {
39
+ }
36
40
  declare type KeyboardOptions = Partial<keyboardOptions>;
37
41
  declare type PointerApiOptions = Partial<pointerOptions>;
38
42
  declare type TabOptions = Omit<tabOptions, 'shift'>;
39
43
  declare type TypeOptions = Omit<typeOptions, 'initialSelectionStart' | 'initialSelectionEnd'>;
40
44
  declare type UploadOptions = uploadOptions;
41
- interface SetupOptions extends ClickOptions, KeyboardOptions, PointerOptions, PointerApiOptions, TabOptions, TypeOptions, UploadOptions {
45
+ interface SetupOptions extends ClickOptions, ClipboardOptions, KeyboardOptions, PointerOptions, PointerApiOptions, TabOptions, TypeOptions, UploadOptions {
42
46
  }
43
47
  /**
44
48
  * Start a "session" with userEvent.
@@ -47,6 +51,8 @@ interface SetupOptions extends ClickOptions, KeyboardOptions, PointerOptions, Po
47
51
  export declare function setup(options?: SetupOptions): {
48
52
  clear: typeof clear;
49
53
  click: typeof click;
54
+ copy: typeof copy;
55
+ cut: typeof cut;
50
56
  dblClick: typeof dblClick;
51
57
  deselectOptions: typeof deselectOptions;
52
58
  hover: typeof hover;
@@ -66,6 +72,8 @@ export declare function setup(options?: SetupOptions): {
66
72
  setup(options: SetupOptions): {
67
73
  clear: typeof clear;
68
74
  click: typeof click;
75
+ copy: typeof copy;
76
+ cut: typeof cut;
69
77
  dblClick: typeof dblClick;
70
78
  deselectOptions: typeof deselectOptions;
71
79
  hover: typeof hover;
@@ -80,7 +88,7 @@ export declare function setup(options?: SetupOptions): {
80
88
  upload: typeof upload;
81
89
  } & any;
82
90
  };
83
- declare function _setup({ applyAccept, autoModify, delay, document, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, }: SetupOptions, { keyboardState, pointerState, }: {
91
+ declare function _setup({ applyAccept, autoModify, delay, document, keyboardMap, pointerMap, skipAutoClose, skipClick, skipHover, skipPointerEventsCheck, writeToClipboard, }: SetupOptions, { keyboardState, pointerState, }: {
84
92
  keyboardState: keyboardState;
85
93
  pointerState: pointerState;
86
94
  }): UserEventApis & {
package/dist/setup.js CHANGED
@@ -16,7 +16,7 @@ var _hover = require("./hover");
16
16
 
17
17
  var _keyboard = require("./keyboard");
18
18
 
19
- var _paste = require("./paste");
19
+ var _clipboard = require("./clipboard");
20
20
 
21
21
  var _pointer = require("./pointer");
22
22
 
@@ -33,11 +33,13 @@ var _utils = require("./utils");
33
33
  const userEventApis = {
34
34
  clear: _clear.clear,
35
35
  click: _click.click,
36
+ copy: _clipboard.copy,
37
+ cut: _clipboard.cut,
36
38
  dblClick: _click.dblClick,
37
39
  deselectOptions: _selectOptions.deselectOptions,
38
40
  hover: _hover.hover,
39
41
  keyboard: _keyboard.keyboard,
40
- paste: _paste.paste,
42
+ paste: _clipboard.paste,
41
43
  pointer: _pointer.pointer,
42
44
  selectOptions: _selectOptions.selectOptions,
43
45
  tab: _tab.tab,
@@ -77,7 +79,14 @@ function _setup({
77
79
  skipAutoClose,
78
80
  skipClick,
79
81
  skipHover,
80
- skipPointerEventsCheck = false
82
+ skipPointerEventsCheck = false,
83
+ // Changing default return type from DataTransfer to Promise<DataTransfer>
84
+ // would require a lot of overloading right now.
85
+ // The APIs returned by setup will most likely be changed to async before stable release anyway.
86
+ // See https://github.com/testing-library/user-event/issues/504#issuecomment-944883855
87
+ // So the default option can be changed during alpha instead of introducing too much code here.
88
+ // TODO: This should default to true
89
+ writeToClipboard = false
81
90
  }, {
82
91
  keyboardState,
83
92
  pointerState
@@ -99,7 +108,8 @@ function _setup({
99
108
  skipHover
100
109
  };
101
110
  const clipboardDefaults = {
102
- document
111
+ document,
112
+ writeToClipboard
103
113
  };
104
114
  const typeDefaults = {
105
115
  delay,
@@ -120,6 +130,20 @@ function _setup({
120
130
  };
121
131
  return _click.click.call(userEvent, ...args);
122
132
  },
133
+ // copy needs typecasting because of the overloading
134
+ copy: (...args) => {
135
+ args[0] = { ...clipboardDefaults,
136
+ ...args[0]
137
+ };
138
+ return _clipboard.copy.call(userEvent, ...args);
139
+ },
140
+ // cut needs typecasting because of the overloading
141
+ cut: (...args) => {
142
+ args[0] = { ...clipboardDefaults,
143
+ ...args[0]
144
+ };
145
+ return _clipboard.cut.call(userEvent, ...args);
146
+ },
123
147
  dblClick: (...args) => {
124
148
  args[1] = { ...pointerDefaults,
125
149
  ...clickDefaults,
@@ -156,7 +180,7 @@ function _setup({
156
180
  args[1] = { ...clipboardDefaults,
157
181
  ...args[1]
158
182
  };
159
- return _paste.paste.call(userEvent, ...args);
183
+ return _clipboard.paste.call(userEvent, ...args);
160
184
  },
161
185
  // pointer needs typecasting because of the overloading
162
186
  pointer: (...args) => {
@@ -5,3 +5,5 @@ export declare function attachClipboardStubToView(window: Window & typeof global
5
5
  };
6
6
  export declare function resetClipboardStubOnView(window: Window & typeof globalThis): void;
7
7
  export declare function detachClipboardStubFromView(window: Window & typeof globalThis): void;
8
+ export declare function readDataTransferFromClipboard(document: Document): Promise<DataTransfer>;
9
+ export declare function writeDataTransferToClipboard(document: Document, clipboardData: DataTransfer): Promise<void>;
@@ -6,7 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.attachClipboardStubToView = attachClipboardStubToView;
7
7
  exports.createClipboardItem = createClipboardItem;
8
8
  exports.detachClipboardStubFromView = detachClipboardStubFromView;
9
+ exports.readDataTransferFromClipboard = readDataTransferFromClipboard;
9
10
  exports.resetClipboardStubOnView = resetClipboardStubOnView;
11
+ exports.writeDataTransferToClipboard = writeDataTransferToClipboard;
10
12
 
11
13
  var _ = require("..");
12
14
 
@@ -127,6 +129,49 @@ function detachClipboardStubFromView(window) {
127
129
  window.navigator.clipboard[ClipboardStubControl].detachClipboardStub();
128
130
  }
129
131
  }
132
+
133
+ async function readDataTransferFromClipboard(document) {
134
+ var _document$defaultView;
135
+
136
+ const clipboard = (_document$defaultView = document.defaultView) == null ? void 0 : _document$defaultView.navigator.clipboard;
137
+ const items = clipboard && (await clipboard.read());
138
+
139
+ if (!items) {
140
+ throw new Error('The Clipboard API is unavailable.');
141
+ }
142
+
143
+ const dt = (0, _.createDataTransfer)();
144
+
145
+ for (const item of items) {
146
+ for (const type of item.types) {
147
+ dt.setData(type, await item.getType(type).then(b => (0, _.readBlobText)(b)));
148
+ }
149
+ }
150
+
151
+ return dt;
152
+ }
153
+
154
+ async function writeDataTransferToClipboard(document, clipboardData) {
155
+ var _document$defaultView2;
156
+
157
+ const clipboard = (_document$defaultView2 = document.defaultView) == null ? void 0 : _document$defaultView2.navigator.clipboard;
158
+ const items = [];
159
+
160
+ for (let i = 0; i < clipboardData.items.length; i++) {
161
+ const dtItem = clipboardData.items[i];
162
+ const blob = (0, _.getBlobFromDataTransferItem)(dtItem);
163
+ items.push(createClipboardItem(blob));
164
+ }
165
+
166
+ const written = clipboard && (await clipboard.write(items).then(() => true, // Can happen with other implementations that e.g. require permissions
167
+
168
+ /* istanbul ignore next */
169
+ () => false));
170
+
171
+ if (!written) {
172
+ throw new Error('The Clipboard API is unavailable.');
173
+ }
174
+ }
130
175
  /* istanbul ignore else */
131
176
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
132
177
 
@@ -1 +1,2 @@
1
1
  export declare function createDataTransfer(files?: File[]): DataTransfer;
2
+ export declare function getBlobFromDataTransferItem(item: DataTransferItem): Blob;
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createDataTransfer = createDataTransfer;
7
+ exports.getBlobFromDataTransferItem = getBlobFromDataTransferItem;
7
8
 
8
9
  var _FileList = require("./FileList");
9
10
 
@@ -140,4 +141,18 @@ function createDataTransfer(files = []) {
140
141
  get: () => (0, _FileList.createFileList)(files)
141
142
  });
142
143
  return dt;
144
+ }
145
+
146
+ function getBlobFromDataTransferItem(item) {
147
+ if (item.kind === 'file') {
148
+ return item.getAsFile();
149
+ }
150
+
151
+ let data = '';
152
+ item.getAsString(s => {
153
+ data = s;
154
+ });
155
+ return new Blob([data], {
156
+ type: item.type
157
+ });
143
158
  }
@@ -0,0 +1 @@
1
+ export declare function copySelection(target: Element): DataTransfer;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.copySelection = copySelection;
7
+
8
+ var _document = require("../../document");
9
+
10
+ var _DataTransfer = require("../dataTransfer/DataTransfer");
11
+
12
+ var _selection = require("./selection");
13
+
14
+ function copySelection(target) {
15
+ const data = (0, _selection.hasOwnSelection)(target) ? {
16
+ 'text/plain': readSelectedValueFromInput(target)
17
+ } : // TODO: We could implement text/html copying of DOM nodes here
18
+ {
19
+ 'text/plain': String(target.ownerDocument.getSelection())
20
+ };
21
+ const dt = (0, _DataTransfer.createDataTransfer)();
22
+
23
+ for (const type in data) {
24
+ if (data[type]) {
25
+ dt.setData(type, data[type]);
26
+ }
27
+ }
28
+
29
+ return dt;
30
+ }
31
+
32
+ function readSelectedValueFromInput(target) {
33
+ const sel = (0, _document.getUISelection)(target);
34
+ const val = (0, _document.getUIValue)(target);
35
+ return val.substring(sel.startOffset, sel.endOffset);
36
+ }
@@ -14,6 +14,7 @@ export * from './edit/isValidInputTimeValue';
14
14
  export * from './edit/maxLength';
15
15
  export * from './edit/prepareInput';
16
16
  export * from './focus/blur';
17
+ export * from './focus/copySelection';
17
18
  export * from './focus/focus';
18
19
  export * from './focus/getActiveElement';
19
20
  export * from './focus/getTabDestination';
@@ -212,6 +212,19 @@ Object.keys(_blur).forEach(function (key) {
212
212
  });
213
213
  });
214
214
 
215
+ var _copySelection = require("./focus/copySelection");
216
+
217
+ Object.keys(_copySelection).forEach(function (key) {
218
+ if (key === "default" || key === "__esModule") return;
219
+ if (key in exports && exports[key] === _copySelection[key]) return;
220
+ Object.defineProperty(exports, key, {
221
+ enumerable: true,
222
+ get: function () {
223
+ return _copySelection[key];
224
+ }
225
+ });
226
+ });
227
+
215
228
  var _focus = require("./focus/focus");
216
229
 
217
230
  Object.keys(_focus).forEach(function (key) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testing-library/user-event",
3
- "version": "14.0.0-alpha.13",
3
+ "version": "14.0.0-alpha.14",
4
4
  "description": "Fire events the same way the user does",
5
5
  "main": "dist/index.js",
6
6
  "keywords": [