|
| 1 | +const defaults = { |
| 2 | + delay: 100, |
| 3 | + timeout: 4000, |
| 4 | +}; |
| 5 | + |
| 6 | +/** |
| 7 | + * Waits for specified function to resolve to a truthy value. |
| 8 | + * |
| 9 | + * @param {Function} fn function to be evaluated until truthy |
| 10 | + * @param {*} arg optional argument to pass to `fn` |
| 11 | + * @param {Object} options optional parameters |
| 12 | + * @returns {Promise} promise which resolves to function result |
| 13 | + */ |
| 14 | +export function waitForFunction(fn, arg, options = {}) { |
| 15 | + const settings = { |
| 16 | + ...defaults, |
| 17 | + ...options, |
| 18 | + }; |
| 19 | + |
| 20 | + return new Promise((resolve, reject) => { |
| 21 | + let timeElapsed = 0; |
| 22 | + |
| 23 | + const int = setInterval(() => { |
| 24 | + const result = fn(arg); |
| 25 | + |
| 26 | + if (result) { |
| 27 | + clearInterval(int); |
| 28 | + resolve(result); |
| 29 | + } |
| 30 | + |
| 31 | + timeElapsed += settings.delay; |
| 32 | + |
| 33 | + if (timeElapsed >= settings.timeout) { |
| 34 | + reject( |
| 35 | + `waitForFunction() did not return a truthy value\n\n${fn.toString()}\n\n` |
| 36 | + ); |
| 37 | + } |
| 38 | + }, settings.delay); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Waits for specified CSS selector to be located in the DOM |
| 44 | + * |
| 45 | + * @param {String} cssSelector CSS selector to query for |
| 46 | + * @param {Object} options optional parameters |
| 47 | + * @returns {Promise} promise which resolves to first matching element |
| 48 | + */ |
| 49 | +export function waitForSelector(cssSelector, options = {}) { |
| 50 | + const settings = { |
| 51 | + ...defaults, |
| 52 | + ...options, |
| 53 | + }; |
| 54 | + |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + let timeElapsed = 0; |
| 57 | + |
| 58 | + const int = setInterval(() => { |
| 59 | + const elm = document.querySelector(cssSelector); |
| 60 | + if (elm) { |
| 61 | + clearInterval(int); |
| 62 | + resolve(elm); |
| 63 | + } |
| 64 | + |
| 65 | + timeElapsed += settings.delay; |
| 66 | + |
| 67 | + if (timeElapsed >= settings.timeout) { |
| 68 | + reject( |
| 69 | + `waitForSelector() was unable to find CSS selector ${cssSelector}` |
| 70 | + ); |
| 71 | + } |
| 72 | + }, settings.delay); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Waits for specified CSS selector to contain text content |
| 78 | + * |
| 79 | + * @param {String} cssSelector CSS selector to query for |
| 80 | + * @param {String} text text to match |
| 81 | + * @param {Object} options optional parameters |
| 82 | + * @returns {Promise} promise which resolves to first matching element that contains specified text |
| 83 | + */ |
| 84 | +export function waitForText(cssSelector, text, options = {}) { |
| 85 | + const settings = { |
| 86 | + ...defaults, |
| 87 | + ...options, |
| 88 | + }; |
| 89 | + |
| 90 | + return new Promise((resolve, reject) => { |
| 91 | + waitForSelector(cssSelector, settings) |
| 92 | + .then(elm => { |
| 93 | + const isMatch = elm.textContent.includes(text); |
| 94 | + |
| 95 | + if (isMatch) { |
| 96 | + resolve(elm); |
| 97 | + } else { |
| 98 | + reject( |
| 99 | + `waitForText() did not find "${text}" in CSS selector ${cssSelector}` |
| 100 | + ); |
| 101 | + } |
| 102 | + }) |
| 103 | + .catch(() => { |
| 104 | + reject(`waitForText() was unable to find CSS selector ${cssSelector}`); |
| 105 | + }); |
| 106 | + }); |
| 107 | +} |
0 commit comments