react-native-navigation的迁移库

RNNStackControllerTest.m 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import <ReactNativeNavigation/RNNStackController.h>
  4. #import <ReactNativeNavigation/RNNComponentViewController.h>
  5. #import "RNNTestRootViewCreator.h"
  6. @interface RNNStackControllerTest : XCTestCase
  7. @property (nonatomic, strong) RNNStackController *uut;
  8. @end
  9. @implementation RNNStackControllerTest {
  10. RNNComponentViewController* _vc1;
  11. id _vc2Mock;
  12. RNNComponentViewController* _vc2;
  13. UIViewController* _vc3;
  14. RNNNavigationOptions* _options;
  15. RNNTestRootViewCreator* _creator;
  16. RNNEventEmitter* _eventEmitter;
  17. id _presenter;
  18. }
  19. - (void)setUp {
  20. [super setUp];
  21. _presenter = [OCMockObject partialMockForObject:[[RNNStackPresenter alloc] init]];
  22. _eventEmitter = [OCMockObject niceMockForClass:[RNNEventEmitter class]];
  23. _creator = [[RNNTestRootViewCreator alloc] init];
  24. _vc1 = [[RNNComponentViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] rootViewCreator:nil eventEmitter:nil presenter:[OCMockObject partialMockForObject:[[RNNComponentPresenter alloc] init]] options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  25. _vc2 = [[RNNComponentViewController alloc] initWithLayoutInfo:[RNNLayoutInfo new] rootViewCreator:nil eventEmitter:nil presenter:[[RNNComponentPresenter alloc] init] options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]];
  26. _vc2Mock = [OCMockObject partialMockForObject:_vc2];
  27. _vc3 = [UIViewController new];
  28. _options = [OCMockObject partialMockForObject:[[RNNNavigationOptions alloc] initEmptyOptions]];
  29. self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:_options defaultOptions:nil presenter:_presenter eventEmitter:_eventEmitter childViewControllers:@[_vc1, _vc2]];
  30. }
  31. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  32. XCTAssertNotNil(self.uut.presenter);
  33. }
  34. - (void)testInitWithLayoutInfo_shouldPreferLargeTitle {
  35. XCTAssertTrue(self.uut.navigationBar.prefersLargeTitles);
  36. }
  37. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  38. self.uut = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1, _vc2]];
  39. XCTAssertTrue(self.uut.viewControllers.count == 2);
  40. }
  41. - (void)testChildViewControllerForStatusBarStyle_shouldReturnTopViewController {
  42. XCTAssertTrue(self.uut.childViewControllerForStatusBarStyle == self.uut.topViewController);
  43. }
  44. - (void)testCurrentChild_shouldReturnTopViewController {
  45. XCTAssertTrue(self.uut.getCurrentChild == self.uut.topViewController);
  46. }
  47. - (void)testGetLeafViewController_shouldReturnTopViewController {
  48. XCTAssertTrue(self.uut.getCurrentChild == self.uut.topViewController);
  49. }
  50. - (void)testCurrentChild_shouldReturnLastChildWithLayoutInfo {
  51. [self.uut addChildViewController:[UIViewController new]];
  52. XCTAssertTrue(self.uut.getCurrentChild != self.uut.topViewController);
  53. XCTAssertTrue(self.uut.getCurrentChild == self.uut.childViewControllers[self.uut.childViewControllers.count-2]);
  54. }
  55. - (void)testPreferredStatusBarStyle_shouldReturnLeafPreferredStatusBarStyle {
  56. self.uut.getCurrentChild.resolveOptions.statusBar.style = [[Text alloc] initWithValue:@"light"];
  57. XCTAssertTrue(self.uut.preferredStatusBarStyle == self.uut.getCurrentChild.preferredStatusBarStyle);
  58. }
  59. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleTrue {
  60. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(1)];
  61. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  62. }
  63. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleFalse {
  64. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(0)];
  65. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  66. }
  67. - (void)testPreferredStatusHidden_shouldHideStatusBar {
  68. self.uut.options.statusBar.visible = [Bool withValue:@(1)];
  69. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  70. }
  71. - (void)testPreferredStatusHidden_shouldShowStatusBar {
  72. self.uut.options.statusBar.visible = [Bool withValue:@(0)];
  73. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  74. }
  75. - (void)testPopGestureEnabled_false {
  76. NSNumber* popGestureEnabled = @(0);
  77. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  78. options.popGesture = [[Bool alloc] initWithValue:popGestureEnabled];
  79. self.uut = [self createNavigationControllerWithOptions:options];
  80. [self.uut viewWillAppear:false];
  81. XCTAssertFalse(self.uut.interactivePopGestureRecognizer.enabled);
  82. }
  83. - (void)testPopGestureEnabled_true {
  84. NSNumber* popGestureEnabled = @(1);
  85. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  86. options.popGesture = [[Bool alloc] initWithValue:popGestureEnabled];
  87. self.uut = [self createNavigationControllerWithOptions:options];
  88. [self.uut onChildWillAppear];
  89. XCTAssertTrue(self.uut.interactivePopGestureRecognizer.enabled);
  90. }
  91. - (void)testRootBackgroundImage {
  92. UIImage* rootBackgroundImage = [[UIImage alloc] init];
  93. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  94. options.rootBackgroundImage = [[Image alloc] initWithValue:rootBackgroundImage];
  95. self.uut = [self createNavigationControllerWithOptions:options];
  96. [self.uut onChildWillAppear];
  97. XCTAssertTrue([[(UIImageView*)self.uut.view.subviews[0] image] isEqual:rootBackgroundImage]);
  98. }
  99. - (void)testTopBarBackgroundClipToBounds_true {
  100. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  101. options.topBar.background.clipToBounds = [[Bool alloc] initWithValue:@(1)];
  102. self.uut = [self createNavigationControllerWithOptions:options];
  103. [self.uut onChildWillAppear];
  104. XCTAssertTrue(self.uut.navigationBar.clipsToBounds);
  105. }
  106. - (void)testTopBarBackgroundClipToBounds_false {
  107. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  108. options.topBar.background.clipToBounds = [[Bool alloc] initWithValue:@(0)];
  109. self.uut = [self createNavigationControllerWithOptions:options];
  110. XCTAssertFalse(self.uut.navigationController.navigationBar.clipsToBounds);
  111. }
  112. - (void)testSupportedOrientationsShouldReturnCurrentChildSupportedOrientations {
  113. XCTAssertEqual(self.uut.supportedInterfaceOrientations, self.uut.getCurrentChild.supportedInterfaceOrientations);
  114. }
  115. - (void)testPopViewControllerReturnLastChildViewController {
  116. RNNStackController* uut = [RNNStackController new];
  117. [uut setViewControllers:@[_vc1, _vc2]];
  118. XCTAssertEqual([uut popViewControllerAnimated:NO], _vc2);
  119. }
  120. - (void)testPopViewControllerSetTopBarBackgroundForPoppingViewController {
  121. _options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
  122. [_vc1 overrideOptions:_options];
  123. [self.uut popViewControllerAnimated:NO];
  124. [_vc1 viewWillAppear:YES];
  125. XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.childViewControllers.lastObject.navigationItem.standardAppearance.backgroundColor);
  126. }
  127. - (void)testPopViewControllerSetDefaultTopBarBackgroundForPoppingViewController {
  128. _options.topBar.background.color = [[Color alloc] initWithValue:[UIColor redColor]];
  129. [_vc1 setDefaultOptions:_options];
  130. [self.uut popViewControllerAnimated:NO];
  131. XCTAssertEqual(_vc1.resolveOptions.topBar.background.color.get, self.uut.navigationBar.barTintColor);
  132. }
  133. - (void)testPopViewControllerShouldInvokeApplyOptionsBeforePoppingForDestinationViewController {
  134. RNNStackController* uut = [RNNStackController new];
  135. [uut setViewControllers:@[_vc1, _vc2]];
  136. [[(id)uut.presenter expect] applyOptionsBeforePopping:[OCMArg any]];
  137. [uut popViewControllerAnimated:NO];
  138. [(id)uut.presenter verify];
  139. }
  140. - (void)testPopViewController_ShouldEmitScreenPoppedEvent {
  141. RNNStackController* uut = [RNNStackController new];
  142. [uut setViewControllers:@[_vc1, _vc2]];
  143. [[(id)uut.eventEmitter expect] sendScreenPoppedEvent:_vc2.layoutInfo.componentId];
  144. [uut popViewControllerAnimated:NO];
  145. [(id)uut.eventEmitter verify];
  146. }
  147. - (void)testOverrideOptionsShouldOverrideOptionsState {
  148. RNNNavigationOptions* overrideOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
  149. [(RNNNavigationOptions*)[(id)self.uut.options expect] overrideOptions:overrideOptions];
  150. [self.uut overrideOptions:overrideOptions];
  151. [(id)self.uut.options verify];
  152. }
  153. - (void)testMergeChildOptionsShouldUpdatePresenterForVisibleChild {
  154. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  155. [[_presenter expect] mergeOptions:options resolvedOptions:[OCMArg any]];
  156. [self.uut mergeChildOptions:options child:self.uut.childViewControllers.lastObject];
  157. [_presenter verify];
  158. }
  159. - (void)testMergeChildOptionsShouldNotUpdatePresenterForInvisibleChild {
  160. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  161. [[_presenter reject] mergeOptions:options resolvedOptions:self.uut.resolveOptions];
  162. [self.uut mergeChildOptions:options child:self.uut.childViewControllers.firstObject];
  163. [_presenter verify];
  164. }
  165. - (RNNStackController *)createNavigationControllerWithOptions:(RNNNavigationOptions *)options {
  166. RNNStackController* nav = [[RNNStackController alloc] initWithLayoutInfo:nil creator:_creator options:options defaultOptions:nil presenter:[[RNNStackPresenter alloc] init] eventEmitter:nil childViewControllers:@[_vc1]];
  167. return nav;
  168. }
  169. @end