UNPKG

3.43 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2016-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var _assign = require('object-assign');
12
13var _extends = _assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
14
15function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
16
17var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
18
19function getRootDisplayNames() {
20 return ReactComponentTreeHook.getRootIDs().map(ReactComponentTreeHook.getDisplayName);
21}
22
23function getRegisteredDisplayNames() {
24 return ReactComponentTreeHook.getRegisteredIDs().map(ReactComponentTreeHook.getDisplayName);
25}
26
27function expectTree(rootID, expectedTree, parentPath) {
28 var displayName = ReactComponentTreeHook.getDisplayName(rootID);
29 var ownerID = ReactComponentTreeHook.getOwnerID(rootID);
30 var parentID = ReactComponentTreeHook.getParentID(rootID);
31 var childIDs = ReactComponentTreeHook.getChildIDs(rootID);
32 var text = ReactComponentTreeHook.getText(rootID);
33 var element = ReactComponentTreeHook.getElement(rootID);
34 var path = parentPath ? parentPath + ' > ' + displayName : displayName;
35
36 function expectEqual(actual, expected, name) {
37 // Get Jasmine to print descriptive error messages.
38 // We pass path so that we know where the mismatch occurred.
39 expect(_defineProperty({
40 path: path
41 }, name, actual)).toEqual(_defineProperty({
42 path: path
43 }, name, expected));
44 }
45
46 if (expectedTree.parentDisplayName !== undefined) {
47 expectEqual(ReactComponentTreeHook.getDisplayName(parentID), expectedTree.parentDisplayName, 'parentDisplayName');
48 }
49 if (expectedTree.ownerDisplayName !== undefined) {
50 expectEqual(ReactComponentTreeHook.getDisplayName(ownerID), expectedTree.ownerDisplayName, 'ownerDisplayName');
51 }
52 if (expectedTree.parentID !== undefined) {
53 expectEqual(parentID, expectedTree.parentID, 'parentID');
54 }
55 if (expectedTree.text !== undefined) {
56 expectEqual(text, expectedTree.text, 'text');
57 expectEqual('' + element, expectedTree.text, 'element.toString()');
58 } else {
59 expectEqual(text, null, 'text');
60 }
61 if (expectedTree.element !== undefined) {
62 // TODO: Comparing elements makes tests run out of memory on errors.
63 // For now, compare just types.
64 expectEqual(element && element.type, expectedTree.element && expectedTree.element.type, 'element.type');
65 } else if (text == null) {
66 expectEqual(typeof element, 'object', 'typeof element');
67 }
68 if (expectedTree.children !== undefined) {
69 expectEqual(childIDs.length, expectedTree.children.length, 'children.length');
70 for (var i = 0; i < childIDs.length; i++) {
71 expectTree(childIDs[i], _extends({ parentID: rootID }, expectedTree.children[i]), path);
72 }
73 } else {
74 expectEqual(childIDs, [], 'childIDs');
75 }
76}
77
78var ReactComponentTreeTestUtils = {
79 expectTree: expectTree,
80 getRootDisplayNames: getRootDisplayNames,
81 getRegisteredDisplayNames: getRegisteredDisplayNames
82};
83
84module.exports = ReactComponentTreeTestUtils;
\No newline at end of file