web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.5 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getWindowFromNode = getWindowFromNode;
  6. exports.getDocument = getDocument;
  7. exports.runWithRealTimers = runWithRealTimers;
  8. exports.checkContainerType = checkContainerType;
  9. exports.jestFakeTimersAreEnabled = jestFakeTimersAreEnabled;
  10. exports.TEXT_NODE = exports.setTimeout = exports.setImmediate = exports.clearTimeout = void 0;
  11. const globalObj = typeof window === 'undefined' ? global : window; // Constant node.nodeType for text nodes, see:
  12. // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#Node_type_constants
  13. const TEXT_NODE = 3; // Currently this fn only supports jest timers, but it could support other test runners in the future.
  14. exports.TEXT_NODE = TEXT_NODE;
  15. function runWithRealTimers(callback) {
  16. // istanbul ignore else
  17. if (typeof jest !== 'undefined') {
  18. return runWithJestRealTimers(callback).callbackReturnValue;
  19. } // istanbul ignore next
  20. return callback();
  21. }
  22. function runWithJestRealTimers(callback) {
  23. const timerAPI = {
  24. clearInterval,
  25. clearTimeout,
  26. setInterval,
  27. setTimeout
  28. }; // For more on why we have the check here,
  29. // checkout https://github.com/testing-library/dom-testing-library/issues/914
  30. if (typeof setImmediate === 'function') {
  31. timerAPI.setImmediate = setImmediate;
  32. }
  33. if (typeof clearImmediate === 'function') {
  34. timerAPI.clearImmediate = clearImmediate;
  35. }
  36. jest.useRealTimers();
  37. const callbackReturnValue = callback();
  38. const usedFakeTimers = Object.entries(timerAPI).some(([name, func]) => func !== globalObj[name]);
  39. if (usedFakeTimers) {
  40. var _timerAPI$setTimeout;
  41. jest.useFakeTimers((_timerAPI$setTimeout = timerAPI.setTimeout) != null && _timerAPI$setTimeout.clock ? 'modern' : 'legacy');
  42. }
  43. return {
  44. callbackReturnValue,
  45. usedFakeTimers
  46. };
  47. }
  48. function jestFakeTimersAreEnabled() {
  49. // istanbul ignore else
  50. if (typeof jest !== 'undefined') {
  51. return runWithJestRealTimers(() => {}).usedFakeTimers;
  52. } // istanbul ignore next
  53. return false;
  54. } // we only run our tests in node, and setImmediate is supported in node.
  55. // istanbul ignore next
  56. function setImmediatePolyfill(fn) {
  57. return globalObj.setTimeout(fn, 0);
  58. }
  59. function getTimeFunctions() {
  60. // istanbul ignore next
  61. return {
  62. clearTimeoutFn: globalObj.clearTimeout,
  63. setImmediateFn: globalObj.setImmediate || setImmediatePolyfill,
  64. setTimeoutFn: globalObj.setTimeout
  65. };
  66. }
  67. const {
  68. clearTimeoutFn,
  69. setImmediateFn,
  70. setTimeoutFn
  71. } = runWithRealTimers(getTimeFunctions);
  72. exports.setTimeout = setTimeoutFn;
  73. exports.setImmediate = setImmediateFn;
  74. exports.clearTimeout = clearTimeoutFn;
  75. function getDocument() {
  76. /* istanbul ignore if */
  77. if (typeof window === 'undefined') {
  78. throw new Error('Could not find default container');
  79. }
  80. return window.document;
  81. }
  82. function getWindowFromNode(node) {
  83. if (node.defaultView) {
  84. // node is document
  85. return node.defaultView;
  86. } else if (node.ownerDocument && node.ownerDocument.defaultView) {
  87. // node is a DOM node
  88. return node.ownerDocument.defaultView;
  89. } else if (node.window) {
  90. // node is window
  91. return node.window;
  92. } else if (node.then instanceof Function) {
  93. throw new Error(`It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`);
  94. } else if (Array.isArray(node)) {
  95. throw new Error(`It looks like you passed an Array instead of a DOM node. Did you do something like \`fireEvent.click(screen.getAllBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`?`);
  96. } else {
  97. // The user passed something unusual to a calling function
  98. throw new Error(`Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`);
  99. }
  100. }
  101. function checkContainerType(container) {
  102. if (!container || !(typeof container.querySelector === 'function') || !(typeof container.querySelectorAll === 'function')) {
  103. throw new TypeError(`Expected container to be an Element, a Document or a DocumentFragment but got ${getTypeName(container)}.`);
  104. }
  105. function getTypeName(object) {
  106. if (typeof object === 'object') {
  107. return object === null ? 'null' : object.constructor.name;
  108. }
  109. return typeof object;
  110. }
  111. }