react-native-navigation的迁移库

RNNViewControllerPresenterTest.m 8.9KB

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