@testing-library/user-event 14.0.0-alpha.10 → 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.
Files changed (48) hide show
  1. package/dist/click.d.ts +3 -3
  2. package/dist/click.js +8 -9
  3. package/dist/clipboard/copy.d.ts +11 -0
  4. package/dist/clipboard/copy.js +30 -0
  5. package/dist/clipboard/cut.d.ts +11 -0
  6. package/dist/clipboard/cut.js +36 -0
  7. package/dist/clipboard/index.d.ts +3 -0
  8. package/dist/clipboard/index.js +44 -0
  9. package/dist/clipboard/paste.d.ts +6 -0
  10. package/dist/clipboard/paste.js +45 -0
  11. package/dist/hover.d.ts +2 -2
  12. package/dist/hover.js +2 -2
  13. package/dist/index.d.ts +0 -2
  14. package/dist/index.js +0 -8
  15. package/dist/keyboard/getEventProps.d.ts +0 -2
  16. package/dist/keyboard/getEventProps.js +1 -7
  17. package/dist/keyboard/getNextKeyDef.d.ts +0 -1
  18. package/dist/keyboard/getNextKeyDef.js +2 -32
  19. package/dist/keyboard/index.d.ts +0 -1
  20. package/dist/keyboard/index.js +0 -8
  21. package/dist/keyboard/keyMap.js +24 -48
  22. package/dist/keyboard/keyboardImplementation.js +12 -16
  23. package/dist/keyboard/plugins/index.d.ts +0 -1
  24. package/dist/keyboard/plugins/index.js +1 -10
  25. package/dist/keyboard/types.d.ts +0 -2
  26. package/dist/selectOptions.d.ts +2 -2
  27. package/dist/selectOptions.js +25 -26
  28. package/dist/setup.d.ts +11 -3
  29. package/dist/setup.js +57 -19
  30. package/dist/upload.d.ts +0 -1
  31. package/dist/upload.js +1 -1
  32. package/dist/utils/dataTransfer/Blob.d.ts +1 -0
  33. package/dist/utils/dataTransfer/Blob.js +21 -0
  34. package/dist/utils/dataTransfer/Clipboard.d.ts +9 -0
  35. package/dist/utils/dataTransfer/Clipboard.js +188 -0
  36. package/dist/utils/dataTransfer/DataTransfer.d.ts +2 -0
  37. package/dist/utils/dataTransfer/DataTransfer.js +158 -0
  38. package/dist/utils/dataTransfer/FileList.d.ts +1 -0
  39. package/dist/utils/dataTransfer/FileList.js +13 -0
  40. package/dist/utils/focus/copySelection.d.ts +1 -0
  41. package/dist/utils/focus/copySelection.js +36 -0
  42. package/dist/utils/index.d.ts +5 -0
  43. package/dist/utils/index.js +65 -0
  44. package/package.json +1 -1
  45. package/dist/keyboard/specialCharMap.d.ts +0 -22
  46. package/dist/keyboard/specialCharMap.js +0 -30
  47. package/dist/paste.d.ts +0 -7
  48. package/dist/paste.js +0 -60
@@ -67,8 +67,6 @@ export interface keyboardKey {
67
67
  key?: string;
68
68
  /** Location on the keyboard for keys with multiple representation */
69
69
  location?: DOM_KEY_LOCATION;
70
- /** keyCode for legacy support */
71
- keyCode?: number;
72
70
  /** Does the character in `key` require/imply AltRight to be pressed? */
73
71
  altGr?: boolean;
74
72
  /** Does the character in `key` require/imply a shiftKey to be pressed? */
@@ -1,4 +1,4 @@
1
1
  import { PointerOptions } from './utils';
2
2
  import type { UserEvent } from './setup';
3
- export declare function selectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, init?: MouseEventInit, options?: PointerOptions): void;
4
- export declare function deselectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, init?: MouseEventInit, options?: PointerOptions): void;
3
+ export declare function selectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, options?: PointerOptions): void;
4
+ export declare function deselectOptions(this: UserEvent, select: Element, values: HTMLElement | HTMLElement[] | string[] | string, options?: PointerOptions): void;
@@ -10,15 +10,15 @@ var _dom = require("@testing-library/dom");
10
10
 
11
11
  var _utils = require("./utils");
12
12
 
