forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactComponentTreeTestUtils.js
More file actions
105 lines (96 loc) · 2.94 KB
/
ReactComponentTreeTestUtils.js
File metadata and controls
105 lines (96 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule ReactComponentTreeTestUtils
*/
'use strict';
var ReactComponentTreeHook = require('ReactComponentTreeHook');
function getRootDisplayNames() {
return ReactComponentTreeHook.getRootIDs().map(
ReactComponentTreeHook.getDisplayName,
);
}
function getRegisteredDisplayNames() {
return ReactComponentTreeHook.getRegisteredIDs().map(
ReactComponentTreeHook.getDisplayName,
);
}
function expectTree(rootID, expectedTree, parentPath) {
var displayName = ReactComponentTreeHook.getDisplayName(rootID);
var ownerID = ReactComponentTreeHook.getOwnerID(rootID);
var parentID = ReactComponentTreeHook.getParentID(rootID);
var childIDs = ReactComponentTreeHook.getChildIDs(rootID);
var text = ReactComponentTreeHook.getText(rootID);
var element = ReactComponentTreeHook.getElement(rootID);
var path = parentPath ? `${parentPath} > ${displayName}` : displayName;
function expectEqual(actual, expected, name) {
// Get Jasmine to print descriptive error messages.
// We pass path so that we know where the mismatch occurred.
expect({
path,
[name]: actual,
}).toEqual({
path,
[name]: expected,
});
}
if (expectedTree.parentDisplayName !== undefined) {
expectEqual(
ReactComponentTreeHook.getDisplayName(parentID),
expectedTree.parentDisplayName,
'parentDisplayName',
);
}
if (expectedTree.ownerDisplayName !== undefined) {
expectEqual(
ReactComponentTreeHook.getDisplayName(ownerID),
expectedTree.ownerDisplayName,
'ownerDisplayName',
);
}
if (expectedTree.parentID !== undefined) {
expectEqual(parentID, expectedTree.parentID, 'parentID');
}
if (expectedTree.text !== undefined) {
expectEqual(text, expectedTree.text, 'text');
expectEqual('' + element, expectedTree.text, 'element.toString()');
} else {
expectEqual(text, null, 'text');
}
if (expectedTree.element !== undefined) {
// TODO: Comparing elements makes tests run out of memory on errors.
// For now, compare just types.
expectEqual(
element && element.type,
expectedTree.element && expectedTree.element.type,
'element.type',
);
} else if (text == null) {
expectEqual(typeof element, 'object', 'typeof element');
}
if (expectedTree.children !== undefined) {
expectEqual(
childIDs.length,
expectedTree.children.length,
'children.length',
);
for (var i = 0; i < childIDs.length; i++) {
expectTree(
childIDs[i],
{parentID: rootID, ...expectedTree.children[i]},
path,
);
}
} else {
expectEqual(childIDs, [], 'childIDs');
}
}
var ReactComponentTreeTestUtils = {
expectTree,
getRootDisplayNames,
getRegisteredDisplayNames,
};
module.exports = ReactComponentTreeTestUtils;