UNPKG

7.2 kBJavaScriptView Raw
1/** @license React v0.8.1
2 * jest-react.development.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';
11
12
13
14if (process.env.NODE_ENV !== "production") {
15 (function() {
16'use strict';
17
18Object.defineProperty(exports, '__esModule', { value: true });
19
20/*
21object-assign
22(c) Sindre Sorhus
23@license MIT
24*/
25
26
27/* eslint-disable no-unused-vars */
28var getOwnPropertySymbols = Object.getOwnPropertySymbols;
29var hasOwnProperty = Object.prototype.hasOwnProperty;
30var propIsEnumerable = Object.prototype.propertyIsEnumerable;
31
32function 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
40function shouldUseNative() {
41 try {
42 if (!Object.assign) {
43 return false;
44 }
45
46 // Detect buggy property enumeration order in older V8 versions.
47
48 // https://bugs.chromium.org/p/v8/issues/detail?id=4118
49 var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
50 test1[5] = 'de';
51 if (Object.getOwnPropertyNames(test1)[0] === '5') {
52 return false;
53 }
54
55 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
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 // https://bugs.chromium.org/p/v8/issues/detail?id=3056
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 // We don't expect any of the above to throw, but better to be safe.
80 return false;
81 }
82}
83
84var 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// Do not require this module directly! Use normal `invariant` calls with
112// template literal strings. The messages will be converted to ReactError during
113// build, and in production they will be minified.
114
115// Do not require this module directly! Use normal `invariant` calls with
116// template literal strings. The messages will be converted to ReactError during
117// build, and in production they will be minified.
118function ReactError(error) {
119 error.name = 'Invariant Violation';
120 return error;
121}
122
123// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
124// nor polyfill, then a plain number is used for performance.
125var hasSymbol = typeof Symbol === 'function' && Symbol.for;
126var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
127
128var REACT_FRAGMENT_TYPE = hasSymbol ? Symbol.for('react.fragment') : 0xeacb;
129
130
131
132 // TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
133// (unstable) APIs that have been removed. Can we remove the symbols?
134
135/**
136 * Use invariant() to assert state which your program assumes to be true.
137 *
138 * Provide sprintf-style format (only %s is supported) and arguments
139 * to provide information about what broke and what you were
140 * expecting.
141 *
142 * The invariant message will be stripped in production, but the invariant
143 * will remain to ensure logic does not differ in production.
144 */
145
146function captureAssertion(fn) {
147 // Trick to use a Jest matcher inside another Jest matcher. `fn` contains an
148 // assertion; if it throws, we capture the error and return it, so the stack
149 // trace presented to the user points to the original assertion in the
150 // test file.
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
167function 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
180function 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
220function 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
239function 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
268exports.unstable_toMatchRenderedOutput = unstable_toMatchRenderedOutput;
269 })();
270}