@testing-library/user-event
Version:
Fire events the same way the user does
137 lines (135 loc) • 4.65 kB
JavaScript
import '../../utils/click/isClickableInput.js';
import '../../utils/dataTransfer/Clipboard.js';
import { getValue } from '../../utils/edit/getValue.js';
import { input } from '../../utils/edit/input.js';
import { isContentEditable } from '../../utils/edit/isContentEditable.js';
import { isEditable } from '../../utils/edit/isEditable.js';
import { walkRadio } from '../../utils/edit/walkRadio.js';
import '@testing-library/dom';
import { isElementType } from '../../utils/misc/isElementType.js';
import '@testing-library/dom/dist/helpers.js';
import { moveSelection, setSelectionRange, hasOwnSelection } from '../../utils/focus/selection.js';
import { focus } from '../../utils/focus/focus.js';
import { getTabDestination } from '../../utils/focus/getTabDestination.js';
import { selectAll } from '../../utils/focus/selectAll.js';
import '../../utils/keyDef/readNextDescriptor.js';
import '../../utils/misc/level.js';
import '../../options.js';
import '../eventMap.js';
import './click.js';
import './cut.js';
import './keypress.js';
import './keyup.js';
import './paste.js';
import { behavior } from './registry.js';
import { setUISelection } from '../../document/selection.js';
behavior.keydown = (event, target, config)=>{
var ref;
var ref1;
return (ref1 = (ref = keydownBehavior[event.key]) === null || ref === void 0 ? void 0 : ref.call(keydownBehavior, event, target, config)) !== null && ref1 !== void 0 ? ref1 : combinationBehavior(event, target, config);
};
const keydownBehavior = {
ArrowDown: (event, target, config)=>{
/* istanbul ignore else */ if (isElementType(target, 'input', {
type: 'radio'
})) {
return ()=>walkRadio(config, target, -1);
}
},
ArrowLeft: (event, target, config)=>{
if (isElementType(target, 'input', {
type: 'radio'
})) {
return ()=>walkRadio(config, target, -1);
}
return ()=>moveSelection(target, -1);
},
ArrowRight: (event, target, config)=>{
if (isElementType(target, 'input', {
type: 'radio'
})) {
return ()=>walkRadio(config, target, 1);
}
return ()=>moveSelection(target, 1);
},
ArrowUp: (event, target, config)=>{
/* istanbul ignore else */ if (isElementType(target, 'input', {
type: 'radio'
})) {
return ()=>walkRadio(config, target, 1);
}
},
Backspace: (event, target, config)=>{
if (isEditable(target)) {
return ()=>{
input(config, target, '', 'deleteContentBackward');
};
}
},
Delete: (event, target, config)=>{
if (isEditable(target)) {
return ()=>{
input(config, target, '', 'deleteContentForward');
};
}
},
End: (event, target)=>{
if (isElementType(target, [
'input',
'textarea'
]) || isContentEditable(target)) {
return ()=>{
var ref;
var ref1;
const newPos = (ref1 = (ref = getValue(target)) === null || ref === void 0 ? void 0 : ref.length) !== null && ref1 !== void 0 ? ref1 : /* istanbul ignore next */ 0;
setSelectionRange(target, newPos, newPos);
};
}
},
Home: (event, target)=>{
if (isElementType(target, [
'input',
'textarea'
]) || isContentEditable(target)) {
return ()=>{
setSelectionRange(target, 0, 0);
};
}
},
PageDown: (event, target)=>{
if (isElementType(target, [
'input'
])) {
return ()=>{
const newPos = getValue(target).length;
setSelectionRange(target, newPos, newPos);
};
}
},
PageUp: (event, target)=>{
if (isElementType(target, [
'input'
])) {
return ()=>{
setSelectionRange(target, 0, 0);
};
}
},
Tab: (event, target, config)=>{
return ()=>{
const dest = getTabDestination(target, config.system.keyboard.modifiers.Shift);
focus(dest);
if (hasOwnSelection(dest)) {
setUISelection(dest, {
anchorOffset: 0,
focusOffset: dest.value.length
});
}
};
}
};
const combinationBehavior = (event, target, config)=>{
if (event.code === 'KeyA' && config.system.keyboard.modifiers.Control) {
return ()=>selectAll(target);
}
};