pure.js 5.6 KB

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