@testing-library/user-event
Advanced tools
Comparing version 14.0.0-alpha.14 to 14.0.0-alpha.15
@@ -1,11 +0,2 @@ | ||
import type { UserEvent } from '../setup'; | ||
export interface copyOptions { | ||
document?: Document; | ||
writeToClipboard?: boolean; | ||
} | ||
export declare function copy(this: UserEvent, options: Omit<copyOptions, 'writeToClipboard'> & { | ||
writeToClipboard: true; | ||
}): Promise<DataTransfer>; | ||
export declare function copy(this: UserEvent, options?: Omit<copyOptions, 'writeToClipboard'> & { | ||
writeToClipboard?: boolean; | ||
}): DataTransfer; | ||
import { UserEvent } from '../setup'; | ||
export declare function copy(this: UserEvent): Promise<DataTransfer | undefined>; |
@@ -10,8 +10,10 @@ "use strict"; | ||
var _setup = require("../setup"); | ||
var _utils = require("../utils"); | ||
function copy(options) { | ||
var _options$document, _doc$activeElement; | ||
async function copy() { | ||
var _doc$activeElement; | ||
const doc = (_options$document = options == null ? void 0 : options.document) != null ? _options$document : document; | ||
const doc = this[_setup.Config].document; | ||
const target = (_doc$activeElement = doc.activeElement) != null ? _doc$activeElement : | ||
@@ -30,3 +32,7 @@ /* istanbul ignore next */ | ||
return options != null && options.writeToClipboard ? (0, _utils.writeDataTransferToClipboard)(doc, clipboardData).then(() => clipboardData) : clipboardData; | ||
if (this[_setup.Config].writeToClipboard) { | ||
await (0, _utils.writeDataTransferToClipboard)(doc, clipboardData); | ||
} | ||
return clipboardData; | ||
} |
@@ -1,11 +0,2 @@ | ||
import type { UserEvent } from '../setup'; | ||
export interface cutOptions { | ||
document?: Document; | ||
writeToClipboard?: boolean; | ||
} | ||
export declare function cut(this: UserEvent, options: Omit<cutOptions, 'writeToClipboard'> & { | ||
writeToClipboard: true; | ||
}): Promise<DataTransfer>; | ||
export declare function cut(this: UserEvent, options?: Omit<cutOptions, 'writeToClipboard'> & { | ||
writeToClipboard?: boolean; | ||
}): DataTransfer; | ||
import { UserEvent } from '../setup'; | ||
export declare function cut(this: UserEvent): Promise<DataTransfer | undefined>; |
@@ -10,8 +10,10 @@ "use strict"; | ||
var _setup = require("../setup"); | ||
var _utils = require("../utils"); | ||
function cut(options) { | ||
var _options$document, _doc$activeElement; | ||
async function cut() { | ||
var _doc$activeElement; | ||
const doc = (_options$document = options == null ? void 0 : options.document) != null ? _options$document : document; | ||
const doc = this[_setup.Config].document; | ||
const target = (_doc$activeElement = doc.activeElement) != null ? _doc$activeElement : | ||
@@ -36,3 +38,7 @@ /* istanbul ignore next */ | ||
return options != null && options.writeToClipboard ? (0, _utils.writeDataTransferToClipboard)(doc, clipboardData).then(() => clipboardData) : clipboardData; | ||
if (this[_setup.Config].writeToClipboard) { | ||
await (0, _utils.writeDataTransferToClipboard)(doc, clipboardData); | ||
} | ||
return clipboardData; | ||
} |
@@ -1,6 +0,2 @@ | ||
import type { UserEvent } from '../setup'; | ||
export interface pasteOptions { | ||
document?: Document; | ||
} | ||
export declare function paste(this: UserEvent, clipboardData?: undefined, options?: pasteOptions): Promise<void>; | ||
export declare function paste(this: UserEvent, clipboardData: DataTransfer | string, options?: pasteOptions): void; | ||
import { UserEvent } from '../setup'; | ||
export declare function paste(this: UserEvent, clipboardData?: DataTransfer | string): Promise<void>; |
@@ -10,15 +10,17 @@ "use strict"; | ||
var _setup = require("../setup"); | ||
var _utils = require("../utils"); | ||
function paste(clipboardData, options) { | ||
var _options$document, _doc$activeElement; | ||
async function paste(clipboardData) { | ||
var _doc$activeElement, _ref; | ||
const doc = (_options$document = options == null ? void 0 : options.document) != null ? _options$document : document; | ||
const doc = this[_setup.Config].document; | ||
const target = (_doc$activeElement = doc.activeElement) != null ? _doc$activeElement : | ||
/* istanbul ignore next */ | ||
doc.body; | ||
const data = typeof clipboardData === 'string' ? getClipboardDataFromString(clipboardData) : clipboardData; | ||
return data ? pasteImpl(target, data) : (0, _utils.readDataTransferFromClipboard)(doc).then(dt => pasteImpl(target, dt), () => { | ||
const data = (_ref = typeof clipboardData === 'string' ? getClipboardDataFromString(clipboardData) : clipboardData) != null ? _ref : await (0, _utils.readDataTransferFromClipboard)(doc).catch(() => { | ||
throw new Error('`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.'); | ||
}); | ||
return pasteImpl(target, data); | ||
} | ||
@@ -25,0 +27,0 @@ |
@@ -1,6 +0,3 @@ | ||
import { UserEventApis, setup } from './setup'; | ||
declare const userEvent: UserEventApis & { | ||
setup: typeof setup; | ||
}; | ||
export default userEvent; | ||
export { userEvent as default } from './setup'; | ||
export type { keyboardKey } from './keyboard'; | ||
export type { pointerKey } from './pointer'; |
@@ -6,10 +6,9 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
Object.defineProperty(exports, "default", { | ||
enumerable: true, | ||
get: function () { | ||
return _setup.userEvent; | ||
} | ||
}); | ||
var _setup = require("./setup"); | ||
const userEvent = { ...Object.fromEntries(Object.entries(_setup.userEventApis).map(([k, f]) => [k, (...a) => f.apply(userEvent, a)])), | ||
setup: _setup.setup | ||
}; | ||
var _default = userEvent; | ||
exports.default = _default; | ||
var _setup = require("./setup"); |
@@ -1,2 +0,2 @@ | ||
import { keyboardKey, keyboardOptions } from './types'; | ||
import { keyboardKey } from './types'; | ||
/** | ||
@@ -12,3 +12,3 @@ * Get the next key from keyMap | ||
*/ | ||
export declare function getNextKeyDef(text: string, options: keyboardOptions): { | ||
export declare function getNextKeyDef(keyboardMap: keyboardKey[], text: string): { | ||
keyDef: keyboardKey; | ||
@@ -15,0 +15,0 @@ consumedLength: number; |
@@ -20,4 +20,4 @@ "use strict"; | ||
*/ | ||
function getNextKeyDef(text, options) { | ||
var _options$keyboardMap$; | ||
function getNextKeyDef(keyboardMap, text) { | ||
var _keyboardMap$find; | ||
@@ -32,3 +32,3 @@ const { | ||
} = (0, _utils.readNextDescriptor)(text); | ||
const keyDef = (_options$keyboardMap$ = options.keyboardMap.find(def => { | ||
const keyDef = (_keyboardMap$find = keyboardMap.find(def => { | ||
if (type === '[') { | ||
@@ -45,3 +45,3 @@ var _def$code; | ||
return def.key === descriptor; | ||
})) != null ? _options$keyboardMap$ : { | ||
})) != null ? _keyboardMap$find : { | ||
key: 'Unknown', | ||
@@ -48,0 +48,0 @@ code: 'Unknown', |
@@ -1,18 +0,7 @@ | ||
import { keyboardState, keyboardOptions, keyboardKey } from './types'; | ||
export type { keyboardOptions, keyboardKey }; | ||
export declare function keyboard(text: string, options?: Partial<keyboardOptions & { | ||
keyboardState: keyboardState; | ||
delay: 0; | ||
}>): keyboardState; | ||
export declare function keyboard(text: string, options: Partial<keyboardOptions & { | ||
keyboardState: keyboardState; | ||
delay: number; | ||
}>): Promise<keyboardState>; | ||
export declare function keyboardImplementationWrapper(text: string, config?: Partial<keyboardOptions & { | ||
keyboardState: keyboardState; | ||
}>): { | ||
promise: Promise<void>; | ||
state: keyboardState; | ||
releaseAllKeys: () => void; | ||
}; | ||
import { UserEvent } from '../setup'; | ||
import { releaseAllKeys } from './keyboardImplementation'; | ||
import type { keyboardState, keyboardKey } from './types'; | ||
export { releaseAllKeys }; | ||
export type { keyboardKey, keyboardState }; | ||
export declare function keyboard(this: UserEvent, text: string): Promise<void>; | ||
export declare function createKeyboardState(): keyboardState; |
@@ -8,51 +8,17 @@ "use strict"; | ||
exports.keyboard = keyboard; | ||
exports.keyboardImplementationWrapper = keyboardImplementationWrapper; | ||
Object.defineProperty(exports, "releaseAllKeys", { | ||
enumerable: true, | ||
get: function () { | ||
return _keyboardImplementation.releaseAllKeys; | ||
} | ||
}); | ||
var _dom = require("@testing-library/dom"); | ||
var _setup = require("../setup"); | ||
var _document = require("../document"); | ||
var _keyboardImplementation = require("./keyboardImplementation"); | ||
var _keyMap = require("./keyMap"); | ||
function keyboard(text, options) { | ||
var _options$delay; | ||
const { | ||
promise, | ||
state | ||
} = keyboardImplementationWrapper(text, options); | ||
if (((_options$delay = options == null ? void 0 : options.delay) != null ? _options$delay : 0) > 0) { | ||
return (0, _dom.getConfig)().asyncWrapper(() => promise.then(() => state)); | ||
} else { | ||
// prevent users from dealing with UnhandledPromiseRejectionWarning in sync call | ||
promise.catch(console.error); | ||
return state; | ||
} | ||
async function keyboard(text) { | ||
return (0, _keyboardImplementation.keyboardImplementation)(this[_setup.Config], text); | ||
} | ||
function keyboardImplementationWrapper(text, config = {}) { | ||
const { | ||
keyboardState: state = createKeyboardState(), | ||
delay = 0, | ||
document: doc = document, | ||
autoModify = false, | ||
keyboardMap = _keyMap.defaultKeyMap | ||
} = config; | ||
const options = { | ||
delay, | ||
document: doc, | ||
autoModify, | ||
keyboardMap | ||
}; | ||
(0, _document.prepareDocument)(document); | ||
return { | ||
promise: (0, _keyboardImplementation.keyboardImplementation)(text, options, state), | ||
state, | ||
releaseAllKeys: () => (0, _keyboardImplementation.releaseAllKeys)(options, state) | ||
}; | ||
} | ||
function createKeyboardState() { | ||
@@ -59,0 +25,0 @@ return { |
@@ -1,3 +0,3 @@ | ||
import { keyboardState, keyboardOptions } from './types'; | ||
export declare function keyboardImplementation(text: string, options: keyboardOptions, state: keyboardState): Promise<void>; | ||
export declare function releaseAllKeys(options: keyboardOptions, state: keyboardState): void; | ||
import { Config } from '../setup'; | ||
export declare function keyboardImplementation(config: Config, text: string): Promise<void>; | ||
export declare function releaseAllKeys(config: Config): Promise<void>; |
@@ -23,8 +23,11 @@ "use strict"; | ||
async function keyboardImplementation(text, options, state) { | ||
var _state$repeatKey; | ||
async function keyboardImplementation(config, text) { | ||
var _keyboardState$repeat; | ||
const { | ||
document | ||
} = options; | ||
document, | ||
keyboardState, | ||
keyboardMap, | ||
delay | ||
} = config; | ||
@@ -39,15 +42,15 @@ const getCurrentElement = () => getActive(document); | ||
repeat | ||
} = (_state$repeatKey = state.repeatKey) != null ? _state$repeatKey : (0, _getNextKeyDef.getNextKeyDef)(text, options); | ||
const pressed = state.pressed.find(p => p.keyDef === keyDef); // Release the key automatically if it was pressed before. | ||
} = (_keyboardState$repeat = keyboardState.repeatKey) != null ? _keyboardState$repeat : (0, _getNextKeyDef.getNextKeyDef)(keyboardMap, text); | ||
const pressed = keyboardState.pressed.find(p => p.keyDef === keyDef); // Release the key automatically if it was pressed before. | ||
// Do not release the key on iterations on `state.repeatKey`. | ||
if (pressed && !state.repeatKey) { | ||
keyup(keyDef, getCurrentElement, options, state, pressed.unpreventedDefault); | ||
if (pressed && !keyboardState.repeatKey) { | ||
await keyup(keyDef, getCurrentElement, config, pressed.unpreventedDefault); | ||
} | ||
if (!releasePrevious) { | ||
const unpreventedDefault = keydown(keyDef, getCurrentElement, options, state); | ||
const unpreventedDefault = await keydown(keyDef, getCurrentElement, config); | ||
if (unpreventedDefault && hasKeyPress(keyDef, state)) { | ||
keypress(keyDef, getCurrentElement, options, state); | ||
if (unpreventedDefault && hasKeyPress(keyDef, config)) { | ||
await keypress(keyDef, getCurrentElement, config); | ||
} // Release the key only on the last iteration on `state.repeatKey`. | ||
@@ -57,3 +60,3 @@ | ||
if (releaseSelf && repeat <= 1) { | ||
keyup(keyDef, getCurrentElement, options, state, unpreventedDefault); | ||
await keyup(keyDef, getCurrentElement, config, unpreventedDefault); | ||
} | ||
@@ -63,3 +66,3 @@ } | ||
if (repeat > 1) { | ||
state.repeatKey = { | ||
keyboardState.repeatKey = { | ||
// don't consume again on the next iteration | ||
@@ -73,11 +76,11 @@ consumedLength: 0, | ||
} else { | ||
delete state.repeatKey; | ||
delete keyboardState.repeatKey; | ||
} | ||
if (text.length > consumedLength || repeat > 1) { | ||
if (options.delay > 0) { | ||
await (0, _utils.wait)(options.delay); | ||
if (typeof delay === 'number') { | ||
await (0, _utils.wait)(delay); | ||
} | ||
return keyboardImplementation(text.slice(consumedLength), options, state); | ||
return keyboardImplementation(config, text.slice(consumedLength)); | ||
} | ||
@@ -96,24 +99,24 @@ | ||
function releaseAllKeys(options, state) { | ||
const getCurrentElement = () => getActive(options.document); | ||
async function releaseAllKeys(config) { | ||
const getCurrentElement = () => getActive(config.document); | ||
for (const k of state.pressed) { | ||
keyup(k.keyDef, getCurrentElement, options, state, k.unpreventedDefault); | ||
for (const k of config.keyboardState.pressed) { | ||
await keyup(k.keyDef, getCurrentElement, config, k.unpreventedDefault); | ||
} | ||
} | ||
function keydown(keyDef, getCurrentElement, options, state) { | ||
async function keydown(keyDef, getCurrentElement, config) { | ||
const element = getCurrentElement(); // clear carried characters when focus is moved | ||
if (element !== state.activeElement) { | ||
state.carryValue = undefined; | ||
state.carryChar = ''; | ||
if (element !== config.keyboardState.activeElement) { | ||
config.keyboardState.carryValue = undefined; | ||
config.keyboardState.carryChar = ''; | ||
} | ||
state.activeElement = element; | ||
applyPlugins(plugins.preKeydownBehavior, keyDef, element, options, state); | ||
config.keyboardState.activeElement = element; | ||
applyPlugins(plugins.preKeydownBehavior, keyDef, element, config); | ||
const unpreventedDefault = _dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(keyDef, state)); | ||
const unpreventedDefault = _dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState)); | ||
state.pressed.push({ | ||
config.keyboardState.pressed.push({ | ||
keyDef, | ||
@@ -125,3 +128,3 @@ unpreventedDefault | ||
// all default behavior like keypress/submit etc is applied to the currentElement | ||
applyPlugins(plugins.keydownBehavior, keyDef, getCurrentElement(), options, state); | ||
applyPlugins(plugins.keydownBehavior, keyDef, getCurrentElement(), config); | ||
} | ||
@@ -132,6 +135,6 @@ | ||
function keypress(keyDef, getCurrentElement, options, state) { | ||
async function keypress(keyDef, getCurrentElement, config) { | ||
const element = getCurrentElement(); | ||
const unpreventedDefault = _dom.fireEvent.keyPress(element, { ...(0, _getEventProps.getKeyEventProps)(keyDef, state), | ||
const unpreventedDefault = _dom.fireEvent.keyPress(element, { ...(0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState), | ||
charCode: keyDef.key === 'Enter' ? 13 : String(keyDef.key).charCodeAt(0) | ||
@@ -141,25 +144,25 @@ }); | ||
if (unpreventedDefault) { | ||
applyPlugins(plugins.keypressBehavior, keyDef, getCurrentElement(), options, state); | ||
applyPlugins(plugins.keypressBehavior, keyDef, getCurrentElement(), config); | ||
} | ||
} | ||
function keyup(keyDef, getCurrentElement, options, state, unprevented) { | ||
async function keyup(keyDef, getCurrentElement, config, unprevented) { | ||
const element = getCurrentElement(); | ||
applyPlugins(plugins.preKeyupBehavior, keyDef, element, options, state); | ||
applyPlugins(plugins.preKeyupBehavior, keyDef, element, config); | ||
const unpreventedDefault = _dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(keyDef, state)); | ||
const unpreventedDefault = _dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(keyDef, config.keyboardState)); | ||
if (unprevented && unpreventedDefault) { | ||
applyPlugins(plugins.keyupBehavior, keyDef, getCurrentElement(), options, state); | ||
applyPlugins(plugins.keyupBehavior, keyDef, getCurrentElement(), config); | ||
} | ||
state.pressed = state.pressed.filter(k => k.keyDef !== keyDef); | ||
applyPlugins(plugins.postKeyupBehavior, keyDef, element, options, state); | ||
config.keyboardState.pressed = config.keyboardState.pressed.filter(k => k.keyDef !== keyDef); | ||
applyPlugins(plugins.postKeyupBehavior, keyDef, element, config); | ||
} | ||
function applyPlugins(pluginCollection, keyDef, element, options, state) { | ||
const plugin = pluginCollection.find(p => p.matches(keyDef, element, options, state)); | ||
function applyPlugins(pluginCollection, keyDef, element, config) { | ||
const plugin = pluginCollection.find(p => p.matches(keyDef, element, config)); | ||
if (plugin) { | ||
plugin.handle(keyDef, element, options, state); | ||
plugin.handle(keyDef, element, config); | ||
} | ||
@@ -170,6 +173,6 @@ | ||
function hasKeyPress(keyDef, state) { | ||
function hasKeyPress(keyDef, config) { | ||
var _keyDef$key; | ||
return (((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 || keyDef.key === 'Enter') && !state.modifiers.ctrl && !state.modifiers.alt; | ||
return (((_keyDef$key = keyDef.key) == null ? void 0 : _keyDef$key.length) === 1 || keyDef.key === 'Enter') && !config.keyboardState.modifiers.ctrl && !config.keyboardState.modifiers.alt; | ||
} |
@@ -26,7 +26,9 @@ "use strict"; | ||
}, | ||
handle: (keyDef, element, options, state) => { | ||
var _state$carryValue; | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
var _keyboardState$carryV; | ||
let newEntry = keyDef.key; | ||
const textToBeTyped = ((_state$carryValue = state.carryValue) != null ? _state$carryValue : '') + newEntry; | ||
const textToBeTyped = ((_keyboardState$carryV = keyboardState.carryValue) != null ? _keyboardState$carryV : '') + newEntry; | ||
const timeNewEntry = (0, _utils.buildTimeValue)(textToBeTyped); | ||
@@ -60,3 +62,3 @@ | ||
(0, _shared.fireChangeForInputTimeIfValid)(element, prevValue, timeNewEntry); | ||
state.carryValue = textToBeTyped; | ||
keyboardState.carryValue = textToBeTyped; | ||
} | ||
@@ -72,7 +74,9 @@ }, { | ||
}, | ||
handle: (keyDef, element, options, state) => { | ||
var _state$carryValue2; | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
var _keyboardState$carryV2; | ||
let newEntry = keyDef.key; | ||
const textToBeTyped = ((_state$carryValue2 = state.carryValue) != null ? _state$carryValue2 : '') + newEntry; | ||
const textToBeTyped = ((_keyboardState$carryV2 = keyboardState.carryValue) != null ? _keyboardState$carryV2 : '') + newEntry; | ||
const isValidToBeTyped = (0, _utils.isValidDateValue)(element, textToBeTyped); | ||
@@ -113,3 +117,3 @@ | ||
state.carryValue = textToBeTyped; | ||
keyboardState.carryValue = textToBeTyped; | ||
} | ||
@@ -165,8 +169,10 @@ }, { | ||
}) || (0, _utils.isContentEditable)(element)) && (0, _utils.getSpaceUntilMaxLength)(element) !== 0, | ||
handle: (keyDef, element, options, state) => { | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
var _prepareInput2; | ||
(_prepareInput2 = (0, _utils.prepareInput)('\n', element, (0, _utils.isContentEditable)(element) && !state.modifiers.shift ? 'insertParagraph' : 'insertLineBreak')) == null ? void 0 : _prepareInput2.commit(); | ||
(_prepareInput2 = (0, _utils.prepareInput)('\n', element, (0, _utils.isContentEditable)(element) && !keyboardState.modifiers.shift ? 'insertParagraph' : 'insertLineBreak')) == null ? void 0 : _prepareInput2.commit(); | ||
} | ||
}]; | ||
exports.keypressBehavior = keypressBehavior; |
@@ -14,5 +14,7 @@ "use strict"; | ||
const keydownBehavior = [{ | ||
matches: (keyDef, element, options, state) => keyDef.code === 'KeyA' && state.modifiers.ctrl, | ||
matches: (keyDef, element, { | ||
keyboardState | ||
}) => keyDef.code === 'KeyA' && keyboardState.modifiers.ctrl, | ||
handle: (keyDef, element) => (0, _utils.selectAll)(element) | ||
}]; | ||
exports.keydownBehavior = keydownBehavior; |
@@ -29,4 +29,6 @@ "use strict"; | ||
matches: keyDef => keyDef.key === key, | ||
handle: (keyDef, element, options, state) => { | ||
state.modifiers[modKey] = true; | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
keyboardState.modifiers[modKey] = true; | ||
} | ||
@@ -37,6 +39,9 @@ })), // AltGraph produces an extra keydown for Control | ||
matches: keyDef => keyDef.key === 'AltGraph', | ||
handle: (keyDef, element, options, state) => { | ||
var _options$keyboardMap$; | ||
handle: (keyDef, element, { | ||
keyboardMap, | ||
keyboardState | ||
}) => { | ||
var _keyboardMap$find; | ||
const ctrlKeyDef = (_options$keyboardMap$ = options.keyboardMap.find(k => k.key === 'Control')) != null ? _options$keyboardMap$ : | ||
const ctrlKeyDef = (_keyboardMap$find = keyboardMap.find(k => k.key === 'Control')) != null ? _keyboardMap$find : | ||
/* istanbul ignore next */ | ||
@@ -48,3 +53,3 @@ { | ||
_dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, state)); | ||
_dom.fireEvent.keyDown(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, keyboardState)); | ||
} | ||
@@ -55,4 +60,6 @@ }]; | ||
matches: keyDef => keyDef.key === 'CapsLock', | ||
handle: (keyDef, element, options, state) => { | ||
state.modifiers.caps = !state.modifiers.caps; | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
keyboardState.modifiers.caps = !keyboardState.modifiers.caps; | ||
} | ||
@@ -68,4 +75,6 @@ }, { | ||
matches: keyDef => keyDef.key === 'Tab', | ||
handle: (keyDef, element, options, state) => { | ||
const dest = (0, _utils.getTabDestination)(element, state.modifiers.shift); | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
const dest = (0, _utils.getTabDestination)(element, keyboardState.modifiers.shift); | ||
@@ -99,4 +108,6 @@ if (dest === element.ownerDocument.body) { | ||
(0, _utils.isElementType)(element, 'a') && Boolean(element.href)), | ||
handle: (keyDef, element, options, state) => { | ||
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(state)); | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(keyboardState)); | ||
} | ||
@@ -117,4 +128,6 @@ }, { | ||
matches: keyDef => keyDef.key === key, | ||
handle: (keyDef, element, options, state) => { | ||
state.modifiers[modKey] = false; | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
keyboardState.modifiers[modKey] = false; | ||
} | ||
@@ -125,4 +138,6 @@ }))]; | ||
matches: (keyDef, element) => keyDef.key === ' ' && (0, _utils.isClickableInput)(element), | ||
handle: (keyDef, element, options, state) => { | ||
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(state)); | ||
handle: (keyDef, element, { | ||
keyboardState | ||
}) => { | ||
_dom.fireEvent.click(element, (0, _getEventProps.getMouseEventProps)(keyboardState)); | ||
} | ||
@@ -135,6 +150,9 @@ }]; | ||
matches: keyDef => keyDef.key === 'AltGraph', | ||
handle: (keyDef, element, options, state) => { | ||
var _options$keyboardMap$2; | ||
handle: (keyDef, element, { | ||
keyboardMap, | ||
keyboardState | ||
}) => { | ||
var _keyboardMap$find2; | ||
const ctrlKeyDef = (_options$keyboardMap$2 = options.keyboardMap.find(k => k.key === 'Control')) != null ? _options$keyboardMap$2 : | ||
const ctrlKeyDef = (_keyboardMap$find2 = keyboardMap.find(k => k.key === 'Control')) != null ? _keyboardMap$find2 : | ||
/* istanbul ignore next */ | ||
@@ -146,5 +164,5 @@ { | ||
_dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, state)); | ||
_dom.fireEvent.keyUp(element, (0, _getEventProps.getKeyEventProps)(ctrlKeyDef, keyboardState)); | ||
} | ||
}]; | ||
exports.postKeyupBehavior = postKeyupBehavior; |
@@ -0,1 +1,2 @@ | ||
import { Config } from '../setup'; | ||
import { getNextKeyDef } from './getNextKeyDef'; | ||
@@ -47,12 +48,2 @@ /** | ||
}; | ||
export declare type keyboardOptions = { | ||
/** Document in which to perform the events */ | ||
document: Document; | ||
/** Delay between keystrokes */ | ||
delay: number; | ||
/** Add modifiers for given keys - not implemented yet */ | ||
autoModify: boolean; | ||
/** Keyboard layout to use */ | ||
keyboardMap: keyboardKey[]; | ||
}; | ||
export declare enum DOM_KEY_LOCATION { | ||
@@ -77,4 +68,4 @@ STANDARD = 0, | ||
export interface behaviorPlugin { | ||
matches: (keyDef: keyboardKey, element: Element, options: keyboardOptions, state: keyboardState) => boolean; | ||
handle: (keyDef: keyboardKey, element: Element, options: keyboardOptions, state: keyboardState) => void; | ||
matches: (keyDef: keyboardKey, element: Element, config: Config) => boolean; | ||
handle: (keyDef: keyboardKey, element: Element, config: Config) => void; | ||
} |
@@ -0,18 +1,10 @@ | ||
import { UserEvent } from '../setup'; | ||
import { PointerAction, PointerActionTarget } from './pointerAction'; | ||
import type { inputDeviceState, pointerOptions, pointerState } from './types'; | ||
export declare function pointer(input: PointerInput, options?: Partial<pointerOptions & { | ||
delay: 0; | ||
} & inputDeviceState>): pointerState; | ||
export declare function pointer(input: PointerInput, options: Partial<pointerOptions & { | ||
delay: number; | ||
} & inputDeviceState>): Promise<pointerState>; | ||
import type { pointerState, pointerKey } from './types'; | ||
export type { pointerState, pointerKey }; | ||
declare type PointerActionInput = string | ({ | ||
keys: string; | ||
} & PointerActionTarget) | PointerAction; | ||
declare type PointerInput = PointerActionInput | Array<PointerActionInput>; | ||
export declare function pointerImplementationWrapper(input: PointerInput, config: Partial<pointerOptions & inputDeviceState>): { | ||
promise: Promise<unknown[]>; | ||
pointerState: pointerState; | ||
}; | ||
export declare function createPointerState(): pointerState; | ||
export {}; | ||
export declare type PointerInput = PointerActionInput | Array<PointerActionInput>; | ||
export declare function pointer(this: UserEvent, input: PointerInput): Promise<void>; | ||
export declare function createPointerState(document: Document): pointerState; |
@@ -8,48 +8,19 @@ "use strict"; | ||
exports.pointer = pointer; | ||
exports.pointerImplementationWrapper = pointerImplementationWrapper; | ||
var _dom = require("@testing-library/dom"); | ||
var _setup = require("../setup"); | ||
var _keyboard = require("../keyboard"); | ||
var _parseKeyDef = require("./parseKeyDef"); | ||
var _keyMap = require("./keyMap"); | ||
var _pointerAction = require("./pointerAction"); | ||
function pointer(input, options = {}) { | ||
var _options$delay; | ||
async function pointer(input) { | ||
const { | ||
promise, | ||
pointerState | ||
} = pointerImplementationWrapper(input, options); | ||
if (((_options$delay = options.delay) != null ? _options$delay : 0) > 0) { | ||
return (0, _dom.getConfig)().asyncWrapper(() => promise.then(() => pointerState)); | ||
} else { | ||
// prevent users from dealing with UnhandledPromiseRejectionWarning in sync call | ||
promise.catch(console.error); | ||
return pointerState; | ||
} | ||
} | ||
function pointerImplementationWrapper(input, config) { | ||
const { | ||
pointerState = createPointerState(), | ||
keyboardState = (0, _keyboard.createKeyboardState)(), | ||
delay = 0, | ||
pointerMap = _keyMap.defaultKeyMap | ||
} = config; | ||
const options = { | ||
delay, | ||
pointerMap | ||
}; | ||
} = this[_setup.Config]; | ||
const actions = []; | ||
(Array.isArray(input) ? input : [input]).forEach(actionInput => { | ||
if (typeof actionInput === 'string') { | ||
actions.push(...(0, _parseKeyDef.parseKeyDef)(actionInput, options)); | ||
actions.push(...(0, _parseKeyDef.parseKeyDef)(pointerMap, actionInput)); | ||
} else if ('keys' in actionInput) { | ||
actions.push(...(0, _parseKeyDef.parseKeyDef)(actionInput.keys, options).map(i => ({ ...actionInput, | ||
actions.push(...(0, _parseKeyDef.parseKeyDef)(pointerMap, actionInput.keys).map(i => ({ ...actionInput, | ||
...i | ||
@@ -61,12 +32,6 @@ }))); | ||
}); | ||
return { | ||
promise: (0, _pointerAction.pointerAction)(actions, options, { | ||
pointerState, | ||
keyboardState | ||
}), | ||
pointerState | ||
}; | ||
return (0, _pointerAction.pointerAction)(this[_setup.Config], actions).then(() => undefined); | ||
} | ||
function createPointerState() { | ||
function createPointerState(document) { | ||
return { | ||
@@ -78,2 +43,3 @@ pointerId: 1, | ||
pointerId: 1, | ||
target: document.body, | ||
coords: { | ||
@@ -80,0 +46,0 @@ clientX: 0, |
@@ -1,3 +0,3 @@ | ||
import { pointerKey, pointerOptions } from './types'; | ||
export declare function parseKeyDef(keys: string, { pointerMap }: pointerOptions): { | ||
import { pointerKey } from './types'; | ||
export declare function parseKeyDef(pointerMap: pointerKey[], keys: string): { | ||
keyDef: pointerKey; | ||
@@ -4,0 +4,0 @@ releasePrevious: boolean; |
@@ -10,5 +10,3 @@ "use strict"; | ||
function parseKeyDef(keys, { | ||
pointerMap | ||
}) { | ||
function parseKeyDef(pointerMap, keys) { | ||
const defs = []; | ||
@@ -15,0 +13,0 @@ |
@@ -0,6 +1,7 @@ | ||
import { Config } from '../setup'; | ||
import { PointerMoveAction } from './pointerMove'; | ||
import { PointerPressAction } from './pointerPress'; | ||
import { inputDeviceState, pointerOptions, PointerTarget, SelectionTarget } from './types'; | ||
import { PointerTarget, SelectionTarget } from './types'; | ||
export declare type PointerActionTarget = Partial<PointerTarget> & Partial<SelectionTarget>; | ||
export declare type PointerAction = PointerActionTarget & (Omit<PointerPressAction, 'target' | 'coords'> | Omit<PointerMoveAction, 'target' | 'coords'>); | ||
export declare function pointerAction(actions: PointerAction[], options: pointerOptions, state: inputDeviceState): Promise<unknown[]>; | ||
export declare function pointerAction(config: Config, actions: PointerAction[]): Promise<void>; |
@@ -14,5 +14,3 @@ "use strict"; | ||
async function pointerAction(actions, options, state) { | ||
const ret = []; | ||
async function pointerAction(config, actions) { | ||
for (let i = 0; i < actions.length; i++) { | ||
@@ -23,18 +21,15 @@ var _action$target, _action$coords; | ||
const pointerName = 'pointerName' in action && action.pointerName ? action.pointerName : 'keyDef' in action ? action.keyDef.pointerType === 'touch' ? action.keyDef.name : action.keyDef.pointerType : 'mouse'; | ||
const target = (_action$target = action.target) != null ? _action$target : getPrevTarget(pointerName, state.pointerState); | ||
const coords = (_action$coords = action.coords) != null ? _action$coords : pointerName in state.pointerState.position ? state.pointerState.position[pointerName].coords : undefined; | ||
const promise = 'keyDef' in action ? (0, _pointerPress.pointerPress)({ ...action, | ||
const target = (_action$target = action.target) != null ? _action$target : getPrevTarget(pointerName, config.pointerState); | ||
const coords = (_action$coords = action.coords) != null ? _action$coords : pointerName in config.pointerState.position ? config.pointerState.position[pointerName].coords : undefined; | ||
await ('keyDef' in action ? (0, _pointerPress.pointerPress)({ ...action, | ||
target, | ||
coords | ||
}, state) : (0, _pointerMove.pointerMove)({ ...action, | ||
}, config) : (0, _pointerMove.pointerMove)({ ...action, | ||
target, | ||
coords | ||
}, state); | ||
ret.push(promise); | ||
}, config)); | ||
if (options.delay > 0) { | ||
await promise; | ||
if (typeof config.delay === 'number') { | ||
if (i < actions.length - 1) { | ||
await (0, _utils.wait)(options.delay); | ||
await (0, _utils.wait)(config.delay); | ||
} | ||
@@ -44,4 +39,3 @@ } | ||
delete state.pointerState.activeClickCount; | ||
return Promise.all(ret); | ||
delete config.pointerState.activeClickCount; | ||
} | ||
@@ -48,0 +42,0 @@ |
@@ -1,2 +0,3 @@ | ||
import { inputDeviceState, PointerTarget, SelectionTarget } from './types'; | ||
import { inputDeviceState } from '../setup'; | ||
import { PointerTarget, SelectionTarget } from './types'; | ||
export interface PointerMoveAction extends PointerTarget, SelectionTarget { | ||
@@ -3,0 +4,0 @@ pointerName?: string; |
@@ -1,2 +0,3 @@ | ||
import type { inputDeviceState, pointerKey, PointerTarget, SelectionTarget } from './types'; | ||
import { inputDeviceState } from '../setup'; | ||
import type { pointerKey, PointerTarget, SelectionTarget } from './types'; | ||
export interface PointerPressAction extends PointerTarget, SelectionTarget { | ||
@@ -3,0 +4,0 @@ keyDef: pointerKey; |
@@ -1,2 +0,1 @@ | ||
import { keyboardState } from '../keyboard/types'; | ||
import { PointerCoords, MouseButton } from '../utils'; | ||
@@ -37,8 +36,2 @@ /** | ||
}; | ||
export declare type pointerOptions = { | ||
/** Delay between keystrokes */ | ||
delay: number; | ||
/** Available pointer keys */ | ||
pointerMap: pointerKey[]; | ||
}; | ||
export interface pointerKey { | ||
@@ -70,5 +63,1 @@ /** Name of the pointer key */ | ||
} | ||
export interface inputDeviceState { | ||
pointerState: pointerState; | ||
keyboardState: keyboardState; | ||
} |
@@ -28,2 +28,3 @@ export * from './click/isClickableInput'; | ||
export * from './misc/findClosest'; | ||
export * from './misc/getDocumentFromNode'; | ||
export * from './misc/isDescendantOrSelf'; | ||
@@ -30,0 +31,0 @@ export * from './misc/isElementType'; |
@@ -358,2 +358,15 @@ "use strict"; | ||
var _getDocumentFromNode = require("./misc/getDocumentFromNode"); | ||
Object.keys(_getDocumentFromNode).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _getDocumentFromNode[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _getDocumentFromNode[key]; | ||
} | ||
}); | ||
}); | ||
var _isDescendantOrSelf = require("./misc/isDescendantOrSelf"); | ||
@@ -360,0 +373,0 @@ |
{ | ||
"name": "@testing-library/user-event", | ||
"version": "14.0.0-alpha.14", | ||
"version": "14.0.0-alpha.15", | ||
"description": "Fire events the same way the user does", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231779
174
5033