All files / src/utils waitForElementToExist.js

100% Statements 18/18
100% Branches 5/5
100% Functions 6/6
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  6x 63x 63x 53x     6x 63x 63x 10x 10x     53x 50x       6x 13x 13x   13x 10x 10x          
 
const checkSelectorExists = selector => {
    const elm = document.querySelector(selector);
    if (elm !== null) return elm;
    return null;
}
 
const untilElementExists = (selector, interval, cb) =>{
    const elm = checkSelectorExists(selector);
    if (elm) {
        cb(elm);
        return;
    }
 
    setTimeout(function() {
        untilElementExists(selector, interval, cb);
    }, interval);
}
 
const waitForElementToExist = (selector, timeout = 5000) => {
    return new Promise((resolve, reject) => {
        const timeoutFn = setTimeout(reject, timeout, `${selector} not available within timeout period`);
        
        untilElementExists(selector, 100, (elm) => {
            clearTimeout(timeoutFn);
            resolve(elm);
        });
    });
};
 
export default waitForElementToExist;