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.

154 lines
4.6 KiB

  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. var _exportNames = {
  8. render: true,
  9. cleanup: true,
  10. act: true,
  11. fireEvent: true
  12. };
  13. exports.render = render;
  14. exports.cleanup = cleanup;
  15. Object.defineProperty(exports, "act", {
  16. enumerable: true,
  17. get: function () {
  18. return _actCompat.default;
  19. }
  20. });
  21. Object.defineProperty(exports, "fireEvent", {
  22. enumerable: true,
  23. get: function () {
  24. return _fireEvent.fireEvent;
  25. }
  26. });
  27. var React = _interopRequireWildcard(require("react"));
  28. var _reactDom = _interopRequireDefault(require("react-dom"));
  29. var _dom = require("@testing-library/dom");
  30. Object.keys(_dom).forEach(function (key) {
  31. if (key === "default" || key === "__esModule") return;
  32. if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  33. if (key in exports && exports[key] === _dom[key]) return;
  34. Object.defineProperty(exports, key, {
  35. enumerable: true,
  36. get: function () {
  37. return _dom[key];
  38. }
  39. });
  40. });
  41. var _actCompat = _interopRequireWildcard(require("./act-compat"));
  42. var _fireEvent = require("./fire-event");
  43. (0, _dom.configure)({
  44. asyncWrapper: async cb => {
  45. let result;
  46. await (0, _actCompat.asyncAct)(async () => {
  47. result = await cb();
  48. });
  49. return result;
  50. },
  51. eventWrapper: cb => {
  52. let result;
  53. (0, _actCompat.default)(() => {
  54. result = cb();
  55. });
  56. return result;
  57. }
  58. });
  59. const mountedContainers = new Set();
  60. function render(ui, {
  61. container,
  62. baseElement = container,
  63. queries,
  64. hydrate = false,
  65. wrapper: WrapperComponent
  66. } = {}) {
  67. if (!baseElement) {
  68. // default to document.body instead of documentElement to avoid output of potentially-large
  69. // head elements (such as JSS style blocks) in debug output
  70. baseElement = document.body;
  71. }
  72. if (!container) {
  73. container = baseElement.appendChild(document.createElement('div'));
  74. } // we'll add it to the mounted containers regardless of whether it's actually
  75. // added to document.body so the cleanup method works regardless of whether
  76. // they're passing us a custom container or not.
  77. mountedContainers.add(container);
  78. const wrapUiIfNeeded = innerElement => WrapperComponent ? /*#__PURE__*/React.createElement(WrapperComponent, null, innerElement) : innerElement;
  79. (0, _actCompat.default)(() => {
  80. if (hydrate) {
  81. _reactDom.default.hydrate(wrapUiIfNeeded(ui), container);
  82. } else {
  83. _reactDom.default.render(wrapUiIfNeeded(ui), container);
  84. }
  85. });
  86. return {
  87. container,
  88. baseElement,
  89. debug: (el = baseElement, maxLength, options) => Array.isArray(el) ? // eslint-disable-next-line no-console
  90. el.forEach(e => console.log((0, _dom.prettyDOM)(e, maxLength, options))) : // eslint-disable-next-line no-console,
  91. console.log((0, _dom.prettyDOM)(el, maxLength, options)),
  92. unmount: () => {
  93. (0, _actCompat.default)(() => {
  94. _reactDom.default.unmountComponentAtNode(container);
  95. });
  96. },
  97. rerender: rerenderUi => {
  98. render(wrapUiIfNeeded(rerenderUi), {
  99. container,
  100. baseElement
  101. }); // Intentionally do not return anything to avoid unnecessarily complicating the API.
  102. // folks can use all the same utilities we return in the first place that are bound to the container
  103. },
  104. asFragment: () => {
  105. /* istanbul ignore else (old jsdom limitation) */
  106. if (typeof document.createRange === 'function') {
  107. return document.createRange().createContextualFragment(container.innerHTML);
  108. } else {
  109. const template = document.createElement('template');
  110. template.innerHTML = container.innerHTML;
  111. return template.content;
  112. }
  113. },
  114. ...(0, _dom.getQueriesForElement)(baseElement, queries)
  115. };
  116. }
  117. function cleanup() {
  118. mountedContainers.forEach(cleanupAtContainer);
  119. } // maybe one day we'll expose this (perhaps even as a utility returned by render).
  120. // but let's wait until someone asks for it.
  121. function cleanupAtContainer(container) {
  122. (0, _actCompat.default)(() => {
  123. _reactDom.default.unmountComponentAtNode(container);
  124. });
  125. if (container.parentNode === document.body) {
  126. document.body.removeChild(container);
  127. }
  128. mountedContainers.delete(container);
  129. } // just re-export everything from dom-testing-library
  130. // NOTE: we're not going to export asyncAct because that's our own compatibility
  131. // thing for people using react-dom@16.8.0. Anyone else doesn't need it and
  132. // people should just upgrade anyway.
  133. /* eslint func-name-matching:0 */