UNPKG

@testing-library/user-event

Version:
94 lines (91 loc) 3.41 kB
import { prepareDocument } from '../document/index.js'; import { bindDispatchUIEvent } from '../event/index.js'; import { defaultOptionsSetup, defaultOptionsDirect } from '../options.js'; import '../utils/click/isClickableInput.js'; import { attachClipboardStubToView } from '../utils/dataTransfer/Clipboard.js'; import '../utils/edit/maxLength.js'; import '../utils/edit/isEditable.js'; import '@testing-library/dom'; import '@testing-library/dom/dist/helpers.js'; import '../utils/keyDef/readNextDescriptor.js'; import { getDocumentFromNode } from '../utils/misc/getDocumentFromNode.js'; import { setLevelRef, ApiLevel } from '../utils/misc/level.js'; import { wait } from '../utils/misc/wait.js'; import { System } from '../system/index.js'; import { Config } from './config.js'; import * as api from './api.js'; import { wrapAsync } from './wrapAsync.js'; function createConfig(options = {}, defaults = defaultOptionsSetup, node) { const document = getDocument(options, node, defaults); var _system; return { ...defaults, ...options, document, system: (_system = options.system) !== null && _system !== void 0 ? _system : new System() }; } /** * Start a "session" with userEvent. * All APIs returned by this function share an input device state and a default configuration. */ function setupMain(options = {}) { const config = createConfig(options); prepareDocument(config.document); var _defaultView; const view = (_defaultView = config.document.defaultView) !== null && _defaultView !== void 0 ? _defaultView : /* istanbul ignore next */ globalThis.window; attachClipboardStubToView(view); return doSetup(config); } /** * Setup in direct call per `userEvent.anyApi()` */ function setupDirect({ keyboardState , pointerState , ...options } = {}, node) { const config = createConfig({ ...options, system: pointerState !== null && pointerState !== void 0 ? pointerState : keyboardState }, defaultOptionsDirect, node); prepareDocument(config.document); return { config, api: doSetup(config) }; } /** * Create a set of callbacks with different default settings but the same state. */ function setupSub(options) { return doSetup({ ...this[Config], ...options }); } function wrapAndBindImpl(instance, impl) { function method(...args) { setLevelRef(instance[Config], ApiLevel.Call); return wrapAsync(()=>impl.apply(instance, args).then(async (ret)=>{ await wait(instance[Config]); return ret; })); } Object.defineProperty(method, 'name', { get: ()=>impl.name }); return method; } function doSetup(config) { const instance = { [Config]: config, dispatchUIEvent: bindDispatchUIEvent(config), ...api }; return { ...Object.fromEntries(Object.entries(api).map(([name, api])=>[ name, wrapAndBindImpl(instance, api), ])), setup: setupSub.bind(instance) }; } function getDocument(options, node, defaults) { var _document, ref; return (ref = (_document = options.document) !== null && _document !== void 0 ? _document : node && getDocumentFromNode(node)) !== null && ref !== void 0 ? ref : defaults.document; } export { createConfig, setupDirect, setupMain, setupSub };