13
- function selectOptions(select, values, init, options = {}) {
14
- return selectOptionsBase.call(this, true, select, values, init, options);
13
+ function selectOptions(select, values, options = {}) {
14
+ return selectOptionsBase.call(this, true, select, values, options);
15
15
  }
16
16
 
17
- function deselectOptions(select, values, init, options = {}) {
18
- return selectOptionsBase.call(this, false, select, values, init, options);
17
+ function deselectOptions(select, values, options = {}) {
18
+ return selectOptionsBase.call(this, false, select, values, options);
19
19
  }
20
20
 
21
- function selectOptionsBase(newValue, select, values, init, {
21
+ function selectOptionsBase(newValue, select, values, {
22
22
  skipPointerEventsCheck
23
23
  }) {
24
24
  if (!newValue && !select.multiple) {
@@ -48,42 +48,42 @@ function selectOptionsBase(newValue, select, values, init, {
48
48
  const withPointerEvents = skipPointerEventsCheck ? true : (0, _utils.hasPointerEvents)(option); // events fired for multiple select are weird. Can't use hover...
49
49
 
50
50
  if (withPointerEvents) {
51
- _dom.fireEvent.pointerOver(option, init);
51
+ _dom.fireEvent.pointerOver(option);
52
52
 
53
- _dom.fireEvent.pointerEnter(select, init);
53
+ _dom.fireEvent.pointerEnter(select);
54
54
 
55
55
  _dom.fireEvent.mouseOver(option);
56
56
 
57
57
  _dom.fireEvent.mouseEnter(select);
58
58
 
59
- _dom.fireEvent.pointerMove(option, init);
59
+ _dom.fireEvent.pointerMove(option);
60
60
 
61
- _dom.fireEvent.mouseMove(option, init);
61
+ _dom.fireEvent.mouseMove(option);
62
62
 
63
- _dom.fireEvent.pointerDown(option, init);
63
+ _dom.fireEvent.pointerDown(option);
64
64
 
65
- _dom.fireEvent.mouseDown(option, init);
65
+ _dom.fireEvent.mouseDown(option);
66
66
  }
67
67
 
68
68
  (0, _utils.focus)(select);
69
69
 
70
70
  if (withPointerEvents) {
71
- _dom.fireEvent.pointerUp(option, init);
71
+ _dom.fireEvent.pointerUp(option);
72
72
 
73
- _dom.fireEvent.mouseUp(option, init);
73
+ _dom.fireEvent.mouseUp(option);
74
74
  }
75
75
 
76
76
  selectOption(option);
77
77
 
78
78
  if (withPointerEvents) {
79
- _dom.fireEvent.click(option, init);
79
+ _dom.fireEvent.click(option);
80
80
  }
81
81
  }
82
82
  } else if (selectedOptions.length === 1) {
83
83
  const withPointerEvents = skipPointerEventsCheck ? true : (0, _utils.hasPointerEvents)(select); // the click to open the select options
84
84
 
85
85
  if (withPointerEvents) {
86
- this.click(select, init, {
86
+ this.click(select, {
87
87
  skipPointerEventsCheck: true
88
88
  });
89
89
  } else {
@@ -95,32 +95,32 @@ function selectOptionsBase(newValue, select, values, init, {
95
95
  if (withPointerEvents) {
96
96
  // the browser triggers another click event on the select for the click on the option
97
97
  // this second click has no 'down' phase
98
- _dom.fireEvent.pointerOver(select, init);
98
+ _dom.fireEvent.pointerOver(select);
99
99
 
100
- _dom.fireEvent.pointerEnter(select, init);
100
+ _dom.fireEvent.pointerEnter(select);
101
101
 
102
102
  _dom.fireEvent.mouseOver(select);
103
103
 
104
104
  _dom.fireEvent.mouseEnter(select);
105
105
 
106
- _dom.fireEvent.pointerUp(select, init);
106
+ _dom.fireEvent.pointerUp(select);
107
107
 
108
- _dom.fireEvent.mouseUp(select, init);
108
+ _dom.fireEvent.mouseUp(select);
109
109
 
110
- _dom.fireEvent.click(select, init);
110
+ _dom.fireEvent.click(select);
111
111
  }
112
112
  } else {
113
113
  throw (0, _dom.getConfig)().getElementError(`Cannot select multiple options on a non-multiple select`, select);
114
114
  }
115
115
  } else if (select.getAttribute('role') === 'listbox') {
116
116
  selectedOptions.forEach(option => {
117
- this.hover(option, init, {
117
+ this.hover(option, {
118
118
  skipPointerEventsCheck
119
119
  });
120
- this.click(option, init, {
120
+ this.click(option, {
121
121
  skipPointerEventsCheck
122
122
  });
123
- this.unhover(option, init, {
123
+ this.unhover(option, {
124
124
  skipPointerEventsCheck
125
125
  });
126
126
  });
@@ -133,10 +133,9 @@ function selectOptionsBase(newValue, select, values, init, {
133
133
  (0, _dom.fireEvent)(select, (0, _dom.createEvent)('input', select, {
134
134
  bubbles: true,
135
135
  cancelable: false,
136
- composed: true,
137
- ...init
136
+ composed: true
138
137
  }));
139
138
 
140
- _dom.fireEvent.change(select, init);
139
+ _dom.fireEvent.change(select);
141
140
  }
142
141
  }
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
 
@@ -28,14 +28,18 @@ var _type = require("./type");
28
28
 
29
29
  var _upload = require("./upload");
30
30
 
31
+ var _utils = require("./utils");
32
+
31
33
  const userEventApis = {
32
34
  clear: _clear.clear,
33
35
  click: _click.click,
36
+ copy: _clipboard.copy,
37
+ cut: _clipboard.cut,
34
38
  dblClick: _click.dblClick,
35
39
  deselectOptions: _selectOptions.deselectOptions,
36
40
  hover: _hover.hover,
37
41
  keyboard: _keyboard.keyboard,
38
- paste: _paste.paste,
42
+ paste: _clipboard.paste,
39
43
  pointer: _pointer.pointer,
40
44
  selectOptions: _selectOptions.selectOptions,
41
45
  tab: _tab.tab,
@@ -51,9 +55,14 @@ exports.userEventApis = userEventApis;
51
55
  * All APIs returned by this function share an input device state and a default configuration.
52
56
  */
53
57
  function setup(options = {}) {
54
- var _options$document;
55
-
56
- (0, _document.prepareDocument)((_options$document = options.document) != null ? _options$document : document);
58
+ var _options$document, _doc$defaultView;
59
+
60
+ const doc = (_options$document = options.document) != null ? _options$document : document;
61
+ (0, _document.prepareDocument)(doc);
62
+ const view = (_doc$defaultView = doc.defaultView) != null ? _doc$defaultView :
63
+ /* istanbul ignore next */
64
+ window;
65
+ (0, _utils.attachClipboardStubToView)(view);
57
66
  return _setup(options, {
58
67
  keyboardState: (0, _keyboard.createKeyboardState)(),
59
68
  pointerState: (0, _pointer.createPointerState)()
@@ -70,7 +79,14 @@ function _setup({
70
79
  skipAutoClose,
71
80
  skipClick,
72
81
  skipHover,
73
- 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
74
90
  }, {
75
91
  keyboardState,
76
92
  pointerState
@@ -91,6 +107,10 @@ function _setup({
91
107
  const clickDefaults = {
92
108
  skipHover
93
109
  };
110
+ const clipboardDefaults = {
111
+ document,
112
+ writeToClipboard
113
+ };
94
114
  const typeDefaults = {
95
115
  delay,
96
116
  skipAutoClose,
@@ -104,28 +124,42 @@ function _setup({
104
124
  return _clear.clear.call(userEvent, ...args);
105
125
  },
106
126
  click: (...args) => {
107
- args[2] = { ...pointerDefaults,
127
+ args[1] = { ...pointerDefaults,
108
128
  ...clickDefaults,
109
- ...args[2]
129
+ ...args[1]
110
130
  };
111
131
  return _click.click.call(userEvent, ...args);
112
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
+ },
113
147
  dblClick: (...args) => {
114
- args[2] = { ...pointerDefaults,
148
+ args[1] = { ...pointerDefaults,
115
149
  ...clickDefaults,
116
- ...args[2]
150
+ ...args[1]
117
151
  };
118
152
  return _click.dblClick.call(userEvent, ...args);
119
153
  },
120
154
  deselectOptions: (...args) => {
121
- args[3] = { ...pointerDefaults,
122
- ...args[3]
155
+ args[2] = { ...pointerDefaults,
156
+ ...args[2]
123
157
  };
124
158
  return _selectOptions.deselectOptions.call(userEvent, ...args);
125
159
  },
126
160
  hover: (...args) => {
127
- args[2] = { ...pointerDefaults,
128
- ...args[2]
161
+ args[1] = { ...pointerDefaults,
162
+ ...args[1]
129
163
  };
130
164
  return _hover.hover.call(userEvent, ...args);
131
165
  },
@@ -141,8 +175,12 @@ function _setup({
141
175
  return ret.then(() => undefined);
142
176
  }
143
177
  },
178
+ // paste needs typecasting because of the overloading
144
179
  paste: (...args) => {
145
- return _paste.paste.call(userEvent, ...args);
180
+ args[1] = { ...clipboardDefaults,
181
+ ...args[1]
182
+ };
183
+ return _clipboard.paste.call(userEvent, ...args);
146
184
  },
147
185
  // pointer needs typecasting because of the overloading
148
186
  pointer: (...args) => {
@@ -158,8 +196,8 @@ function _setup({
158
196
  }
159
197
  },
160
198
  selectOptions: (...args) => {
161
- args[3] = { ...pointerDefaults,
162
- ...args[3]
199
+ args[2] = { ...pointerDefaults,
200
+ ...args[2]
163
201
  };
164
202
  return _selectOptions.selectOptions.call(userEvent, ...args);
165
203
  },
@@ -187,8 +225,8 @@ function _setup({
187
225
  return _type.type.call(userEvent, ...args);
188
226
  },
189
227
  unhover: (...args) => {
190
- args[2] = { ...pointerDefaults,
191
- ...args[2]
228
+ args[1] = { ...pointerDefaults,
229
+ ...args[1]
192
230
  };
193
231
  return _hover.unhover.call(userEvent, ...args);
194
232
  },
package/dist/upload.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { UserEvent } from './setup';
2
2
  interface uploadInit {
3
- clickInit?: MouseEventInit;
4
3
  changeInit?: EventInit;
5
4
  }
6
5
  export interface uploadOptions {
package/dist/upload.js CHANGED
@@ -23,7 +23,7 @@ function upload(element, fileOrFiles, init, {
23
23
  }
24
24
 
25
25
  if ((0, _utils.isDisabled)(element)) return;
26
- this.click(element, init == null ? void 0 : init.clickInit);
26
+ this.click(element);
27
27
  const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles]).filter(file => !applyAccept || isAcceptableFile(file, input.accept)).slice(0, input.multiple ? undefined : 1); // blur fires when the file selector pops up
28
28
 
29
29
  (0, _utils.blur)(element); // focus fires when they make their selection
@@ -0,0 +1 @@
1
+ export declare function readBlobText(blob: Blob): Promise<string>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.readBlobText = readBlobText;
7
+
8
+ // jsdom does not implement Blob.text()
9
+ function readBlobText(blob) {
10
+ return new Promise((res, rej) => {
11
+ const fr = new FileReader();
12
+ fr.onerror = rej;
13
+ fr.onabort = rej;
14
+
15
+ fr.onload = () => {
16
+ res(String(fr.result));
17
+ };
18
+
19
+ fr.readAsText(blob);
20
+ });
21
+ }
@@ -0,0 +1,9 @@
1
+ export declare function createClipboardItem(...blobs: Array<Blob | string>): ClipboardItem;
2
+ export declare function attachClipboardStubToView(window: Window & typeof globalThis): {
3
+ resetClipboardStub: () => void;
4
+ detachClipboardStub: () => void;
5
+ };
6
+ export declare function resetClipboardStubOnView(window: Window & typeof globalThis): void;
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>;
@@ -0,0 +1,188 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.attachClipboardStubToView = attachClipboardStubToView;
7
+ exports.createClipboardItem = createClipboardItem;
8
+ exports.detachClipboardStubFromView = detachClipboardStubFromView;
9
+ exports.readDataTransferFromClipboard = readDataTransferFromClipboard;
10
+ exports.resetClipboardStubOnView = resetClipboardStubOnView;
11
+ exports.writeDataTransferToClipboard = writeDataTransferToClipboard;
12
+
13
+ var _ = require("..");
14
+
15
+ // Clipboard is not available in jsdom
16
+ class ClipboardItemStub {
17
+ constructor(data) {
18
+ this.data = void 0;
19
+ this.data = data;
20
+ }
21
+
22
+ get types() {
23
+ return Array.from(Object.keys(this.data));
24
+ }
25
+
26
+ async getType(type) {
27
+ const data = await this.data[type];
28
+
29
+ if (!data) {
30
+ throw new Error(`${type} is not one of the available MIME types on this item.`);
31
+ }
32
+
33
+ return data instanceof Blob ? data : new Blob([data], {
34
+ type
35
+ });
36
+ }
37
+
38
+ }
39
+
40
+ const ClipboardStubControl = Symbol('Manage ClipboardSub');
41
+
42
+ class ClipboardStub extends EventTarget {
43
+ constructor(...args) {
44
+ super(...args);
45
+ this.items = [];
46
+ this[ClipboardStubControl] = void 0;
47
+ }
48
+
49
+ async read() {
50
+ return Array.from(this.items);
51
+ }
52
+
53
+ async readText() {
54
+ let text = '';
55
+
56
+ for (const item of this.items) {
57
+ const type = item.types.includes('text/plain') ? 'text/plain' : item.types.find(t => t.startsWith('text/'));
58
+
59
+ if (type) {
60
+ text += await item.getType(type).then(b => (0, _.readBlobText)(b));
61
+ }
62
+ }
63
+
64
+ return text;
65
+ }
66
+
67
+ async write(data) {
68
+ this.items = data;
69
+ }
70
+
71
+ async writeText(text) {
72
+ this.items = [createClipboardItem(text)];
73
+ }
74
+
75
+ } // MDN lists string|Blob|Promise<Blob|string> as possible types in ClipboardItemData
76
+ // lib.dom.d.ts lists only Promise<Blob|string>
77
+ // https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem/ClipboardItem#syntax
78
+
79
+
80
+ function createClipboardItem(...blobs) {
81
+ // use real ClipboardItem if available
82
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
83
+ const constructor = typeof ClipboardItem === 'undefined' ? ClipboardItemStub :
84
+ /* istanbul ignore next */
85
+ ClipboardItem;
86
+ return new constructor(Object.fromEntries(blobs.map(b => [typeof b === 'string' ? 'text/plain' : b.type, Promise.resolve(b)])));
87
+ }
88
+
89
+ function attachClipboardStubToView(window) {
90
+ if (window.navigator.clipboard instanceof ClipboardStub) {
91
+ return window.navigator.clipboard[ClipboardStubControl];
92
+ }
93
+
94
+ const realClipboard = Object.getOwnPropertyDescriptor(window.navigator, 'clipboard');
95
+ let stub = new ClipboardStub();
96
+ const control = {
97
+ resetClipboardStub: () => {
98
+ stub = new ClipboardStub();
99
+ stub[ClipboardStubControl] = control;
100
+ },
101
+ detachClipboardStub: () => {
102
+ /* istanbul ignore if */
103
+ if (realClipboard) {
104
+ Object.defineProperty(window.navigator, 'clipboard', realClipboard);
105
+ } else {
106
+ Object.defineProperty(window.navigator, 'clipboard', {
107
+ value: undefined,
108
+ configurable: true
109
+ });
110
+ }
111
+ }
112
+ };
113
+ stub[ClipboardStubControl] = control;
114
+ Object.defineProperty(window.navigator, 'clipboard', {
115
+ get: () => stub,
116
+ configurable: true
117
+ });
118
+ return stub[ClipboardStubControl];
119
+ }
120
+
121
+ function resetClipboardStubOnView(window) {
122
+ if (window.navigator.clipboard instanceof ClipboardStub) {
123
+ window.navigator.clipboard[ClipboardStubControl].resetClipboardStub();
124
+ }
125
+ }
126
+
127
+ function detachClipboardStubFromView(window) {
128
+ if (window.navigator.clipboard instanceof ClipboardStub) {
129
+ window.navigator.clipboard[ClipboardStubControl].detachClipboardStub();
130
+ }
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
+ }
175
+ /* istanbul ignore else */
176
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
177
+
178
+
179
+ if (afterEach) {
180
+ afterEach(() => resetClipboardStubOnView(window));
181
+ }
182
+ /* istanbul ignore else */
183
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
184
+
185
+
186
+ if (afterAll) {
187
+ afterAll(() => detachClipboardStubFromView(window));
188
+ }
@@ -0,0 +1,2 @@
1
+ export declare function createDataTransfer(files?: File[]): DataTransfer;
2
+ export declare function getBlobFromDataTransferItem(item: DataTransferItem): Blob;