16
16
#import " CKComponentInternal.h"
17
17
#import " CKButtonComponent.h"
18
18
#import " CKTreeNode.h"
19
+ #import " CKRenderTreeNodeWithChild.h"
19
20
#import " CKRenderTreeNodeWithChildren.h"
20
21
#import " CKThreadLocalComponentScope.h"
21
22
@@ -43,22 +44,25 @@ @interface CKBuildComponentTreeTests : XCTestCase
43
44
@end
44
45
45
46
@implementation CKBuildComponentTreeTests
47
+ {
48
+ BOOL _forceParent;
49
+ }
46
50
47
51
#pragma mark - CKComponent
48
52
49
53
- (void )test_buildComponentTree_onCKComponent
50
54
{
51
55
CKRenderTreeNodeWithChildren *root = [[CKRenderTreeNodeWithChildren alloc ] init ];
52
56
CKComponent *c = [CKComponent newWithView: {} size: {}];
53
- [c buildComponentTree: root previousOwner: nil scopeRoot: nil stateUpdates: {} forceParent: NO ];
57
+ [c buildComponentTree: root previousOwner: nil scopeRoot: nil stateUpdates: {} forceParent: _forceParent ];
54
58
55
59
XCTAssertEqual (root.children .size (), 1 );
56
60
XCTAssertEqual (root.children [0 ].component , c);
57
61
58
62
// Simulate a second tree creation.
59
63
CKRenderTreeNodeWithChildren *root2 = [[CKRenderTreeNodeWithChildren alloc ] init ];
60
64
CKComponent *c2 = [CKComponent newWithView: {} size: {}];
61
- [c2 buildComponentTree: root2 previousOwner: root scopeRoot: nil stateUpdates: {} forceParent: NO ];
65
+ [c2 buildComponentTree: root2 previousOwner: root scopeRoot: nil stateUpdates: {} forceParent: _forceParent ];
62
66
XCTAssertTrue (areTreesEqual (root, root2));
63
67
}
64
68
@@ -69,28 +73,33 @@ - (void)test_buildComponentTree_onCKRenderComponent
69
73
CKRenderTreeNodeWithChildren *root = [[CKRenderTreeNodeWithChildren alloc ] init ];
70
74
CKComponent *c = [CKComponent newWithView: {} size: {}];
71
75
CKRenderComponent *renderComponent = [CKComponentTreeTestComponent_Render newWithComponent: c];
72
- [renderComponent buildComponentTree: root previousOwner: nil scopeRoot: nil stateUpdates: {} forceParent: NO ];
76
+ [renderComponent buildComponentTree: root previousOwner: nil scopeRoot: nil stateUpdates: {} forceParent: _forceParent ];
73
77
74
78
// Make sure the root has only one child.
75
79
XCTAssertEqual (root.children .size (), 1 );
76
80
CKTreeNode *singleChildNode = root.children [0 ];
77
81
verifyChildToParentConnection (root, singleChildNode, renderComponent);
78
82
79
83
// Check the next level of the tree
80
- if ([singleChildNode isKindOfClass: [CKRenderTreeNodeWithChildren class ]]) {
84
+ if ((!_forceParent && [singleChildNode isKindOfClass: [CKRenderTreeNodeWithChildren class ]]) ||
85
+ (_forceParent && [singleChildNode isKindOfClass: [CKRenderTreeNodeWithChild class ]])) {
81
86
CKRenderTreeNodeWithChildren *parentNode = (CKRenderTreeNodeWithChildren *)singleChildNode;
82
87
XCTAssertEqual (parentNode.children .size (), 1 );
83
88
CKTreeNode *componentNode = parentNode.children [0 ];
84
89
verifyChildToParentConnection (parentNode, componentNode, c);
85
90
} else {
86
- XCTFail (@" singleChildNode has to be CKRenderTreeNodeWithChildren as it has children." );
91
+ if (_forceParent) {
92
+ XCTFail (@" singleChildNode has to be CKRenderTreeNodeWithChild as it has a child." );
93
+ } else {
94
+ XCTFail (@" singleChildNode has to be CKRenderTreeNodeWithChildren as it has children." );
95
+ }
87
96
}
88
97
89
98
// Simulate a second tree creation.
90
99
CKRenderTreeNodeWithChildren *root2 = [[CKRenderTreeNodeWithChildren alloc ] init ];
91
100
CKComponent *c2 = [CKComponent newWithView: {} size: {}];
92
101
CKRenderComponent *renderComponent2 = [CKComponentTreeTestComponent_Render newWithComponent: c2];
93
- [renderComponent2 buildComponentTree: root2 previousOwner: root scopeRoot: nil stateUpdates: {} forceParent: NO ];
102
+ [renderComponent2 buildComponentTree: root2 previousOwner: root scopeRoot: nil stateUpdates: {} forceParent: _forceParent ];
94
103
XCTAssertTrue (areTreesEqual (root, root2));
95
104
}
96
105
@@ -166,16 +175,23 @@ - (void)test_buildComponentTree_onCKRenderWithChildrenComponent_MakeGroupAnOwner
166
175
XCTAssertTrue (areTreesEqual (root, root2));
167
176
}
168
177
178
+ - (void )test_buildComponentTree_withForceParent
179
+ {
180
+ _forceParent = YES ;
181
+ [self test_buildComponentTree_onCKComponent ];
182
+ [self test_buildComponentTree_onCKRenderComponent ];
183
+ }
184
+
169
185
#pragma mark - Helpers
170
186
171
- static BOOL verifyChildToParentConnection (CKRenderTreeNodeWithChildren * parentNode, CKTreeNode *childNode, CKComponent *c) {
187
+ static BOOL verifyChildToParentConnection (id <CKTreeNodeWithChildrenProtocol> parentNode, CKTreeNode *childNode, CKComponent *c) {
172
188
auto const componentKey = [childNode componentKey ];
173
189
auto const childComponent = [parentNode childForComponentKey: componentKey].component ;
174
190
return [childComponent isEqual: c];
175
191
}
176
192
177
193
/* * Compare the components array to the components in the children nodes of 'parentNode' */
178
- static BOOL verifyComponentsInNode (CKRenderTreeNodeWithChildren * parentNode, NSArray <CKComponent *> *components) {
194
+ static BOOL verifyComponentsInNode (id <CKTreeNodeWithChildrenProtocol> parentNode, NSArray <CKComponent *> *components) {
179
195
// Verify that the root holds two components has its direct children
180
196
NSMutableSet <CKComponent *> *componentsFromTheTree = [NSMutableSet set ];
181
197
for (auto const node : parentNode.children ) {
@@ -186,7 +202,7 @@ static BOOL verifyComponentsInNode(CKRenderTreeNodeWithChildren *parentNode, NSA
186
202
}
187
203
188
204
/* * Compare the children of the trees recursively; returns true if the two trees are equal */
189
- static BOOL areTreesEqual (CKRenderTreeNodeWithChildren * lhs, CKRenderTreeNodeWithChildren * rhs) {
205
+ static BOOL areTreesEqual (id <CKTreeNodeWithChildrenProtocol> lhs, id <CKTreeNodeWithChildrenProtocol> rhs) {
190
206
NSMutableSet <NSString *> *lhsChildrenIdentifiers = [NSMutableSet set ];
191
207
treeChildrenIdentifiers (lhs, lhsChildrenIdentifiers, 0 );
192
208
NSMutableSet <NSString *> *rhsChildrenIdentifiers = [NSMutableSet set ];
@@ -195,7 +211,7 @@ static BOOL areTreesEqual(CKRenderTreeNodeWithChildren *lhs, CKRenderTreeNodeWit
195
211
}
196
212
197
213
/* * Iterate recursively over the tree and add its node identifiers to the set */
198
- static void treeChildrenIdentifiers (CKRenderTreeNodeWithChildren * node, NSMutableSet <NSString *> *identifiers, int level) {
214
+ static void treeChildrenIdentifiers (id <CKTreeNodeWithChildrenProtocol> node, NSMutableSet <NSString *> *identifiers, int level) {
199
215
for (auto const childNode : node.children ) {
200
216
// We add the child identifier + its level in the tree.
201
217
[identifiers addObject: [NSString stringWithFormat: @" %d -%d " ,childNode.nodeIdentifier, level]];
0 commit comments