549 lines
19 KiB
JavaScript
549 lines
19 KiB
JavaScript
"use strict";
|
|
|
|
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
|
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
|
|
|
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
|
|
|
|
/**
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
var fs = require("fs");
|
|
var path = require("path");
|
|
|
|
var _require = require("../lib/prepack-node.js"),
|
|
prepackSources = _require.prepackSources;
|
|
|
|
var babel = require("babel-core");
|
|
var React = require("react");
|
|
var ReactTestRenderer = require("react-test-renderer");
|
|
|
|
var _require2 = require("../lib/utils/json.js"),
|
|
mergeAdacentJSONTextNodes = _require2.mergeAdacentJSONTextNodes;
|
|
/* eslint-disable no-undef */
|
|
|
|
|
|
var _global = global,
|
|
expect = _global.expect,
|
|
describe = _global.describe,
|
|
it = _global.it;
|
|
|
|
|
|
function runTestSuite(outputJsx) {
|
|
var _this = this;
|
|
|
|
var runTest = function () {
|
|
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(directory, name) {
|
|
var source, _compileSourceWithPre, compiledSource, A, B, rendererA, rendererB, getTrials, resultA, resultB, i, _resultA$i, nameA, valueA, _resultB$i, nameB, valueB;
|
|
|
|
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
while (1) {
|
|
switch (_context.prev = _context.next) {
|
|
case 0:
|
|
source = fs.readFileSync(path.join(reactTestRoot, directory, name)).toString();
|
|
_compileSourceWithPre = compileSourceWithPrepack(source), compiledSource = _compileSourceWithPre.compiledSource;
|
|
A = runSource(source);
|
|
|
|
expect(typeof A === "undefined" ? "undefined" : _typeof(A)).toBe("function");
|
|
B = runSource(compiledSource);
|
|
|
|
expect(typeof B === "undefined" ? "undefined" : _typeof(B)).toBe("function");
|
|
|
|
rendererA = ReactTestRenderer.create(null);
|
|
rendererB = ReactTestRenderer.create(null);
|
|
|
|
if (!(A == null || B == null)) {
|
|
_context.next = 10;
|
|
break;
|
|
}
|
|
|
|
throw new Error("React test runner issue");
|
|
|
|
case 10:
|
|
// // Use the original version of the test in case transforming messes it up.
|
|
getTrials = A.getTrials;
|
|
// // Run tests that assert the rendered output matches.
|
|
|
|
resultA = getTrials(rendererA, A);
|
|
resultB = getTrials(rendererB, B);
|
|
|
|
// // the test has returned many values for us to check
|
|
|
|
for (i = 0; i < resultA.length; i++) {
|
|
_resultA$i = _slicedToArray(resultA[i], 2), nameA = _resultA$i[0], valueA = _resultA$i[1];
|
|
_resultB$i = _slicedToArray(resultB[i], 2), nameB = _resultB$i[0], valueB = _resultB$i[1];
|
|
|
|
expect(mergeAdacentJSONTextNodes(valueB)).toEqual(mergeAdacentJSONTextNodes(valueA));
|
|
expect(nameB).toEqual(nameA);
|
|
}
|
|
|
|
case 14:
|
|
case "end":
|
|
return _context.stop();
|
|
}
|
|
}
|
|
}, _callee, this);
|
|
}));
|
|
|
|
return function runTest(_x, _x2) {
|
|
return _ref.apply(this, arguments);
|
|
};
|
|
}();
|
|
|
|
// Jest tests
|
|
|
|
|
|
var reactTestRoot = path.join(__dirname, "../test/react/");
|
|
var prepackOptions = {
|
|
compatibility: "react-mocks",
|
|
internalDebug: true,
|
|
serialize: true,
|
|
uniqueSuffix: "",
|
|
maxStackDepth: 100,
|
|
reactEnabled: true,
|
|
reactOutput: outputJsx ? "jsx" : "create-element",
|
|
inlineExpressions: true,
|
|
simpleClosures: true,
|
|
omitInvariants: true
|
|
};
|
|
|
|
function compileSourceWithPrepack(source) {
|
|
var code = "(function(){" + source + "})()";
|
|
var serialized = prepackSources([{ filePath: "", fileContents: code, sourceMapContents: "" }], prepackOptions);
|
|
if (serialized == null || serialized.reactStatistics == null) {
|
|
throw new Error("React test runner failed during serialization");
|
|
}
|
|
return {
|
|
compiledSource: serialized.code,
|
|
statistics: serialized.reactStatistics
|
|
};
|
|
}
|
|
|
|
function runSource(source) {
|
|
var transformedSource = babel.transform(source, {
|
|
presets: ["babel-preset-react"],
|
|
plugins: ["transform-object-rest-spread"]
|
|
}).code;
|
|
/* eslint-disable no-new-func */
|
|
var fn = new Function("require", "module", transformedSource);
|
|
var moduleShim = { exports: null };
|
|
var requireShim = function requireShim(name) {
|
|
switch (name) {
|
|
case "react":
|
|
return React;
|
|
default:
|
|
throw new Error("Unrecognized import: \"" + name + "\".");
|
|
}
|
|
};
|
|
try {
|
|
// $FlowFixMe flow doesn't new Function
|
|
fn(requireShim, moduleShim);
|
|
} catch (e) {
|
|
console.error(transformedSource);
|
|
throw e;
|
|
}
|
|
return moduleShim.exports;
|
|
}
|
|
|
|
var originalConsoleError = console.error;
|
|
|
|
describe("Test React (" + (outputJsx ? "JSX" : "create-element") + ")", function () {
|
|
describe("Functional component folding", function () {
|
|
var directory = "functional-components";
|
|
|
|
it("Simple", _asyncToGenerator(regeneratorRuntime.mark(function _callee2() {
|
|
return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
while (1) {
|
|
switch (_context2.prev = _context2.next) {
|
|
case 0:
|
|
_context2.next = 2;
|
|
return runTest(directory, "simple.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context2.stop();
|
|
}
|
|
}
|
|
}, _callee2, _this);
|
|
})));
|
|
|
|
it("Simple children", _asyncToGenerator(regeneratorRuntime.mark(function _callee3() {
|
|
return regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
while (1) {
|
|
switch (_context3.prev = _context3.next) {
|
|
case 0:
|
|
_context3.next = 2;
|
|
return runTest(directory, "simple-children.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context3.stop();
|
|
}
|
|
}
|
|
}, _callee3, _this);
|
|
})));
|
|
|
|
it("Simple refs", _asyncToGenerator(regeneratorRuntime.mark(function _callee4() {
|
|
return regeneratorRuntime.wrap(function _callee4$(_context4) {
|
|
while (1) {
|
|
switch (_context4.prev = _context4.next) {
|
|
case 0:
|
|
_context4.next = 2;
|
|
return runTest(directory, "simple-refs.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context4.stop();
|
|
}
|
|
}
|
|
}, _callee4, _this);
|
|
})));
|
|
|
|
it("Conditional", _asyncToGenerator(regeneratorRuntime.mark(function _callee5() {
|
|
return regeneratorRuntime.wrap(function _callee5$(_context5) {
|
|
while (1) {
|
|
switch (_context5.prev = _context5.next) {
|
|
case 0:
|
|
_context5.next = 2;
|
|
return runTest(directory, "conditional.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context5.stop();
|
|
}
|
|
}
|
|
}, _callee5, _this);
|
|
})));
|
|
|
|
it("Key nesting", _asyncToGenerator(regeneratorRuntime.mark(function _callee6() {
|
|
return regeneratorRuntime.wrap(function _callee6$(_context6) {
|
|
while (1) {
|
|
switch (_context6.prev = _context6.next) {
|
|
case 0:
|
|
_context6.next = 2;
|
|
return runTest(directory, "key-nesting.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context6.stop();
|
|
}
|
|
}
|
|
}, _callee6, _this);
|
|
})));
|
|
|
|
it("Key nesting 2", _asyncToGenerator(regeneratorRuntime.mark(function _callee7() {
|
|
return regeneratorRuntime.wrap(function _callee7$(_context7) {
|
|
while (1) {
|
|
switch (_context7.prev = _context7.next) {
|
|
case 0:
|
|
_context7.next = 2;
|
|
return runTest(directory, "key-nesting-2.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context7.stop();
|
|
}
|
|
}
|
|
}, _callee7, _this);
|
|
})));
|
|
|
|
it("Key nesting 3", _asyncToGenerator(regeneratorRuntime.mark(function _callee8() {
|
|
return regeneratorRuntime.wrap(function _callee8$(_context8) {
|
|
while (1) {
|
|
switch (_context8.prev = _context8.next) {
|
|
case 0:
|
|
_context8.next = 2;
|
|
return runTest(directory, "key-nesting-3.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context8.stop();
|
|
}
|
|
}
|
|
}, _callee8, _this);
|
|
})));
|
|
|
|
it("Key change", _asyncToGenerator(regeneratorRuntime.mark(function _callee9() {
|
|
return regeneratorRuntime.wrap(function _callee9$(_context9) {
|
|
while (1) {
|
|
switch (_context9.prev = _context9.next) {
|
|
case 0:
|
|
_context9.next = 2;
|
|
return runTest(directory, "key-change.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context9.stop();
|
|
}
|
|
}
|
|
}, _callee9, _this);
|
|
})));
|
|
|
|
it("Component type change", _asyncToGenerator(regeneratorRuntime.mark(function _callee10() {
|
|
return regeneratorRuntime.wrap(function _callee10$(_context10) {
|
|
while (1) {
|
|
switch (_context10.prev = _context10.next) {
|
|
case 0:
|
|
_context10.next = 2;
|
|
return runTest(directory, "type-change.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context10.stop();
|
|
}
|
|
}
|
|
}, _callee10, _this);
|
|
})));
|
|
|
|
it("Component type same", _asyncToGenerator(regeneratorRuntime.mark(function _callee11() {
|
|
return regeneratorRuntime.wrap(function _callee11$(_context11) {
|
|
while (1) {
|
|
switch (_context11.prev = _context11.next) {
|
|
case 0:
|
|
_context11.next = 2;
|
|
return runTest(directory, "type-same.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context11.stop();
|
|
}
|
|
}
|
|
}, _callee11, _this);
|
|
})));
|
|
|
|
it("Dynamic props", _asyncToGenerator(regeneratorRuntime.mark(function _callee12() {
|
|
return regeneratorRuntime.wrap(function _callee12$(_context12) {
|
|
while (1) {
|
|
switch (_context12.prev = _context12.next) {
|
|
case 0:
|
|
_context12.next = 2;
|
|
return runTest(directory, "dynamic-props.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context12.stop();
|
|
}
|
|
}
|
|
}, _callee12, _this);
|
|
})));
|
|
|
|
it("Dynamic context", _asyncToGenerator(regeneratorRuntime.mark(function _callee13() {
|
|
return regeneratorRuntime.wrap(function _callee13$(_context13) {
|
|
while (1) {
|
|
switch (_context13.prev = _context13.next) {
|
|
case 0:
|
|
_context13.next = 2;
|
|
return runTest(directory, "dynamic-context.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context13.stop();
|
|
}
|
|
}
|
|
}, _callee13, _this);
|
|
})));
|
|
|
|
it("React.cloneElement", _asyncToGenerator(regeneratorRuntime.mark(function _callee14() {
|
|
return regeneratorRuntime.wrap(function _callee14$(_context14) {
|
|
while (1) {
|
|
switch (_context14.prev = _context14.next) {
|
|
case 0:
|
|
_context14.next = 2;
|
|
return runTest(directory, "clone-element.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context14.stop();
|
|
}
|
|
}
|
|
}, _callee14, _this);
|
|
})));
|
|
|
|
it("Return text", _asyncToGenerator(regeneratorRuntime.mark(function _callee15() {
|
|
return regeneratorRuntime.wrap(function _callee15$(_context15) {
|
|
while (1) {
|
|
switch (_context15.prev = _context15.next) {
|
|
case 0:
|
|
_context15.next = 2;
|
|
return runTest(directory, "return-text.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context15.stop();
|
|
}
|
|
}
|
|
}, _callee15, _this);
|
|
})));
|
|
|
|
it("Return undefined", _asyncToGenerator(regeneratorRuntime.mark(function _callee16() {
|
|
return regeneratorRuntime.wrap(function _callee16$(_context16) {
|
|
while (1) {
|
|
switch (_context16.prev = _context16.next) {
|
|
case 0:
|
|
// this test will cause a React console.error to show
|
|
// we monkey patch it to stop it polluting the test output
|
|
// with a false-negative error
|
|
global.console.error = function () {};
|
|
_context16.prev = 1;
|
|
_context16.next = 4;
|
|
return runTest(directory, "return-undefined.js");
|
|
|
|
case 4:
|
|
_context16.prev = 4;
|
|
|
|
global.console.error = originalConsoleError;
|
|
return _context16.finish(4);
|
|
|
|
case 7:
|
|
case "end":
|
|
return _context16.stop();
|
|
}
|
|
}
|
|
}, _callee16, _this, [[1,, 4, 7]]);
|
|
})));
|
|
|
|
it("Class component as root", _asyncToGenerator(regeneratorRuntime.mark(function _callee17() {
|
|
return regeneratorRuntime.wrap(function _callee17$(_context17) {
|
|
while (1) {
|
|
switch (_context17.prev = _context17.next) {
|
|
case 0:
|
|
_context17.next = 2;
|
|
return runTest(directory, "class-root.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context17.stop();
|
|
}
|
|
}
|
|
}, _callee17, _this);
|
|
})));
|
|
|
|
it("Class component as root with multiple render methods", _asyncToGenerator(regeneratorRuntime.mark(function _callee18() {
|
|
return regeneratorRuntime.wrap(function _callee18$(_context18) {
|
|
while (1) {
|
|
switch (_context18.prev = _context18.next) {
|
|
case 0:
|
|
_context18.next = 2;
|
|
return runTest(directory, "class-root-with-render-methods.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context18.stop();
|
|
}
|
|
}
|
|
}, _callee18, _this);
|
|
})));
|
|
|
|
it("Class component as root with props", _asyncToGenerator(regeneratorRuntime.mark(function _callee19() {
|
|
return regeneratorRuntime.wrap(function _callee19$(_context19) {
|
|
while (1) {
|
|
switch (_context19.prev = _context19.next) {
|
|
case 0:
|
|
_context19.next = 2;
|
|
return runTest(directory, "class-root-with-props.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context19.stop();
|
|
}
|
|
}
|
|
}, _callee19, _this);
|
|
})));
|
|
|
|
it("Class component as root with state", _asyncToGenerator(regeneratorRuntime.mark(function _callee20() {
|
|
return regeneratorRuntime.wrap(function _callee20$(_context20) {
|
|
while (1) {
|
|
switch (_context20.prev = _context20.next) {
|
|
case 0:
|
|
_context20.next = 2;
|
|
return runTest(directory, "class-root-with-state.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context20.stop();
|
|
}
|
|
}
|
|
}, _callee20, _this);
|
|
})));
|
|
|
|
it("Class component as root with refs", _asyncToGenerator(regeneratorRuntime.mark(function _callee21() {
|
|
return regeneratorRuntime.wrap(function _callee21$(_context21) {
|
|
while (1) {
|
|
switch (_context21.prev = _context21.next) {
|
|
case 0:
|
|
_context21.next = 2;
|
|
return runTest(directory, "class-root-with-refs.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context21.stop();
|
|
}
|
|
}
|
|
}, _callee21, _this);
|
|
})));
|
|
|
|
it("Class component as root with instance variables", _asyncToGenerator(regeneratorRuntime.mark(function _callee22() {
|
|
return regeneratorRuntime.wrap(function _callee22$(_context22) {
|
|
while (1) {
|
|
switch (_context22.prev = _context22.next) {
|
|
case 0:
|
|
_context22.next = 2;
|
|
return runTest(directory, "class-root-with-instance-vars.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context22.stop();
|
|
}
|
|
}
|
|
}, _callee22, _this);
|
|
})));
|
|
|
|
it("Class component as root with instance variables #2", _asyncToGenerator(regeneratorRuntime.mark(function _callee23() {
|
|
return regeneratorRuntime.wrap(function _callee23$(_context23) {
|
|
while (1) {
|
|
switch (_context23.prev = _context23.next) {
|
|
case 0:
|
|
_context23.next = 2;
|
|
return runTest(directory, "class-root-with-instance-vars-2.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context23.stop();
|
|
}
|
|
}
|
|
}, _callee23, _this);
|
|
})));
|
|
});
|
|
|
|
describe("Class component folding", function () {
|
|
var directory = "class-components";
|
|
|
|
it("Simple classes", _asyncToGenerator(regeneratorRuntime.mark(function _callee24() {
|
|
return regeneratorRuntime.wrap(function _callee24$(_context24) {
|
|
while (1) {
|
|
switch (_context24.prev = _context24.next) {
|
|
case 0:
|
|
_context24.next = 2;
|
|
return runTest(directory, "simple-classes.js");
|
|
|
|
case 2:
|
|
case "end":
|
|
return _context24.stop();
|
|
}
|
|
}
|
|
}, _callee24, _this);
|
|
})));
|
|
});
|
|
});
|
|
}
|
|
|
|
runTestSuite(true);
|
|
runTestSuite(false);
|
|
//# sourceMappingURL=test-react.js.map
|