setup.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. var patchFocus = require('../document/patchFocus.js');
  3. var prepareDocument = require('../document/prepareDocument.js');
  4. var dispatchEvent = require('../event/dispatchEvent.js');
  5. var Clipboard = require('../utils/dataTransfer/Clipboard.js');
  6. var getWindow = require('../utils/misc/getWindow.js');
  7. var getDocumentFromNode = require('../utils/misc/getDocumentFromNode.js');
  8. var level = require('../utils/misc/level.js');
  9. var wait = require('../utils/misc/wait.js');
  10. var options = require('../options.js');
  11. require('@testing-library/dom');
  12. var keyMap = require('../keyboard/keyMap.js');
  13. var keyMap$1 = require('../pointer/keyMap.js');
  14. var index = require('../system/index.js');
  15. var api = require('./api.js');
  16. var wrapAsync = require('./wrapAsync.js');
  17. /**
  18. * Default options applied when API is called per `userEvent.anyApi()`
  19. */ const defaultOptionsDirect = {
  20. applyAccept: true,
  21. autoModify: true,
  22. delay: 0,
  23. document: globalThis.document,
  24. keyboardMap: keyMap.defaultKeyMap,
  25. pointerMap: keyMap$1.defaultKeyMap,
  26. pointerEventsCheck: options.PointerEventsCheckLevel.EachApiCall,
  27. skipAutoClose: false,
  28. skipClick: false,
  29. skipHover: false,
  30. writeToClipboard: false,
  31. advanceTimers: ()=>Promise.resolve()
  32. };
  33. /**
  34. * Default options applied when API is called per `userEvent().anyApi()`
  35. */ const defaultOptionsSetup = {
  36. ...defaultOptionsDirect,
  37. writeToClipboard: true
  38. };
  39. function createConfig(options = {}, defaults = defaultOptionsSetup, node) {
  40. const document = getDocument(options, node, defaults);
  41. return {
  42. ...defaults,
  43. ...options,
  44. document
  45. };
  46. }
  47. /**
  48. * Start a "session" with userEvent.
  49. * All APIs returned by this function share an input device state and a default configuration.
  50. */ function setupMain(options = {}) {
  51. const config = createConfig(options);
  52. prepareDocument.prepareDocument(config.document);
  53. patchFocus.patchFocus(getWindow.getWindow(config.document).HTMLElement);
  54. var _config_document_defaultView;
  55. const view = (_config_document_defaultView = config.document.defaultView) !== null && _config_document_defaultView !== undefined ? _config_document_defaultView : /* istanbul ignore next */ globalThis.window;
  56. Clipboard.attachClipboardStubToView(view);
  57. return createInstance(config).api;
  58. }
  59. /**
  60. * Setup in direct call per `userEvent.anyApi()`
  61. */ function setupDirect({ keyboardState, pointerState, ...options } = {}, node) {
  62. const config = createConfig(options, defaultOptionsDirect, node);
  63. prepareDocument.prepareDocument(config.document);
  64. patchFocus.patchFocus(getWindow.getWindow(config.document).HTMLElement);
  65. var _ref;
  66. const system = (_ref = pointerState !== null && pointerState !== undefined ? pointerState : keyboardState) !== null && _ref !== undefined ? _ref : new index.System();
  67. return {
  68. api: createInstance(config, system).api,
  69. system
  70. };
  71. }
  72. /**
  73. * Create a set of callbacks with different default settings but the same state.
  74. */ function setupSub(options) {
  75. return createInstance({
  76. ...this.config,
  77. ...options
  78. }, this.system).api;
  79. }
  80. function wrapAndBindImpl(instance, impl) {
  81. function method(...args) {
  82. level.setLevelRef(instance, level.ApiLevel.Call);
  83. return wrapAsync.wrapAsync(()=>impl.apply(instance, args).then(async (ret)=>{
  84. await wait.wait(instance.config);
  85. return ret;
  86. }));
  87. }
  88. Object.defineProperty(method, 'name', {
  89. get: ()=>impl.name
  90. });
  91. return method;
  92. }
  93. function createInstance(config, system = new index.System()) {
  94. const instance = {};
  95. Object.assign(instance, {
  96. config,
  97. dispatchEvent: dispatchEvent.dispatchEvent.bind(instance),
  98. dispatchUIEvent: dispatchEvent.dispatchUIEvent.bind(instance),
  99. system,
  100. levelRefs: {},
  101. ...api.userEventApi
  102. });
  103. return {
  104. instance,
  105. api: {
  106. ...Object.fromEntries(Object.entries(api.userEventApi).map(([name, api])=>[
  107. name,
  108. wrapAndBindImpl(instance, api)
  109. ])),
  110. setup: setupSub.bind(instance)
  111. }
  112. };
  113. }
  114. function getDocument(options, node, defaults) {
  115. var _options_document, _ref;
  116. return (_ref = (_options_document = options.document) !== null && _options_document !== undefined ? _options_document : node && getDocumentFromNode.getDocumentFromNode(node)) !== null && _ref !== undefined ? _ref : defaults.document;
  117. }
  118. exports.createConfig = createConfig;
  119. exports.createInstance = createInstance;
  120. exports.setupDirect = setupDirect;
  121. exports.setupMain = setupMain;
  122. exports.setupSub = setupSub;