react-native-navigation的迁移库

RNNViewControllerPresenterTest.m 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNViewControllerPresenter.h"
  4. #import "UIViewController+RNNOptions.h"
  5. #import "RNNReactView.h"
  6. #import "RNNRootViewController.h"
  7. @interface RNNViewControllerPresenterTest : XCTestCase
  8. @property (nonatomic, strong) RNNViewControllerPresenter *uut;
  9. @property (nonatomic, strong) RNNNavigationOptions *options;
  10. @property (nonatomic, strong) UIViewController *bindedViewController;
  11. @property (nonatomic, strong) RNNReactComponentRegistry *componentRegistry;
  12. @end
  13. @implementation RNNViewControllerPresenterTest
  14. - (void)setUp {
  15. [super setUp];
  16. self.componentRegistry = [OCMockObject partialMockForObject:[RNNReactComponentRegistry new]];
  17. self.uut = [[RNNViewControllerPresenter alloc] initWithComponentRegistry:self.componentRegistry];
  18. self.bindedViewController = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  19. [self.uut bindViewController:self.bindedViewController];
  20. self.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  21. }
  22. - (void)testApplyOptions_backgroundImageDefaultNilShouldNotAddSubview {
  23. [self.uut applyOptions:self.options];
  24. XCTAssertTrue((self.bindedViewController.view.subviews.count) == 0);
  25. }
  26. - (void)testApplyOptions_topBarPrefersLargeTitleDefaultFalse {
  27. [self.uut applyOptions:self.options];
  28. XCTAssertTrue(self.bindedViewController.navigationItem.largeTitleDisplayMode == UINavigationItemLargeTitleDisplayModeNever);
  29. }
  30. - (void)testApplyOptions_layoutBackgroundColorDefaultWhiteColor {
  31. [self.uut applyOptions:self.options];
  32. XCTAssertNil(self.bindedViewController.view.backgroundColor);
  33. }
  34. - (void)testApplyOptions_statusBarBlurDefaultFalse {
  35. [self.uut applyOptions:self.options];
  36. XCTAssertNil([self.bindedViewController.view viewWithTag:BLUR_STATUS_TAG]);
  37. }
  38. - (void)testApplyOptions_statusBarStyleDefaultStyle {
  39. [self.uut applyOptions:self.options];
  40. XCTAssertTrue([self.bindedViewController preferredStatusBarStyle] == UIStatusBarStyleDefault);
  41. }
  42. - (void)testApplyOptions_backButtonVisibleDefaultTrue {
  43. [self.uut applyOptions:self.options];
  44. XCTAssertFalse(self.bindedViewController.navigationItem.hidesBackButton);
  45. }
  46. - (void)testApplyOptions_drawBehindTabBarTrueWhenVisibleFalse {
  47. self.options.bottomTabs.visible = [[Bool alloc] initWithValue:@(0)];
  48. [[(id)self.bindedViewController expect] rnn_setDrawBehindTabBar:YES];
  49. [self.uut applyOptionsOnInit:self.options];
  50. [(id)self.bindedViewController verify];
  51. }
  52. - (void)testApplyOptions_setOverlayTouchOutsideIfHasValue {
  53. self.options.overlay.interceptTouchOutside = [[Bool alloc] initWithBOOL:YES];
  54. [[(id)self.bindedViewController expect] rnn_setInterceptTouchOutside:YES];
  55. [self.uut applyOptions:self.options];
  56. [(id)self.bindedViewController verify];
  57. }
  58. - (void)testBindViewControllerShouldCreateNavigationButtonsCreator {
  59. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  60. [presenter bindViewController:self.bindedViewController];
  61. XCTAssertNotNil(presenter.navigationButtons);
  62. }
  63. - (void)testApplyOptionsOnInit_shouldSetModalPresentetionStyleWithDefault {
  64. [[(id)self.bindedViewController expect] rnn_setModalPresentationStyle:UIModalPresentationFullScreen];
  65. [self.uut applyOptionsOnInit:self.options];
  66. [(id)self.bindedViewController verify];
  67. }
  68. - (void)testApplyOptionsOnInit_shouldSetModalTransitionStyleWithDefault {
  69. [[(id)self.bindedViewController expect] rnn_setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
  70. [self.uut applyOptionsOnInit:self.options];
  71. [(id)self.bindedViewController verify];
  72. }
  73. - (void)testApplyOptionsOnInit_shouldSetModalPresentetionStyleWithValue {
  74. self.options.modalPresentationStyle = [[Text alloc] initWithValue:@"overCurrentContext"];
  75. [[(id)self.bindedViewController expect] rnn_setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  76. [self.uut applyOptionsOnInit:self.options];
  77. [(id)self.bindedViewController verify];
  78. }
  79. - (void)testApplyOptionsOnInit_shouldSetModalTransitionStyleWithValue {
  80. self.options.modalTransitionStyle = [[Text alloc] initWithValue:@"crossDissolve"];
  81. [[(id)self.bindedViewController expect] rnn_setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
  82. [self.uut applyOptionsOnInit:self.options];
  83. [(id)self.bindedViewController verify];
  84. }
  85. -(void)testApplyOptionsOnInit_TopBarDrawUnder_true {
  86. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(1)];
  87. [[(id)self.bindedViewController expect] rnn_setDrawBehindTopBar:YES];
  88. [self.uut applyOptionsOnInit:self.options];
  89. [(id)self.bindedViewController verify];
  90. }
  91. -(void)testApplyOptionsOnInit_TopBarDrawUnder_false {
  92. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(0)];
  93. [[(id)self.bindedViewController expect] rnn_setDrawBehindTopBar:NO];
  94. [self.uut applyOptionsOnInit:self.options];
  95. [(id)self.bindedViewController verify];
  96. }
  97. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_true {
  98. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(1)];
  99. [[(id)self.bindedViewController expect] rnn_setDrawBehindTabBar:YES];
  100. [self.uut applyOptionsOnInit:self.options];
  101. [(id)self.bindedViewController verify];
  102. }
  103. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_false {
  104. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(0)];
  105. [[(id)self.bindedViewController expect] rnn_setDrawBehindTabBar:NO];
  106. [self.uut applyOptionsOnInit:self.options];
  107. [(id)self.bindedViewController verify];
  108. }
  109. - (void)testReactViewShouldBeReleasedOnDealloc {
  110. RNNRootViewController* bindViewController = [RNNRootViewController new];
  111. bindViewController.layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  112. [self.uut bindViewController:bindViewController];
  113. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"componentName"}];
  114. [[(id)self.componentRegistry expect] clearComponentsForParentId:self.uut.boundComponentId];
  115. self.uut = nil;
  116. [(id)self.componentRegistry verify];
  117. }
  118. - (void)testBindViewControllerShouldSetBindedComponentId {
  119. RNNRootViewController* bindViewController = [RNNRootViewController new];
  120. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  121. layoutInfo.componentId = @"componentId";
  122. bindViewController.layoutInfo = layoutInfo;
  123. [self.uut bindViewController:bindViewController];
  124. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  125. }
  126. - (void)testRenderComponentsCreateReactViewWithBindedComponentId {
  127. RNNRootViewController* bindedViewController = [RNNRootViewController new];
  128. RNNLayoutInfo* layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  129. bindedViewController.layoutInfo = layoutInfo;
  130. [self.uut bindViewController:bindedViewController];
  131. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"titleComponent"}];
  132. [[(id)self.componentRegistry expect] createComponentIfNotExists:self.options.topBar.title.component parentComponentId:self.uut.boundComponentId reactViewReadyBlock:[OCMArg any]];
  133. [self.uut renderComponents:self.options perform:nil];
  134. [(id)self.componentRegistry verify];
  135. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  136. }
  137. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withTitle {
  138. Text* title = [[Text alloc] initWithValue:@"Title"];
  139. self.options.topBar.backButton.title = title;
  140. [[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:title.get];
  141. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  142. [(id)self.bindedViewController verify];
  143. }
  144. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withHideTitle {
  145. Text* title = [[Text alloc] initWithValue:@"Title"];
  146. self.options.topBar.backButton.title = title;
  147. self.options.topBar.backButton.showTitle = [[Bool alloc] initWithValue:@(0)];
  148. [[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:@""];
  149. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  150. [(id)self.bindedViewController verify];
  151. }
  152. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withIcon {
  153. Image* image = [[Image alloc] initWithValue:[UIImage new]];
  154. self.options.topBar.backButton.icon = image;
  155. [[(id)self.bindedViewController expect] rnn_setBackButtonIcon:image.get withColor:nil title:nil];
  156. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  157. [(id)self.bindedViewController verify];
  158. }
  159. - (void)testApplyOptionsOnWillMoveToParent_shouldSetBackButtonOnBindedViewController_withDefaultValues {
  160. [[(id)self.bindedViewController expect] rnn_setBackButtonIcon:nil withColor:nil title:nil];
  161. [self.uut applyOptionsOnWillMoveToParentViewController:self.options];
  162. [(id)self.bindedViewController verify];
  163. }
  164. - (RNNLayoutInfo *)createLayoutInfoWithComponentId:(NSString *)componentId {
  165. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  166. layoutInfo.componentId = @"componentId";
  167. return layoutInfo;
  168. }
  169. @end