Skip to content

Commit d80335c

Browse files
kfirappsfacebook-github-bot
authored andcommitted
Add tests for CKRenderTreeNodeWithChild
Summary: Self-explanatory Reviewed By: gkassabli Differential Revision: D7830085 fbshipit-source-id: 0cebcb9899fa6d13a4d435bf5dd1226f923af562
1 parent 0551562 commit d80335c

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

ComponentKitTests/CKBuildComponentTreeTests.mm

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "CKComponentInternal.h"
1717
#import "CKButtonComponent.h"
1818
#import "CKTreeNode.h"
19+
#import "CKRenderTreeNodeWithChild.h"
1920
#import "CKRenderTreeNodeWithChildren.h"
2021
#import "CKThreadLocalComponentScope.h"
2122

@@ -43,22 +44,25 @@ @interface CKBuildComponentTreeTests : XCTestCase
4344
@end
4445

4546
@implementation CKBuildComponentTreeTests
47+
{
48+
BOOL _forceParent;
49+
}
4650

4751
#pragma mark - CKComponent
4852

4953
- (void)test_buildComponentTree_onCKComponent
5054
{
5155
CKRenderTreeNodeWithChildren *root = [[CKRenderTreeNodeWithChildren alloc] init];
5256
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];
5458

5559
XCTAssertEqual(root.children.size(), 1);
5660
XCTAssertEqual(root.children[0].component, c);
5761

5862
// Simulate a second tree creation.
5963
CKRenderTreeNodeWithChildren *root2 = [[CKRenderTreeNodeWithChildren alloc] init];
6064
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];
6266
XCTAssertTrue(areTreesEqual(root, root2));
6367
}
6468

@@ -69,28 +73,33 @@ - (void)test_buildComponentTree_onCKRenderComponent
6973
CKRenderTreeNodeWithChildren *root = [[CKRenderTreeNodeWithChildren alloc] init];
7074
CKComponent *c = [CKComponent newWithView:{} size:{}];
7175
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];
7377

7478
// Make sure the root has only one child.
7579
XCTAssertEqual(root.children.size(), 1);
7680
CKTreeNode *singleChildNode = root.children[0];
7781
verifyChildToParentConnection(root, singleChildNode, renderComponent);
7882

7983
// 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]])) {
8186
CKRenderTreeNodeWithChildren *parentNode = (CKRenderTreeNodeWithChildren *)singleChildNode;
8287
XCTAssertEqual(parentNode.children.size(), 1);
8388
CKTreeNode *componentNode = parentNode.children[0];
8489
verifyChildToParentConnection(parentNode, componentNode, c);
8590
} 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+
}
8796
}
8897

8998
// Simulate a second tree creation.
9099
CKRenderTreeNodeWithChildren *root2 = [[CKRenderTreeNodeWithChildren alloc] init];
91100
CKComponent *c2 = [CKComponent newWithView:{} size:{}];
92101
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];
94103
XCTAssertTrue(areTreesEqual(root, root2));
95104
}
96105

@@ -166,16 +175,23 @@ - (void)test_buildComponentTree_onCKRenderWithChildrenComponent_MakeGroupAnOwner
166175
XCTAssertTrue(areTreesEqual(root, root2));
167176
}
168177

178+
- (void)test_buildComponentTree_withForceParent
179+
{
180+
_forceParent = YES;
181+
[self test_buildComponentTree_onCKComponent];
182+
[self test_buildComponentTree_onCKRenderComponent];
183+
}
184+
169185
#pragma mark - Helpers
170186

