react-native-navigation的迁移库

RNNComponentPresenterTest.m 11KB

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