wait-for-dom-change.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.waitForDomChange = waitForDomChangeWrapper;
  6. var _helpers = require("./helpers");
  7. var _config = require("./config");
  8. let hasWarned = false; // deprecated... TODO: remove this method. People should use wait instead
  9. // the reasoning is that waiting for just any DOM change is an implementation
  10. // detail. People should be waiting for a specific thing to change.
  11. function waitForDomChange({
  12. container = (0, _helpers.getDocument)(),
  13. timeout = (0, _config.getConfig)().asyncUtilTimeout,
  14. mutationObserverOptions = {
  15. subtree: true,
  16. childList: true,
  17. attributes: true,
  18. characterData: true
  19. }
  20. } = {}) {
  21. if (!hasWarned) {
  22. hasWarned = true;
  23. console.warn(`\`waitForDomChange\` has been deprecated. Use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor.`);
  24. }
  25. return new Promise((resolve, reject) => {
  26. const timer = (0, _helpers.setTimeout)(onTimeout, timeout);
  27. const {
  28. MutationObserver
  29. } = (0, _helpers.getWindowFromNode)(container);
  30. const observer = new MutationObserver(onMutation);
  31. (0, _helpers.runWithRealTimers)(() => observer.observe(container, mutationObserverOptions));
  32. function onDone(error, result) {
  33. (0, _helpers.clearTimeout)(timer);
  34. (0, _helpers.setImmediate)(() => observer.disconnect());
  35. if (error) {
  36. reject(error);
  37. } else {
  38. resolve(result);
  39. }
  40. }
  41. function onMutation(mutationsList) {
  42. onDone(null, mutationsList);
  43. }
  44. function onTimeout() {
  45. onDone(new Error('Timed out in waitForDomChange.'), null);
  46. }
  47. });
  48. }
  49. function waitForDomChangeWrapper(...args) {
  50. return (0, _config.getConfig)().asyncWrapper(() => waitForDomChange(...args));
  51. }