171-
static BOOL verifyChildToParentConnection(CKRenderTreeNodeWithChildren * parentNode, CKTreeNode *childNode, CKComponent *c) {
187+
static BOOL verifyChildToParentConnection(id<CKTreeNodeWithChildrenProtocol> parentNode, CKTreeNode *childNode, CKComponent *c) {
172188
auto const componentKey = [childNode componentKey];
173189
auto const childComponent = [parentNode childForComponentKey:componentKey].component;
174190
return [childComponent isEqual:c];
175191
}
176192

177193
/** 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) {
179195
// Verify that the root holds two components has its direct children
180196
NSMutableSet<CKComponent *> *componentsFromTheTree = [NSMutableSet set];
181197
for (auto const node : parentNode.children) {
@@ -186,7 +202,7 @@ static BOOL verifyComponentsInNode(CKRenderTreeNodeWithChildren *parentNode, NSA
186202
}
187203

188204
/** 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) {
190206
NSMutableSet<NSString *> *lhsChildrenIdentifiers = [NSMutableSet set];
191207
treeChildrenIdentifiers(lhs, lhsChildrenIdentifiers, 0);
192208
NSMutableSet<NSString *> *rhsChildrenIdentifiers = [NSMutableSet set];
@@ -195,7 +211,7 @@ static BOOL areTreesEqual(CKRenderTreeNodeWithChildren *lhs, CKRenderTreeNodeWit
195211
}
196212

197213
/** 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) {
199215
for (auto const childNode : node.children) {
200216
// We add the child identifier + its level in the tree.
201217
[identifiers addObject:[NSString stringWithFormat:@"%d-%d",childNode.nodeIdentifier, level]];

ComponentKitTests/CKTreeNodeTests.mm

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "CKButtonComponent.h"
1818
#import "CKTreeNode.h"
1919
#import "CKRenderTreeNode.h"
20+
#import "CKRenderTreeNodeWithChild.h"
2021
#import "CKRenderTreeNodeWithChildren.h"
2122
#import "CKThreadLocalComponentScope.h"
2223

@@ -128,6 +129,53 @@ - (void)test_childForComponentKey_onCKRenderTreeNodeWithChildren_withMultipleChi
128129
XCTAssertTrue(areTreesEqual(root, root2));
129130
}
130131

132+
#pragma mark - CKRenderTreeNodeWithChild
133+
134+
- (void)test_childForComponentKey_onCKRenderTreeNodeWithChild {
135+
// Simulate first component tree creation
136+
CKRenderTreeNodeWithChild *root1 = [[CKRenderTreeNodeWithChild alloc] init];
137+
auto const component1 = [CKComponent newWithView:{} size:{}];
138+
CKTreeNode *childNode1 = [[CKTreeNode alloc] initWithComponent:component1
139+
owner:root1
140+
previousOwner:nil
141+
scopeRoot:nil
142+
stateUpdates:{}];
143+
144+
// Simulate a component tree creation due to a state update
145+
CKRenderTreeNodeWithChild *root2 = [[CKRenderTreeNodeWithChild alloc] init];
146+
auto const component2 = [CKComponent newWithView:{} size:{}];
147+
CKTreeNode *childNode2 = [[CKTreeNode alloc] initWithComponent:component2
148+
owner:root2
149+
previousOwner:root1
150+
scopeRoot:nil
151+
stateUpdates:{}];
152+
153+
XCTAssertTrue(verifyChildToParentConnection(root1, childNode1, component1));
154+
XCTAssertTrue(verifyChildToParentConnection(root2, childNode2, component2));
155+
}
156+
157+
- (void)test_nodeIdentifier_onCKRenderTreeNodeWithChild_betweenGenerations {
158+
// Simulate first component tree creation
159+
CKRenderTreeNodeWithChild *root1 = [[CKRenderTreeNodeWithChild alloc] init];
160+
auto const component1 = [CKComponent newWithView:{} size:{}];
161+
CKTreeNode *childNode1 = [[CKTreeNode alloc] initWithComponent:component1
162+
owner:root1
163+
previousOwner:nil
164+
scopeRoot:nil
165+
stateUpdates:{}];
166+
167+
// Simulate a component tree creation due to a state update
168+
CKRenderTreeNodeWithChild *root2 = [[CKRenderTreeNodeWithChild alloc] init];
169+
auto const component2 = [CKComponent newWithView:{} size:{}];
170+
CKTreeNode *childNode2 = [[CKTreeNode alloc] initWithComponent:component2
171+
owner:root2
172+
previousOwner:root1
173+
scopeRoot:nil
174+
stateUpdates:{}];
175+
176+
XCTAssertEqual(childNode1.nodeIdentifier, childNode2.nodeIdentifier);
177+
}
178+
131179
#pragma mark - State
132180

133181
- (void)test_stateUpdate_onCKTreeNode
@@ -292,29 +340,29 @@ - (void)_test_initialState_withComponent:(CKComponent *)c initialState:(id)initi
292340
XCTAssertNotNil(node.handle);
293341
}
294342

295-
static BOOL verifyChildToParentConnection(CKRenderTreeNodeWithChildren * parentNode, CKTreeNode *childNode, CKComponent *c) {
343+
static BOOL verifyChildToParentConnection(id<CKTreeNodeWithChildrenProtocol> parentNode, CKTreeNode *childNode, CKComponent *c) {
296344
auto const componentKey = [childNode componentKey];
297345
auto const childComponent = [parentNode childForComponentKey:componentKey].component;
298346
return [childComponent isEqual:c];
299347
}
300348

301-
static NSMutableArray<CKTreeNode*> *createsNodesForComponentsWithOwner(CKRenderTreeNodeWithChildren *owner,
302-
CKRenderTreeNodeWithChildren *previousOwner,
349+
static NSMutableArray<CKTreeNode*> *createsNodesForComponentsWithOwner(id<CKTreeNodeWithChildrenProtocol> owner,
350+
id<CKTreeNodeWithChildrenProtocol> previousOwner,
303351
NSArray<CKComponent *> *components) {
304352
NSMutableArray<CKTreeNode*> *nodes = [NSMutableArray array];
305353
for (CKComponent *component in components) {
306354
CKTreeNode *childNode = [[CKTreeNode alloc] initWithComponent:component
307-
owner:owner
308-
previousOwner:previousOwner
309-
scopeRoot:nil
310-
stateUpdates:{}];
355+
owner:owner
356+
previousOwner:previousOwner
357+
scopeRoot:nil
358+
stateUpdates:{}];
311359
[nodes addObject:childNode];
312360
}
313361
return nodes;
314362
}
315363

316364
/** Compare the children of the trees recursively; returns true if the two trees are equal */
317-
static BOOL areTreesEqual(CKRenderTreeNodeWithChildren *lhs, CKRenderTreeNodeWithChildren *rhs) {
365+
static BOOL areTreesEqual(id<CKTreeNodeWithChildrenProtocol> lhs, id<CKTreeNodeWithChildrenProtocol> rhs) {
318366
NSMutableSet<NSString *> *lhsChildrenIdentifiers = [NSMutableSet set];
319367
treeChildrenIdentifiers(lhs, lhsChildrenIdentifiers, 0);
320368
NSMutableSet<NSString *> *rhsChildrenIdentifiers = [NSMutableSet set];
@@ -323,7 +371,7 @@ static BOOL areTreesEqual(CKRenderTreeNodeWithChildren *lhs, CKRenderTreeNodeWit
323371
}
324372

325373
/** Iterate recursively over the tree and add its node identifiers to the set */
326-
static void treeChildrenIdentifiers(CKRenderTreeNodeWithChildren *node, NSMutableSet<NSString *> *identifiers, int level) {
374+
static void treeChildrenIdentifiers(id<CKTreeNodeWithChildrenProtocol> node, NSMutableSet<NSString *> *identifiers, int level) {
327375
for (auto const childNode : node.children) {
328376
// We add the child identifier + its level in the tree.
329377
[identifiers addObject:[NSString stringWithFormat:@"%d-%d",childNode.nodeIdentifier, level]];

0 commit comments

Comments
 (0)