react-native-navigation的迁移库

RNNComponentPresenterTest.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNComponentPresenter.h"
  4. #import "UIViewController+RNNOptions.h"
  5. #import "RNNComponentViewController.h"
  6. #import "UIViewController+LayoutProtocol.h"
  7. #import "RNNTitleViewHelper.h"
  8. @interface RNNComponentPresenterTest : XCTestCase
  9. @property (nonatomic, strong) RNNComponentPresenter *uut;
  10. @property (nonatomic, strong) RNNNavigationOptions *options;
  11. @property (nonatomic, strong) UIViewController *boundViewController;
  12. @property (nonatomic, strong) RNNReactComponentRegistry *componentRegistry;
  13. @end
  14. @implementation RNNComponentPresenterTest
  15. - (void)setUp {
  16. [super setUp];
  17. self.componentRegistry = [OCMockObject partialMockForObject:[RNNReactComponentRegistry new]];
  18. self.uut = [[RNNComponentPresenter alloc] initWithComponentRegistry:self.componentRegistry:[[RNNNavigationOptions alloc] initEmptyOptions]];
  19. self.boundViewController = [OCMockObject partialMockForObject:[RNNComponentViewController new]];
  20. [self.uut boundViewController:self.boundViewController];
  21. self.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  22. }
  23. - (void)testApplyOptions_backgroundImageDefaultNilShouldNotAddSubview {
  24. [self.uut applyOptions:self.options];
  25. XCTAssertTrue((self.boundViewController.view.subviews.count) == 0);
  26. }
  27. - (void)testApplyOptions_topBarPrefersLargeTitleDefaultFalse {
  28. [self.uut applyOptions:self.options];
  29. XCTAssertTrue(self.boundViewController.navigationItem.largeTitleDisplayMode == UINavigationItemLargeTitleDisplayModeNever);
  30. }
  31. - (void)testApplyOptions_layoutBackgroundColorDefaultWhiteColor {
  32. [self.uut applyOptions:self.options];
  33. XCTAssertNil(self.boundViewController.view.backgroundColor);
  34. }
  35. - (void)testApplyOptions_statusBarBlurDefaultFalse {
  36. [self.uut applyOptions:self.options];
  37. XCTAssertNil([self.boundViewController.view viewWithTag:BLUR_STATUS_TAG]);
  38. }
  39. - (void)testApplyOptions_statusBarStyleDefaultStyle {
  40. [self.uut applyOptions:self.options];
  41. XCTAssertTrue([self.boundViewController preferredStatusBarStyle] == UIStatusBarStyleDefault);
  42. }
  43. - (void)testApplyOptions_backButtonVisibleDefaultTrue {
  44. [self.uut applyOptions:self.options];
  45. XCTAssertFalse(self.boundViewController.navigationItem.hidesBackButton);
  46. }
  47. - (void)testApplyOptions_drawBehindTabBarTrueWhenVisibleFalse {
  48. self.options.bottomTabs.visible = [[Bool alloc] initWithValue:@(0)];
  49. [[(id) self.boundViewController expect] setDrawBehindTabBar:YES];
  50. [self.uut applyOptionsOnInit:self.options];
  51. [(id)self.boundViewController verify];
  52. }
  53. - (void)testApplyOptions_setOverlayTouchOutsideIfHasValue {
  54. self.options.overlay.interceptTouchOutside = [[Bool alloc] initWithBOOL:YES];
  55. [(UIViewController *) [(id) self.boundViewController expect] setInterceptTouchOutside:YES];
  56. [self.uut applyOptions:self.options];
  57. [(id)self.boundViewController verify];
  58. }
  59. - (void)testBindViewControllerShouldCreateNavigationButtonsCreator {
  60. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] init];
  61. [presenter boundViewController:self.boundViewController];
  62. XCTAssertNotNil(presenter.navigationButtons);
  63. }
  64. - (void)testApplyOptionsOnInit_shouldSetModalPresentationStyleWithDefault {
  65. [(UIViewController *) [(id) self.boundViewController expect] setModalPresentationStyle:UIModalPresentationFullScreen];
  66. [self.uut applyOptionsOnInit:self.options];
  67. [(id)self.boundViewController verify];
  68. }
  69. - (void)testApplyOptionsOnInit_shouldSetModalTransitionStyleWithDefault {
  70. [(UIViewController *) [(id) self.boundViewController expect] setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
  71. [self.uut applyOptionsOnInit:self.options];
  72. [(id)self.boundViewController verify];
  73. }
  74. - (void)testApplyOptionsOnInit_shouldSetModalPresentationStyleWithValue {
  75. self.options.modalPresentationStyle = [[Text alloc] initWithValue:@"overCurrentContext"];
  76. [(UIViewController *) [(id) self.boundViewController expect] setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  77. [self.uut applyOptionsOnInit:self.options];
  78. [(id)self.boundViewController verify];
  79. }
  80. - (void)testApplyOptionsOnInit_shouldSetModalTransitionStyleWithValue {
  81. self.options.modalTransitionStyle = [[Text alloc] initWithValue:@"crossDissolve"];
  82. [(UIViewController *) [(id) self.boundViewController expect] setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
  83. [self.uut applyOptionsOnInit:self.options];
  84. [(id)self.boundViewController verify];
  85. }
  86. -(void)testApplyOptionsOnInit_TopBarDrawUnder_true {
  87. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(1)];
  88. [[(id) self.boundViewController expect] setDrawBehindTopBar:YES];
  89. [self.uut applyOptionsOnInit:self.options];
  90. [(id)self.boundViewController verify];
  91. }
  92. -(void)testApplyOptionsOnInit_TopBarDrawUnder_false {
  93. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(0)];
  94. [[(id) self.boundViewController expect] setDrawBehindTopBar:NO];
  95. [self.uut applyOptionsOnInit:self.options];
  96. [(id)self.boundViewController verify];
  97. }
  98. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_true {
  99. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(1)];
  100. [[(id) self.boundViewController expect] setDrawBehindTabBar:YES];
  101. [self.uut applyOptionsOnInit:self.options];
  102. [(id)self.boundViewController verify];
  103. }
  104. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_false {
  105. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(0)];
  106. [[(id) self.boundViewController expect] setDrawBehindTabBar:NO];
  107. [self.uut applyOptionsOnInit:self.options];
  108. [(id)self.boundViewController verify];
  109. }
  110. - (void)testReactViewShouldBeReleasedOnDealloc {
  111. RNNComponentViewController* bindViewController = [RNNComponentViewController new];
  112. bindViewController.layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  113. [self.uut boundViewController:bindViewController];
  114. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"componentName"}];
  115. [[(id)self.componentRegistry expect] clearComponentsForParentId:self.uut.boundComponentId];
  116. self.uut = nil;
  117. [(id)self.componentRegistry verify];
  118. }
  119. - (void)testBindViewControllerShouldSetBoundComponentId {
  120. RNNComponentViewController* bindViewController = [RNNComponentViewController new];
  121. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  122. layoutInfo.componentId = @"componentId";
  123. bindViewController.layoutInfo = layoutInfo;
  124. [self.uut boundViewController:bindViewController];
  125. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  126. }
  127. - (void)testRenderComponentsCreateReactViewWithBoundComponentId {
  128. RNNComponentViewController* boundViewController = [RNNComponentViewController new];
  129. RNNLayoutInfo* layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  130. boundViewController.layoutInfo = layoutInfo;
  131. [self.uut boundViewController:boundViewController];
  132. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"titleComponent"}];
  133. [[(id)self.componentRegistry expect] createComponentIfNotExists:self.options.topBar.title.component parentComponentId:self.uut.boundComponentId reactViewReadyBlock:[OCMArg any]];
  134. [self.uut renderComponents:self.options perform:nil];
  135. [(id)self.componentRegistry verify];
  136. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  137. }
  138. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBoundViewController_withTitle {
  139. Text* title = [[Text alloc] initWithValue:@"Title"];
  140. self.options.topBar.backButton.title = title;
  141. [[(id) self.boundViewController expect] setBackButtonIcon:nil withColor:nil title:title.get];
  142. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  143. [(id)self.boundViewController verify];
  144. }
  145. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBoundViewController_withHideTitle {
  146. Text* title = [[Text alloc] initWithValue:@"Title"];
  147. self.options.topBar.backButton.title = title;
  148. self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
  149. [[(id) self.boundViewController expect] setBackButtonIcon:nil withColor:nil title:@""];
  150. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  151. [(id)self.boundViewController verify];
  152. }
  153. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBoundViewController_withIcon {
  154. Image* image = [[Image alloc] initWithValue:[UIImage new]];
  155. self.options.topBar.backButton.icon = image;
  156. [[(id) self.boundViewController expect] setBackButtonIcon:image.get withColor:nil title:nil];
  157. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  158. [(id)self.boundViewController verify];
  159. }
  160. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBoundViewController_withDefaultValues {
  161. [[(id) self.boundViewController expect] setBackButtonIcon:nil withColor:nil title:nil];
  162. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  163. [(id)self.boundViewController verify];
  164. }
  165. - (void)testRemoveTitleComponentIfNeeded_componentIsRemovedIfTitleTextIsDefined {
  166. id mockTitle = [OCMockObject niceMockForClass:[RNNReactView class]];
  167. OCMStub([self.componentRegistry createComponentIfNotExists:[OCMArg any] parentComponentId:[OCMArg any] reactViewReadyBlock:nil]).andReturn(mockTitle);
  168. RNNComponentOptions* component = [RNNComponentOptions new];
  169. component.name = [[Text alloc] initWithValue:@"componentName"];
  170. component.componentId = [[Text alloc] initWithValue:@"someId"];
  171. _options.topBar.title.component = component;
  172. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  173. XCTAssertNotNil(self.boundViewController.navigationItem.titleView);
  174. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  175. [[mockTitle expect] removeFromSuperview];
  176. _options = [[RNNNavigationOptions alloc] initEmptyOptions];
  177. _options.topBar.title.text = [[Text alloc] initWithValue:@""];
  178. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  179. XCTAssertNotEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  180. [mockTitle verify];
  181. }
  182. - (void)testRemoveTitleComponentIfNeeded_componentIsNotRemovedIfMergeOptionsIsCalledWithoutTitleText {
  183. id mockTitle = [OCMockObject niceMockForClass:[RNNReactView class]];
  184. OCMStub([self.componentRegistry createComponentIfNotExists:[OCMArg any] parentComponentId:[OCMArg any] reactViewReadyBlock:nil]).andReturn(mockTitle);
  185. RNNComponentOptions* component = [RNNComponentOptions new];
  186. component.name = [[Text alloc] initWithValue:@"componentName"];
  187. component.componentId = [[Text alloc] initWithValue:@"someId"];
  188. _options.topBar.title.component = component;
  189. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  190. XCTAssertNotNil(self.boundViewController.navigationItem.titleView);
  191. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  192. _options = [[RNNNavigationOptions alloc] initEmptyOptions];
  193. _options.bottomTabs.visible = [[Bool alloc] initWithBOOL:NO];
  194. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  195. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  196. }
  197. - (RNNLayoutInfo *)createLayoutInfoWithComponentId:(NSString *)componentId {
  198. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  199. layoutInfo.componentId = componentId;
  200. return layoutInfo;
  201. }
  202. @end