react-native-navigation的迁移库

RNNControllerFactoryTest.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #import <XCTest/XCTest.h>
  2. #import "RNNControllerFactory.h"
  3. #import "RNNRootViewController.h"
  4. #import "RNNSideMenuController.h"
  5. #import "RNNSideMenuChildVC.h"
  6. #import "RNNNavigationController.h"
  7. #import "RNNTabBarController.h"
  8. #import "RNNSplitViewController.h"
  9. @interface RNNControllerFactoryTest : XCTestCase
  10. @property (nonatomic, strong) id<RNNRootViewCreator> creator;
  11. @property (nonatomic, strong) RNNControllerFactory *factory;
  12. @property (nonatomic, strong) RNNStore *store;
  13. @end
  14. @implementation RNNControllerFactoryTest
  15. - (void)setUp {
  16. [super setUp];
  17. self.creator = nil;
  18. self.store = [RNNStore new];
  19. self.factory = [[RNNControllerFactory alloc] initWithRootViewCreator:self.creator eventEmitter:nil andBridge:nil];
  20. }
  21. - (void)tearDown {
  22. [super tearDown];
  23. }
  24. - (void)testCreateLayout_EmptyLayout {
  25. XCTAssertThrows([self.factory createLayout:@{} saveToStore:self.store]);
  26. }
  27. - (void)testCreateLayout_ComponentLayout {
  28. NSDictionary* layout = @{@"id": @"cntId",
  29. @"type": @"Component",
  30. @"data": @{},
  31. @"children": @[]};
  32. id ans = [self.factory createLayout:layout saveToStore:self.store];
  33. XCTAssertTrue([ans isMemberOfClass:[RNNRootViewController class]]);
  34. }
  35. - (void)testCreateLayout_ExternalComponentLayout {
  36. [_store registerExternalComponent:@"externalComponent" callback:^UIViewController *(NSDictionary *props, RCTBridge *bridge) {
  37. return [UIViewController new];
  38. }];
  39. NSDictionary* layout = @{@"id": @"cntId",
  40. @"type": @"ExternalComponent",
  41. @"data": @{@"name": @"externalComponent"},
  42. @"children": @[]};
  43. id ans = [self.factory createLayout:layout saveToStore:self.store];
  44. XCTAssertTrue([ans isMemberOfClass:[RNNRootViewController class]]);
  45. }
  46. - (void)testCreateLayout_ComponentStackLayout {
  47. NSDictionary* layout = @{@"id": @"cntId",
  48. @"type": @"Stack",
  49. @"data": @{},
  50. @"children": @[]};
  51. id ans = [self.factory createLayout:layout saveToStore:self.store];
  52. XCTAssertTrue([ans isMemberOfClass:[RNNNavigationController class]]);
  53. }
  54. - (void)testCreateLayout_SplitViewLayout {
  55. NSDictionary* layout = @{@"id": @"cntId",
  56. @"type": @"SplitView",
  57. @"data": @{},
  58. @"children": @[
  59. @{@"id": @"cntId_2",
  60. @"type": @"Component",
  61. @"data": @{},
  62. @"children": @[]},
  63. @{@"id": @"cntId_3",
  64. @"type": @"Component",
  65. @"data": @{},
  66. @"children": @[]}]};
  67. id ans = [self.factory createLayout:layout saveToStore:self.store];
  68. XCTAssertTrue([ans isMemberOfClass:[RNNSplitViewController class]]);
  69. }
  70. - (void)testCreateLayout_ComponentStackLayoutRecursive {
  71. NSDictionary* layout = @{@"id": @"cntId",
  72. @"type": @"Stack",
  73. @"data": @{},
  74. @"children": @[
  75. @{@"id": @"cntId_2",
  76. @"type": @"Component",
  77. @"data": @{},
  78. @"children": @[]}]};
  79. RNNNavigationController* ans = (RNNNavigationController*) [self.factory createLayout:layout saveToStore:self.store];
  80. XCTAssertTrue([ans isMemberOfClass:[RNNNavigationController class]]);
  81. XCTAssertTrue(ans.childViewControllers.count == 1);
  82. XCTAssertTrue([ans.childViewControllers[0] isMemberOfClass:[RNNRootViewController class]]);
  83. }
  84. - (void)testCreateLayout_BottomTabsLayout {
  85. NSDictionary* layout = @{
  86. @"id": @"cntId",
  87. @"type": @"BottomTabs",
  88. @"data": @{},
  89. @"children": @[
  90. @{@"id": @"cntId_2",
  91. @"type": @"Stack",
  92. @"data": @{},
  93. @"children": @[
  94. @{@"id": @"cntId_3",
  95. @"type": @"Component",
  96. @"data": @{},
  97. @"children": @[]}]}]};
  98. RNNTabBarController* tabBar = (RNNTabBarController*) [self.factory createLayout:layout saveToStore:self.store];
  99. XCTAssertTrue([tabBar isMemberOfClass:[RNNTabBarController class]]);
  100. XCTAssertTrue(tabBar.childViewControllers.count == 1);
  101. XCTAssertTrue([tabBar.childViewControllers[0] isMemberOfClass:[RNNNavigationController class]]);
  102. UINavigationController *navController = tabBar.childViewControllers[0];
  103. XCTAssertTrue(navController.childViewControllers.count == 1);
  104. XCTAssertTrue([navController.childViewControllers[0] isMemberOfClass:[RNNRootViewController class]]);
  105. }
  106. - (void)testCreateLayout_TopTabsLayout {
  107. NSDictionary* layout = @{
  108. @"id": @"cntId",
  109. @"type": @"TopTabs",
  110. @"data": @{},
  111. @"children": @[
  112. @{@"id": @"cntId_2",
  113. @"type": @"Stack",
  114. @"data": @{},
  115. @"children": @[
  116. @{@"id": @"cntId_3",
  117. @"type": @"Component",
  118. @"data": @{},
  119. @"children": @[]}]}]};
  120. RNNTopTabsViewController* tabBar = (RNNTopTabsViewController*) [self.factory createLayout:layout saveToStore:self.store];
  121. XCTAssertTrue([tabBar isMemberOfClass:[RNNTopTabsViewController class]]);
  122. }
  123. - (void)testCreateLayout_ComponentSideMenuLayoutCenterLeftRight {
  124. NSDictionary* layout = @{@"id": @"cntId",
  125. @"type": @"SideMenuRoot",
  126. @"data": @{},
  127. @"children": @[
  128. @{@"id": @"cntI_2",
  129. @"type": @"SideMenuCenter",
  130. @"data": @{},
  131. @"children": @[
  132. @{@"id": @"cntId_3",
  133. @"type": @"Component",
  134. @"data": @{},
  135. @"children": @[]}]},
  136. @{@"id": @"cntI_4",
  137. @"type": @"SideMenuLeft",
  138. @"data": @{},
  139. @"children": @[
  140. @{@"id": @"cntId_5",
  141. @"type": @"Component",
  142. @"data": @{},
  143. @"children": @[]}]},
  144. @{@"id": @"cntI_6",
  145. @"type": @"SideMenuRight",
  146. @"data": @{},
  147. @"children": @[
  148. @{@"id": @"cntId_7",
  149. @"type": @"Component",
  150. @"data": @{},
  151. @"children": @[]}]}]};
  152. RNNSideMenuController *ans = (RNNSideMenuController*) [self.factory createLayout:layout saveToStore:self.store];
  153. XCTAssertTrue([ans isMemberOfClass:[RNNSideMenuController class]]);
  154. XCTAssertTrue([ans isKindOfClass:[UIViewController class]]);
  155. XCTAssertTrue([ans.center isMemberOfClass:[RNNSideMenuChildVC class]]);
  156. RNNSideMenuChildVC *center = (RNNSideMenuChildVC*)ans.center;
  157. XCTAssertTrue(center.type == RNNSideMenuChildTypeCenter);
  158. XCTAssertTrue([center.child isMemberOfClass:[RNNRootViewController class]]);
  159. RNNSideMenuChildVC *left = (RNNSideMenuChildVC*)ans.left;
  160. XCTAssertTrue(left.type == RNNSideMenuChildTypeLeft);
  161. XCTAssertTrue([left.child isMemberOfClass:[RNNRootViewController class]]);
  162. RNNSideMenuChildVC *right = (RNNSideMenuChildVC*)ans.right;
  163. XCTAssertTrue(right.type == RNNSideMenuChildTypeRight);
  164. XCTAssertTrue([right.child isMemberOfClass:[RNNRootViewController class]]);
  165. }
  166. - (void)testCreateLayout_addComponentToStore {
  167. NSString *componentId = @"cntId";
  168. NSDictionary* layout = @{@"id": componentId,
  169. @"type": @"Component",
  170. @"data": @{},
  171. @"children": @[]};
  172. UIViewController *ans = [self.factory createLayout:layout saveToStore:self.store];
  173. UIViewController *storeAns = [self.store findComponentForId:componentId];
  174. XCTAssertEqualObjects(ans, storeAns);
  175. }
  176. @end