UNPKG

@testing-library/user-event

Version:
67 lines (64 loc) 2.2 kB
import { dispatchUIEvent } from '../event/index.js'; import '../utils/click/isClickableInput.js'; import '../utils/dataTransfer/Clipboard.js'; import '../utils/edit/maxLength.js'; import '../utils/edit/isEditable.js'; import '@testing-library/dom'; import { isElementType } from '../utils/misc/isElementType.js'; import '@testing-library/dom/dist/helpers.js'; import '../utils/keyDef/readNextDescriptor.js'; import '../utils/misc/level.js'; import '../options.js'; import { prepareSelectionInterceptor } from './selection.js'; export { getUISelection, setUISelection } from './selection.js'; import { prepareRangeTextInterceptor } from './setRangeText.js'; import { getInitialValue, clearInitialValue, prepareValueInterceptor } from './value.js'; export { clearInitialValue, commitValueAfterInput, getUIValue, setUIValue } from './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 = getInitialValue(el); if (initialValue !== undefined) { if (el.value !== initialValue) { dispatchUIEvent({}, el, 'change'); } clearInitialValue(el); } }, { capture: true, passive: true }); document[isPrepared] = isPrepared; } function prepareElement(el) { if (el[isPrepared]) { return; } if (isElementType(el, [ 'input', 'textarea' ])) { prepareValueInterceptor(el); prepareSelectionInterceptor(el); prepareRangeTextInterceptor(el); } el[isPrepared] = isPrepared; } export { prepareDocument };