react-native-navigation的迁移库

RNNComponentPresenterTest.m 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  19. self.boundViewController = [OCMockObject partialMockForObject:[RNNComponentViewController new]];
  20. [self.uut bindViewController: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 bindViewController:self.boundViewController];
  62. XCTAssertNotNil(presenter.navigationButtons);
  63. }
  64. -(void)testApplyOptionsOnInit_TopBarDrawUnder_true {
  65. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(1)];
  66. [[(id) self.boundViewController expect] setDrawBehindTopBar:YES];
  67. [self.uut applyOptionsOnInit:self.options];
  68. [(id)self.boundViewController verify];
  69. }
  70. -(void)testApplyOptionsOnInit_TopBarDrawUnder_false {
  71. self.options.topBar.drawBehind = [[Bool alloc] initWithValue:@(0)];
  72. [[(id) self.boundViewController expect] setDrawBehindTopBar:NO];
  73. [self.uut applyOptionsOnInit:self.options];
  74. [(id)self.boundViewController verify];
  75. }
  76. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_true {
  77. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(1)];
  78. [[(id) self.boundViewController expect] setDrawBehindTabBar:YES];
  79. [self.uut applyOptionsOnInit:self.options];
  80. [(id)self.boundViewController verify];
  81. }
  82. -(void)testApplyOptionsOnInit_BottomTabsDrawUnder_false {
  83. self.options.bottomTabs.drawBehind = [[Bool alloc] initWithValue:@(0)];
  84. [[(id) self.boundViewController expect] setDrawBehindTabBar:NO];
  85. [self.uut applyOptionsOnInit:self.options];
  86. [(id)self.boundViewController verify];
  87. }
  88. - (void)testReactViewShouldBeReleasedOnDealloc {
  89. RNNComponentViewController* bindViewController = [RNNComponentViewController new];
  90. bindViewController.layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  91. [self.uut bindViewController:bindViewController];
  92. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"componentName"}];
  93. [[(id)self.componentRegistry expect] clearComponentsForParentId:self.uut.boundComponentId];
  94. self.uut = nil;
  95. [(id)self.componentRegistry verify];
  96. }
  97. - (void)testBindViewControllerShouldSetBoundComponentId {
  98. RNNComponentViewController* bindViewController = [RNNComponentViewController new];
  99. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  100. layoutInfo.componentId = @"componentId";
  101. bindViewController.layoutInfo = layoutInfo;
  102. [self.uut bindViewController:bindViewController];
  103. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  104. }
  105. - (void)testRenderComponentsCreateReactViewWithBoundComponentId {
  106. RNNComponentViewController* boundViewController = [RNNComponentViewController new];
  107. RNNLayoutInfo* layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  108. boundViewController.layoutInfo = layoutInfo;
  109. boundViewController.defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  110. [self.uut bindViewController:boundViewController];
  111. self.options.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"titleComponent", @"componentId": @"id"}];
  112. [[(id)self.componentRegistry expect] createComponentIfNotExists:[OCMArg checkWithBlock:^BOOL(RNNComponentOptions* options) {
  113. return [options.name.get isEqual:@"titleComponent"] &&
  114. [options.componentId.get isEqual:@"id"];
  115. }] parentComponentId:self.uut.boundComponentId componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:[OCMArg any]];
  116. [self.uut renderComponents:self.options perform:nil];
  117. [(id)self.componentRegistry verify];
  118. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  119. }
  120. - (void)testRenderComponentsCreateReactViewFromDefaultOptions {
  121. RNNComponentViewController* boundViewController = [RNNComponentViewController new];
  122. boundViewController.layoutInfo = [self createLayoutInfoWithComponentId:@"componentId"];
  123. self.uut.defaultOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  124. [self.uut bindViewController:boundViewController];
  125. self.uut.defaultOptions.topBar.title.component = [[RNNComponentOptions alloc] initWithDict:@{@"name": @"titleComponent", @"componentId": @"id"}];
  126. [[(id)self.componentRegistry expect] createComponentIfNotExists:[OCMArg checkWithBlock:^BOOL(RNNComponentOptions* options) {
  127. return [options.name.get isEqual:@"titleComponent"] &&
  128. [options.componentId.get isEqual:@"id"];
  129. }] parentComponentId:self.uut.boundComponentId componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:[OCMArg any]];
  130. [self.uut renderComponents:self.options perform:nil];
  131. [(id)self.componentRegistry verify];
  132. XCTAssertEqual(self.uut.boundComponentId, @"componentId");
  133. }
  134. - (void)testRemoveTitleComponentIfNeeded_componentIsRemovedIfTitleTextIsDefined {
  135. id mockTitle = [OCMockObject niceMockForClass:[RNNReactTitleView class]];
  136. OCMStub([self.componentRegistry createComponentIfNotExists:[OCMArg any] parentComponentId:[OCMArg any] componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:nil]).andReturn(mockTitle);
  137. RNNComponentOptions* component = [RNNComponentOptions new];
  138. component.name = [[Text alloc] initWithValue:@"componentName"];
  139. component.componentId = [[Text alloc] initWithValue:@"someId"];
  140. _options.topBar.title.component = component;
  141. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  142. XCTAssertNotNil(self.boundViewController.navigationItem.titleView);
  143. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  144. [[mockTitle expect] removeFromSuperview];
  145. _options = [[RNNNavigationOptions alloc] initEmptyOptions];
  146. _options.topBar.title.text = [[Text alloc] initWithValue:@""];
  147. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  148. XCTAssertNotEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  149. [mockTitle verify];
  150. }
  151. - (void)testRemoveTitleComponentIfNeeded_componentIsNotRemovedIfMergeOptionsIsCalledWithoutTitleText {
  152. id mockTitle = [OCMockObject niceMockForClass:[RNNReactTitleView class]];
  153. OCMStub([self.componentRegistry createComponentIfNotExists:[OCMArg any] parentComponentId:[OCMArg any] componentType:RNNComponentTypeTopBarTitle reactViewReadyBlock:nil]).andReturn(mockTitle);
  154. RNNComponentOptions* component = [RNNComponentOptions new];
  155. component.name = [[Text alloc] initWithValue:@"componentName"];
  156. component.componentId = [[Text alloc] initWithValue:@"someId"];
  157. _options.topBar.title.component = component;
  158. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  159. XCTAssertNotNil(self.boundViewController.navigationItem.titleView);
  160. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  161. _options = [[RNNNavigationOptions alloc] initEmptyOptions];
  162. _options.bottomTabs.visible = [[Bool alloc] initWithBOOL:NO];
  163. [self.uut mergeOptions:_options resolvedOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  164. XCTAssertEqual(self.boundViewController.navigationItem.titleView, mockTitle);
  165. }
  166. - (RNNLayoutInfo *)createLayoutInfoWithComponentId:(NSString *)componentId {
  167. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  168. layoutInfo.componentId = componentId;
  169. return layoutInfo;
  170. }
  171. @end