1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 | if (process.env.NODE_ENV !== "production") {
|
15 | (function() {
|
16 | 'use strict';
|
17 |
|
18 | Object.defineProperty(exports, '__esModule', { value: true });
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | var getOwnPropertySymbols = Object.getOwnPropertySymbols;
|
29 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
30 | var propIsEnumerable = Object.prototype.propertyIsEnumerable;
|
31 |
|
32 | function toObject(val) {
|
33 | if (val === null || val === undefined) {
|
34 | throw new TypeError('Object.assign cannot be called with null or undefined');
|
35 | }
|
36 |
|
37 | return Object(val);
|
38 | }
|
39 |
|
40 | function shouldUseNative() {
|
41 | try {
|
42 | if (!Object.assign) {
|
43 | return false;
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | var test1 = new String('abc');
|
50 | test1[5] = 'de';
|
51 | if (Object.getOwnPropertyNames(test1)[0] === '5') {
|
52 | return false;
|
53 | }
|
54 |
|
55 |
|
56 | var test2 = {};
|
57 | for (var i = 0; i < 10; i++) {
|
58 | test2['_' + String.fromCharCode(i)] = i;
|
59 | }
|
60 | var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
|
61 | return test2[n];
|
62 | });
|
63 | if (order2.join('') !== '0123456789') {
|
64 | return false;
|
65 | }
|
66 |
|
67 |
|
68 | var test3 = {};
|
69 | 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
|
70 | test3[letter] = letter;
|
71 | });
|
72 | if (Object.keys(Object.assign({}, test3)).join('') !==
|
73 | 'abcdefghijklmnopqrst') {
|
74 | return false;
|
75 | }
|
76 |
|
77 | return true;
|
78 | } catch (err) {
|
79 |
|
80 | return false;
|
81 | }
|
82 | }
|
83 |
|
84 | var objectAssign = shouldUseNative() ? Object.assign : function (target, source) {
|
85 | var from;
|
86 | var to = toObject(target);
|
87 | var symbols;
|
88 |
|
89 | for (var s = 1; s < arguments.length; s++) {
|
90 | from = Object(arguments[s]);
|
91 |
|
92 | for (var key in from) {
|
93 | if (hasOwnProperty.call(from, key)) {
|
94 | to[key] = from[key];
|
95 | }
|
96 | }
|
97 |
|
98 | if (getOwnPropertySymbols) {
|
99 | symbols = getOwnPropertySymbols(from);
|
100 | for (var i = 0; i < symbols.length; i++) {
|
101 | if (propIsEnumerable.call(from, symbols[i])) {
|
102 | to[symbols[i]] = from[symbols[i]];
|
103 | }
|
104 | }
|
105 | }
|
106 | }
|
107 |
|
108 | return to;
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function ReactError(error) {
|
119 | error.name = 'Invariant Violation';
|
120 | return error;
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 | var hasSymbol = typeof Symbol === 'function' && Symbol.for;
|
126 | var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
|
127 |
|
128 | var REACT_FRAGMENT_TYPE = hasSymbol ? Symbol.for('react.fragment') : 0xeacb;
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function captureAssertion(fn) {
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | try {
|
152 | fn();
|
153 | } catch (error) {
|
154 | return {
|
155 | pass: false,
|
156 | message: function () {
|
157 | return error.message;
|
158 | }
|
159 | };
|
160 | }
|
161 |
|
162 | return {
|
163 | pass: true
|
164 | };
|
165 | }
|
166 |
|
167 | function assertYieldsWereCleared(root) {
|
168 | var Scheduler = root._Scheduler;
|
169 | var actualYields = Scheduler.unstable_clearYields();
|
170 |
|
171 | (function () {
|
172 | if (!(actualYields.length === 0)) {
|
173 | {
|
174 | throw ReactError(Error("Log of yielded values is not empty. Call expect(ReactTestRenderer).unstable_toHaveYielded(...) first."));
|
175 | }
|
176 | }
|
177 | })();
|
178 | }
|
179 |
|
180 | function unstable_toMatchRenderedOutput(root, expectedJSX) {
|
181 | assertYieldsWereCleared(root);
|
182 | var actualJSON = root.toJSON();
|
183 | var actualJSX;
|
184 |
|
185 | if (actualJSON === null || typeof actualJSON === 'string') {
|
186 | actualJSX = actualJSON;
|
187 | } else if (Array.isArray(actualJSON)) {
|
188 | if (actualJSON.length === 0) {
|
189 | actualJSX = null;
|
190 | } else if (actualJSON.length === 1) {
|
191 | actualJSX = jsonChildToJSXChild(actualJSON[0]);
|
192 | } else {
|
193 | var actualJSXChildren = jsonChildrenToJSXChildren(actualJSON);
|
194 |
|
195 | if (actualJSXChildren === null || typeof actualJSXChildren === 'string') {
|
196 | actualJSX = actualJSXChildren;
|
197 | } else {
|
198 | actualJSX = {
|
199 | $$typeof: REACT_ELEMENT_TYPE,
|
200 | type: REACT_FRAGMENT_TYPE,
|
201 | key: null,
|
202 | ref: null,
|
203 | props: {
|
204 | children: actualJSXChildren
|
205 | },
|
206 | _owner: null,
|
207 | _store: {}
|
208 | };
|
209 | }
|
210 | }
|
211 | } else {
|
212 | actualJSX = jsonChildToJSXChild(actualJSON);
|
213 | }
|
214 |
|
215 | return captureAssertion(function () {
|
216 | expect(actualJSX).toEqual(expectedJSX);
|
217 | });
|
218 | }
|
219 |
|
220 | function jsonChildToJSXChild(jsonChild) {
|
221 | if (jsonChild === null || typeof jsonChild === 'string') {
|
222 | return jsonChild;
|
223 | } else {
|
224 | var jsxChildren = jsonChildrenToJSXChildren(jsonChild.children);
|
225 | return {
|
226 | $$typeof: REACT_ELEMENT_TYPE,
|
227 | type: jsonChild.type,
|
228 | key: null,
|
229 | ref: null,
|
230 | props: jsxChildren === null ? jsonChild.props : objectAssign({}, jsonChild.props, {
|
231 | children: jsxChildren
|
232 | }),
|
233 | _owner: null,
|
234 | _store: {}
|
235 | };
|
236 | }
|
237 | }
|
238 |
|
239 | function jsonChildrenToJSXChildren(jsonChildren) {
|
240 | if (jsonChildren !== null) {
|
241 | if (jsonChildren.length === 1) {
|
242 | return jsonChildToJSXChild(jsonChildren[0]);
|
243 | } else if (jsonChildren.length > 1) {
|
244 | var jsxChildren = [];
|
245 | var allJSXChildrenAreStrings = true;
|
246 | var jsxChildrenString = '';
|
247 |
|
248 | for (var i = 0; i < jsonChildren.length; i++) {
|
249 | var jsxChild = jsonChildToJSXChild(jsonChildren[i]);
|
250 | jsxChildren.push(jsxChild);
|
251 |
|
252 | if (allJSXChildrenAreStrings) {
|
253 | if (typeof jsxChild === 'string') {
|
254 | jsxChildrenString += jsxChild;
|
255 | } else if (jsxChild !== null) {
|
256 | allJSXChildrenAreStrings = false;
|
257 | }
|
258 | }
|
259 | }
|
260 |
|
261 | return allJSXChildrenAreStrings ? jsxChildrenString : jsxChildren;
|
262 | }
|
263 | }
|
264 |
|
265 | return null;
|
266 | }
|
267 |
|
268 | exports.unstable_toMatchRenderedOutput = unstable_toMatchRenderedOutput;
|
269 | })();
|
270 | }
|