1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | if (process.env.NODE_ENV !== "production") {
|
14 | (function() {
|
15 | 'use strict';
|
16 |
|
17 | var React = require('react');
|
18 | var Scheduler = require('scheduler/unstable_mock');
|
19 |
|
20 | var assign = Object.assign;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var REACT_ELEMENT_TYPE = Symbol.for('react.element');
|
27 | var REACT_FRAGMENT_TYPE = Symbol.for('react.fragment');
|
28 |
|
29 | var isArrayImpl = Array.isArray;
|
30 |
|
31 | function isArray(a) {
|
32 | return isArrayImpl(a);
|
33 | }
|
34 |
|
35 | var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
|
36 |
|
37 | function error(format) {
|
38 | {
|
39 | {
|
40 | for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
41 | args[_key2 - 1] = arguments[_key2];
|
42 | }
|
43 |
|
44 | printWarning('error', format, args);
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | function printWarning(level, format, args) {
|
50 |
|
51 |
|
52 | {
|
53 | var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
|
54 | var stack = ReactDebugCurrentFrame.getStackAddendum();
|
55 |
|
56 | if (stack !== '') {
|
57 | format += '%s';
|
58 | args = args.concat([stack]);
|
59 | }
|
60 |
|
61 |
|
62 | var argsWithFormat = args.map(function (item) {
|
63 | return String(item);
|
64 | });
|
65 |
|
66 | argsWithFormat.unshift('Warning: ' + format);
|
67 |
|
68 |
|
69 |
|
70 | Function.prototype.apply.call(console[level], console, argsWithFormat);
|
71 | }
|
72 | }
|
73 |
|
74 | var didWarnAboutMessageChannel = false;
|
75 | var enqueueTaskImpl = null;
|
76 | function enqueueTask(task) {
|
77 | if (enqueueTaskImpl === null) {
|
78 | try {
|
79 |
|
80 |
|
81 | var requireString = ('require' + Math.random()).slice(0, 7);
|
82 | var nodeRequire = module && module[requireString];
|
83 |
|
84 |
|
85 | enqueueTaskImpl = nodeRequire.call(module, 'timers').setImmediate;
|
86 | } catch (_err) {
|
87 |
|
88 |
|
89 |
|
90 | enqueueTaskImpl = function (callback) {
|
91 | {
|
92 | if (didWarnAboutMessageChannel === false) {
|
93 | didWarnAboutMessageChannel = true;
|
94 |
|
95 | if (typeof MessageChannel === 'undefined') {
|
96 | error('This browser does not have a MessageChannel implementation, ' + 'so enqueuing tasks via await act(async () => ...) will fail. ' + 'Please file an issue at https://github.com/facebook/react/issues ' + 'if you encounter this warning.');
|
97 | }
|
98 | }
|
99 | }
|
100 |
|
101 | var channel = new MessageChannel();
|
102 | channel.port1.onmessage = callback;
|
103 | channel.port2.postMessage(undefined);
|
104 | };
|
105 | }
|
106 | }
|
107 |
|
108 | return enqueueTaskImpl(task);
|
109 | }
|
110 |
|
111 | var actingUpdatesScopeDepth = 0;
|
112 | function act(scope) {
|
113 | if (Scheduler.unstable_flushAllWithoutAsserting === undefined) {
|
114 | throw Error('This version of `act` requires a special mock build of Scheduler.');
|
115 | }
|
116 |
|
117 | if (setTimeout._isMockFunction !== true) {
|
118 | throw Error("This version of `act` requires Jest's timer mocks " + '(i.e. jest.useFakeTimers).');
|
119 | }
|
120 |
|
121 | var previousIsActEnvironment = global.IS_REACT_ACT_ENVIRONMENT;
|
122 | var previousActingUpdatesScopeDepth = actingUpdatesScopeDepth;
|
123 | actingUpdatesScopeDepth++;
|
124 |
|
125 | if ( actingUpdatesScopeDepth === 1) {
|
126 |
|
127 |
|
128 | global.IS_REACT_ACT_ENVIRONMENT = false;
|
129 | }
|
130 |
|
131 | var unwind = function () {
|
132 | if ( actingUpdatesScopeDepth === 1) {
|
133 | global.IS_REACT_ACT_ENVIRONMENT = previousIsActEnvironment;
|
134 | }
|
135 |
|
136 | actingUpdatesScopeDepth--;
|
137 |
|
138 | {
|
139 | if (actingUpdatesScopeDepth > previousActingUpdatesScopeDepth) {
|
140 |
|
141 |
|
142 | error('You seem to have overlapping act() calls, this is not supported. ' + 'Be sure to await previous act() calls before making a new one. ');
|
143 | }
|
144 | }
|
145 | };
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | try {
|
151 | var result = scope();
|
152 |
|
153 | if (typeof result === 'object' && result !== null && typeof result.then === 'function') {
|
154 | var thenableResult = result;
|
155 | return {
|
156 | then: function (resolve, reject) {
|
157 | thenableResult.then(function (returnValue) {
|
158 | flushActWork(function () {
|
159 | unwind();
|
160 | resolve(returnValue);
|
161 | }, function (error) {
|
162 | unwind();
|
163 | reject(error);
|
164 | });
|
165 | }, function (error) {
|
166 | unwind();
|
167 | reject(error);
|
168 | });
|
169 | }
|
170 | };
|
171 | } else {
|
172 | var returnValue = result;
|
173 |
|
174 | try {
|
175 |
|
176 |
|
177 | var didFlushWork;
|
178 |
|
179 | do {
|
180 | didFlushWork = Scheduler.unstable_flushAllWithoutAsserting();
|
181 | } while (didFlushWork);
|
182 |
|
183 | return {
|
184 | then: function (resolve, reject) {
|
185 | resolve(returnValue);
|
186 | }
|
187 | };
|
188 | } finally {
|
189 | unwind();
|
190 | }
|
191 | }
|
192 | } catch (error) {
|
193 | unwind();
|
194 | throw error;
|
195 | }
|
196 | }
|
197 |
|
198 | function flushActWork(resolve, reject) {
|
199 |
|
200 |
|
201 | jest.runOnlyPendingTimers();
|
202 | enqueueTask(function () {
|
203 | try {
|
204 | var didFlushWork = Scheduler.unstable_flushAllWithoutAsserting();
|
205 |
|
206 | if (didFlushWork) {
|
207 | flushActWork(resolve, reject);
|
208 | } else {
|
209 | resolve();
|
210 | }
|
211 | } catch (error) {
|
212 | reject(error);
|
213 | }
|
214 | });
|
215 | }
|
216 |
|
217 | function captureAssertion(fn) {
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | try {
|
223 | fn();
|
224 | } catch (error) {
|
225 | return {
|
226 | pass: false,
|
227 | message: function () {
|
228 | return error.message;
|
229 | }
|
230 | };
|
231 | }
|
232 |
|
233 | return {
|
234 | pass: true
|
235 | };
|
236 | }
|
237 |
|
238 | function assertYieldsWereCleared(root) {
|
239 | var Scheduler = root._Scheduler;
|
240 | var actualYields = Scheduler.unstable_clearYields();
|
241 |
|
242 | if (actualYields.length !== 0) {
|
243 | throw new Error('Log of yielded values is not empty. ' + 'Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first.');
|
244 | }
|
245 | }
|
246 |
|
247 | function unstable_toMatchRenderedOutput(root, expectedJSX) {
|
248 | assertYieldsWereCleared(root);
|
249 | var actualJSON = root.toJSON();
|
250 | var actualJSX;
|
251 |
|
252 | if (actualJSON === null || typeof actualJSON === 'string') {
|
253 | actualJSX = actualJSON;
|
254 | } else if (isArray(actualJSON)) {
|
255 | if (actualJSON.length === 0) {
|
256 | actualJSX = null;
|
257 | } else if (actualJSON.length === 1) {
|
258 | actualJSX = jsonChildToJSXChild(actualJSON[0]);
|
259 | } else {
|
260 | var actualJSXChildren = jsonChildrenToJSXChildren(actualJSON);
|
261 |
|
262 | if (actualJSXChildren === null || typeof actualJSXChildren === 'string') {
|
263 | actualJSX = actualJSXChildren;
|
264 | } else {
|
265 | actualJSX = {
|
266 | $$typeof: REACT_ELEMENT_TYPE,
|
267 | type: REACT_FRAGMENT_TYPE,
|
268 | key: null,
|
269 | ref: null,
|
270 | props: {
|
271 | children: actualJSXChildren
|
272 | },
|
273 | _owner: null,
|
274 | _store: {}
|
275 | };
|
276 | }
|
277 | }
|
278 | } else {
|
279 | actualJSX = jsonChildToJSXChild(actualJSON);
|
280 | }
|
281 |
|
282 | return captureAssertion(function () {
|
283 | expect(actualJSX).toEqual(expectedJSX);
|
284 | });
|
285 | }
|
286 |
|
287 | function jsonChildToJSXChild(jsonChild) {
|
288 | if (jsonChild === null || typeof jsonChild === 'string') {
|
289 | return jsonChild;
|
290 | } else {
|
291 | var jsxChildren = jsonChildrenToJSXChildren(jsonChild.children);
|
292 | return {
|
293 | $$typeof: REACT_ELEMENT_TYPE,
|
294 | type: jsonChild.type,
|
295 | key: null,
|
296 | ref: null,
|
297 | props: jsxChildren === null ? jsonChild.props : assign({}, jsonChild.props, {
|
298 | children: jsxChildren
|
299 | }),
|
300 | _owner: null,
|
301 | _store: {}
|
302 | };
|
303 | }
|
304 | }
|
305 |
|
306 | function jsonChildrenToJSXChildren(jsonChildren) {
|
307 | if (jsonChildren !== null) {
|
308 | if (jsonChildren.length === 1) {
|
309 | return jsonChildToJSXChild(jsonChildren[0]);
|
310 | } else if (jsonChildren.length > 1) {
|
311 | var jsxChildren = [];
|
312 | var allJSXChildrenAreStrings = true;
|
313 | var jsxChildrenString = '';
|
314 |
|
315 | for (var i = 0; i < jsonChildren.length; i++) {
|
316 | var jsxChild = jsonChildToJSXChild(jsonChildren[i]);
|
317 | jsxChildren.push(jsxChild);
|
318 |
|
319 | if (allJSXChildrenAreStrings) {
|
320 | if (typeof jsxChild === 'string') {
|
321 | jsxChildrenString += jsxChild;
|
322 | } else if (jsxChild !== null) {
|
323 | allJSXChildrenAreStrings = false;
|
324 | }
|
325 | }
|
326 | }
|
327 |
|
328 | return allJSXChildrenAreStrings ? jsxChildrenString : jsxChildren;
|
329 | }
|
330 | }
|
331 |
|
332 | return null;
|
333 | }
|
334 |
|
335 | exports.act = act;
|
336 | exports.unstable_toMatchRenderedOutput = unstable_toMatchRenderedOutput;
|
337 | })();
|
338 | }
|