@testing-library/user-event
Version:
Fire events the same way the user does
75 lines (70 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var index = require('../event/index.js');
require('../utils/click/isClickableInput.js');
require('../utils/dataTransfer/Clipboard.js');
require('../utils/edit/maxLength.js');
require('../utils/edit/isEditable.js');
require('@testing-library/dom');
var isElementType = require('../utils/misc/isElementType.js');
require('@testing-library/dom/dist/helpers.js');
require('../utils/keyDef/readNextDescriptor.js');
require('../utils/misc/level.js');
require('../options.js');
var selection = require('./selection.js');
var setRangeText = require('./setRangeText.js');
var value = require('./value.js');
const isPrepared = Symbol('Node prepared with document state workarounds');
function prepareDocument(document) {
if (document[isPrepared]) {
return;
}
document.addEventListener('focus', (e)=>{
const el = e.target;
prepareElement(el);
}, {
capture: true,
passive: true
});
// Our test environment defaults to `document.body` as `activeElement`.
// In other environments this might be `null` when preparing.
// istanbul ignore else
if (document.activeElement) {
prepareElement(document.activeElement);
}
document.addEventListener('blur', (e)=>{
const el = e.target;
const initialValue = value.getInitialValue(el);
if (initialValue !== undefined) {
if (el.value !== initialValue) {
index.dispatchUIEvent({}, el, 'change');
}
value.clearInitialValue(el);
}
}, {
capture: true,
passive: true
});
document[isPrepared] = isPrepared;
}
function prepareElement(el) {
if (el[isPrepared]) {
return;
}
if (isElementType.isElementType(el, [
'input',
'textarea'
])) {
value.prepareValueInterceptor(el);
selection.prepareSelectionInterceptor(el);
setRangeText.prepareRangeTextInterceptor(el);
}
el[isPrepared] = isPrepared;
}
exports.getUISelection = selection.getUISelection;
exports.setUISelection = selection.setUISelection;
exports.clearInitialValue = value.clearInitialValue;
exports.commitValueAfterInput = value.commitValueAfterInput;
exports.getUIValue = value.getUIValue;
exports.setUIValue = value.setUIValue;
exports.prepareDocument = prepareDocument;