wait-for-element-to-be-removed.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.waitForElementToBeRemoved = waitForElementToBeRemoved;
  6. var _waitFor = require("./wait-for");
  7. const isRemoved = result => !result || Array.isArray(result) && !result.length; // Check if the element is not present.
  8. // As the name implies, waitForElementToBeRemoved should check `present` --> `removed`
  9. function initialCheck(elements) {
  10. if (isRemoved(elements)) {
  11. throw new Error('The element(s) given to waitForElementToBeRemoved are already removed. waitForElementToBeRemoved requires that the element(s) exist(s) before waiting for removal.');
  12. }
  13. }
  14. async function waitForElementToBeRemoved(callback, options) {
  15. // created here so we get a nice stacktrace
  16. const timeoutError = new Error('Timed out in waitForElementToBeRemoved.');
  17. if (typeof callback !== 'function') {
  18. initialCheck(callback);
  19. const elements = Array.isArray(callback) ? callback : [callback];
  20. const getRemainingElements = elements.map(element => {
  21. let parent = element.parentElement;
  22. if (parent === null) return () => null;
  23. while (parent.parentElement) parent = parent.parentElement;
  24. return () => parent.contains(element) ? element : null;
  25. });
  26. callback = () => getRemainingElements.map(c => c()).filter(Boolean);
  27. }
  28. initialCheck(callback());
  29. return (0, _waitFor.waitFor)(() => {
  30. let result;
  31. try {
  32. result = callback();
  33. } catch (error) {
  34. if (error.name === 'TestingLibraryElementError') {
  35. return undefined;
  36. }
  37. throw error;
  38. }
  39. if (!isRemoved(result)) {
  40. throw timeoutError;
  41. }
  42. return undefined;
  43. }, options);
  44. }
  45. /*
  46. eslint
  47. require-await: "off"
  48. */