wait-for-dom-change.js
1.73 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.waitForDomChange = waitForDomChangeWrapper;
var _helpers = require("./helpers");
var _config = require("./config");
let hasWarned = false; // deprecated... TODO: remove this method. People should use wait instead
// the reasoning is that waiting for just any DOM change is an implementation
// detail. People should be waiting for a specific thing to change.
function waitForDomChange({
container = (0, _helpers.getDocument)(),
timeout = (0, _config.getConfig)().asyncUtilTimeout,
mutationObserverOptions = {
subtree: true,
childList: true,
attributes: true,
characterData: true
}
} = {}) {
if (!hasWarned) {
hasWarned = true;
console.warn(`\`waitForDomChange\` has been deprecated. Use \`waitFor\` instead: https://testing-library.com/docs/dom-testing-library/api-async#waitfor.`);
}
return new Promise((resolve, reject) => {
const timer = (0, _helpers.setTimeout)(function () {
onDone(new Error('Timed out in waitForDomChange.'), null);
}, timeout);
const {
MutationObserver
} = (0, _helpers.getWindowFromNode)(container);
const observer = new MutationObserver(function (mutationsList) {
onDone(null, mutationsList);
});
(0, _helpers.runWithRealTimers)(() => observer.observe(container, mutationObserverOptions));
function onDone(error, result) {
(0, _helpers.clearTimeout)(timer);
(0, _helpers.setImmediate)(() => observer.disconnect());
if (error) {
reject(error);
} else {
resolve(result);
}
}
});
}
function waitForDomChangeWrapper(...args) {
return (0, _config.getConfig)().asyncWrapper(() => waitForDomChange(...args));
}