1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _assign = require('object-assign');
|
12 |
|
13 | var _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 |
|
15 | function _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 |
|
17 | var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
|
18 |
|
19 | function getRootDisplayNames() {
|
20 | return ReactComponentTreeHook.getRootIDs().map(ReactComponentTreeHook.getDisplayName);
|
21 | }
|
22 |
|
23 | function getRegisteredDisplayNames() {
|
24 | return ReactComponentTreeHook.getRegisteredIDs().map(ReactComponentTreeHook.getDisplayName);
|
25 | }
|
26 |
|
27 | function 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 |
|
38 |
|
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 |
|
63 |
|
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 |
|
78 | var ReactComponentTreeTestUtils = {
|
79 | expectTree: expectTree,
|
80 | getRootDisplayNames: getRootDisplayNames,
|
81 | getRegisteredDisplayNames: getRegisteredDisplayNames
|
82 | };
|
83 |
|
84 | module.exports = ReactComponentTreeTestUtils; |
\ | No newline at end of file |