1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.waitFor = waitForWrapper;
|
7 | var _helpers = require("./helpers");
|
8 | var _config = require("./config");
|
9 |
|
10 |
|
11 | function copyStackTrace(target, source) {
|
12 | target.stack = source.stack.replace(source.message, target.message);
|
13 | }
|
14 | function waitFor(callback, {
|
15 | container = (0, _helpers.getDocument)(),
|
16 | timeout = (0, _config.getConfig)().asyncUtilTimeout,
|
17 | showOriginalStackTrace = (0, _config.getConfig)().showOriginalStackTrace,
|
18 | stackTraceError,
|
19 | interval = 50,
|
20 | onTimeout = error => {
|
21 | Object.defineProperty(error, 'message', {
|
22 | value: (0, _config.getConfig)().getElementError(error.message, container).message
|
23 | });
|
24 | return error;
|
25 | },
|
26 | mutationObserverOptions = {
|
27 | subtree: true,
|
28 | childList: true,
|
29 | attributes: true,
|
30 | characterData: true
|
31 | }
|
32 | }) {
|
33 | if (typeof callback !== 'function') {
|
34 | throw new TypeError('Received `callback` arg must be a function');
|
35 | }
|
36 | return new Promise(async (resolve, reject) => {
|
37 | let lastError, intervalId, observer;
|
38 | let finished = false;
|
39 | let promiseStatus = 'idle';
|
40 | const overallTimeoutTimer = setTimeout(handleTimeout, timeout);
|
41 | const usingJestFakeTimers = (0, _helpers.jestFakeTimersAreEnabled)();
|
42 | if (usingJestFakeTimers) {
|
43 | const {
|
44 | unstable_advanceTimersWrapper: advanceTimersWrapper
|
45 | } = (0, _config.getConfig)();
|
46 | checkCallback();
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | while (!finished) {
|
53 | if (!(0, _helpers.jestFakeTimersAreEnabled)()) {
|
54 | const error = new Error(`Changed from using fake timers to real timers while using waitFor. This is not allowed and will result in very strange behavior. Please ensure you're awaiting all async things your test is doing before changing to real timers. For more info, please go to https://github.com/testing-library/dom-testing-library/issues/830`);
|
55 | if (!showOriginalStackTrace) copyStackTrace(error, stackTraceError);
|
56 | reject(error);
|
57 | return;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | await advanceTimersWrapper(async () => {
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | jest.advanceTimersByTime(interval);
|
72 | });
|
73 |
|
74 |
|
75 | if (finished) {
|
76 | break;
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | checkCallback();
|
83 | }
|
84 | } else {
|
85 | try {
|
86 | (0, _helpers.checkContainerType)(container);
|
87 | } catch (e) {
|
88 | reject(e);
|
89 | return;
|
90 | }
|
91 | intervalId = setInterval(checkRealTimersCallback, interval);
|
92 | const {
|
93 | MutationObserver
|
94 | } = (0, _helpers.getWindowFromNode)(container);
|
95 | observer = new MutationObserver(checkRealTimersCallback);
|
96 | observer.observe(container, mutationObserverOptions);
|
97 | checkCallback();
|
98 | }
|
99 | function onDone(error, result) {
|
100 | finished = true;
|
101 | clearTimeout(overallTimeoutTimer);
|
102 | if (!usingJestFakeTimers) {
|
103 | clearInterval(intervalId);
|
104 | observer.disconnect();
|
105 | }
|
106 | if (error) {
|
107 | reject(error);
|
108 | } else {
|
109 | resolve(result);
|
110 | }
|
111 | }
|
112 | function checkRealTimersCallback() {
|
113 | if ((0, _helpers.jestFakeTimersAreEnabled)()) {
|
114 | const error = new Error(`Changed from using real timers to fake timers while using waitFor. This is not allowed and will result in very strange behavior. Please ensure you're awaiting all async things your test is doing before changing to fake timers. For more info, please go to https://github.com/testing-library/dom-testing-library/issues/830`);
|
115 | if (!showOriginalStackTrace) copyStackTrace(error, stackTraceError);
|
116 | return reject(error);
|
117 | } else {
|
118 | return checkCallback();
|
119 | }
|
120 | }
|
121 | function checkCallback() {
|
122 | if (promiseStatus === 'pending') return;
|
123 | try {
|
124 | const result = (0, _config.runWithExpensiveErrorDiagnosticsDisabled)(callback);
|
125 | if (typeof result?.then === 'function') {
|
126 | promiseStatus = 'pending';
|
127 | result.then(resolvedValue => {
|
128 | promiseStatus = 'resolved';
|
129 | onDone(null, resolvedValue);
|
130 | }, rejectedValue => {
|
131 | promiseStatus = 'rejected';
|
132 | lastError = rejectedValue;
|
133 | });
|
134 | } else {
|
135 | onDone(null, result);
|
136 | }
|
137 |
|
138 | } catch (error) {
|
139 |
|
140 | lastError = error;
|
141 | }
|
142 | }
|
143 | function handleTimeout() {
|
144 | let error;
|
145 | if (lastError) {
|
146 | error = lastError;
|
147 | if (!showOriginalStackTrace && error.name === 'TestingLibraryElementError') {
|
148 | copyStackTrace(error, stackTraceError);
|
149 | }
|
150 | } else {
|
151 | error = new Error('Timed out in waitFor.');
|
152 | if (!showOriginalStackTrace) {
|
153 | copyStackTrace(error, stackTraceError);
|
154 | }
|
155 | }
|
156 | onDone(onTimeout(error), null);
|
157 | }
|
158 | });
|
159 | }
|
160 | function waitForWrapper(callback, options) {
|
161 |
|
162 |
|
163 | const stackTraceError = new Error('STACK_TRACE_MESSAGE');
|
164 | return (0, _config.getConfig)().asyncWrapper(() => waitFor(callback, {
|
165 | stackTraceError,
|
166 | ...options
|
167 | }));
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
\ | No newline at end of file |