react-native-navigation的迁移库

RNNTabBarControllerTest.m 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #import <XCTest/XCTest.h>
  2. #import "RNNTabBarController.h"
  3. #import "RNNNavigationOptions.h"
  4. #import "RNNTabBarPresenter.h"
  5. #import "RNNRootViewController.h"
  6. #import <OCMock/OCMock.h>
  7. @interface RNNTabBarControllerTest : XCTestCase
  8. @property (nonatomic, strong) RNNTabBarController *uut;
  9. @end
  10. @implementation RNNTabBarControllerTest
  11. - (void)setUp {
  12. [super setUp];
  13. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[[UIViewController alloc] init]] options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:[[RNNViewControllerPresenter alloc] init]];
  14. }
  15. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  16. XCTAssertNotNil(self.uut.presenter);
  17. }
  18. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  19. UIViewController* vc1 = [[UIViewController alloc] init];
  20. UIViewController* vc2 = [[UIViewController alloc] init];
  21. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[vc1, vc2] options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:[[RNNViewControllerPresenter alloc] init]];
  22. XCTAssertTrue(self.uut.viewControllers.count == 2);
  23. }
  24. - (void)testInitWithLayoutInfo_shouldInitializeDependencies {
  25. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  26. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  27. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  28. NSArray* childViewControllers = @[[UIViewController new]];
  29. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter];
  30. XCTAssertTrue(self.uut.layoutInfo == layoutInfo);
  31. XCTAssertTrue(self.uut.options == options);
  32. XCTAssertTrue(self.uut.presenter == presenter);
  33. XCTAssertTrue(self.uut.childViewControllers.count == childViewControllers.count);
  34. }
  35. - (void)testInitWithEventEmmiter_shouldInitializeDependencies {
  36. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  37. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  38. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  39. RNNEventEmitter* eventEmmiter = [RNNEventEmitter new];
  40. NSArray* childViewControllers = @[[UIViewController new]];
  41. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo childViewControllers:childViewControllers options:options presenter:presenter eventEmitter:eventEmmiter];
  42. XCTAssertTrue(self.uut.layoutInfo == layoutInfo);
  43. XCTAssertTrue(self.uut.options == options);
  44. XCTAssertTrue(self.uut.presenter == presenter);
  45. XCTAssertTrue(self.uut.childViewControllers.count == childViewControllers.count);
  46. XCTAssertTrue(self.uut.eventEmitter == eventEmmiter);
  47. }
  48. - (void)testInitWithLayoutInfo_shouldSetDelegate {
  49. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:[[RNNViewControllerPresenter alloc] init]];
  50. XCTAssertTrue(self.uut.delegate == self.uut);
  51. }
  52. - (void)testWillMoveToParent_invokePresenterApplyOptionsOnWillMoveToParent {
  53. id presenterMock = [OCMockObject partialMockForObject:[RNNTabBarPresenter new]];
  54. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  55. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:presenterMock];
  56. [[presenterMock expect] applyOptionsOnWillMoveToParentViewController:options];
  57. [self.uut willMoveToParentViewController:[UIViewController new]];
  58. [presenterMock verify];
  59. }
  60. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  61. id presenterMock = [OCMockObject partialMockForObject:[RNNTabBarPresenter new]];
  62. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  63. self.uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:presenterMock];
  64. [[presenterMock reject] applyOptionsOnWillMoveToParentViewController:options];
  65. [self.uut willMoveToParentViewController:nil];
  66. [presenterMock verify];
  67. }
  68. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  69. id presenterMock = [OCMockObject partialMockForObject:[RNNTabBarPresenter new]];
  70. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  71. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:presenterMock];
  72. [[presenterMock expect] applyOptions:[OCMArg any]];
  73. [uut onChildWillAppear];
  74. [presenterMock verify];
  75. }
  76. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  77. id presenterMock = [OCMockObject partialMockForObject:[RNNTabBarPresenter new]];
  78. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  79. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:presenterMock];
  80. [(RNNTabBarPresenter *)[presenterMock expect] mergeOptions:options resolvedOptions:nil];
  81. [uut mergeOptions:options];
  82. [presenterMock verify];
  83. }
  84. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  85. id parentMock = [OCMockObject partialMockForObject:[RNNTabBarController new]];
  86. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  87. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:[RNNTabBarPresenter new]];
  88. [parentMock addChildViewController:uut];
  89. [(RNNTabBarController *)[parentMock expect] mergeOptions:options];
  90. [uut mergeOptions:options];
  91. [parentMock verify];
  92. }
  93. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  94. id parentMock = [OCMockObject partialMockForObject:[RNNTabBarController new]];
  95. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  96. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:nil options:options presenter:[RNNTabBarPresenter new]];
  97. [parentMock addChildViewController:uut];
  98. [[parentMock expect] onChildWillAppear];
  99. [uut onChildWillAppear];
  100. [parentMock verify];
  101. }
  102. - (void)testGetCurrentChild_shouldInvokeSelectedViewControllerGetCurrentChild {
  103. id childMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  104. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  105. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[childMock] options:options presenter:[RNNTabBarPresenter new]];
  106. [[childMock expect] getCurrentChild];
  107. [uut getCurrentChild];
  108. [childMock verify];
  109. }
  110. - (void)testGetCurrentChild_shouldInvokeOnSelectedViewController {
  111. id childMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  112. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  113. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[UIViewController new], childMock] options:options presenter:[RNNTabBarPresenter new]];
  114. [uut setSelectedIndex:1];
  115. [[childMock expect] getCurrentChild];
  116. [uut getCurrentChild];
  117. [childMock verify];
  118. }
  119. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  120. id childMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  121. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  122. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[childMock] options:options presenter:[RNNTabBarPresenter new]];
  123. [[childMock expect] preferredStatusBarStyle];
  124. [uut preferredStatusBarStyle];
  125. [childMock verify];
  126. }
  127. - (void)testPreferredStatusBarStyle_shouldInvokeOnSelectedViewController {
  128. id childMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  129. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  130. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[UIViewController new], childMock] options:options presenter:[RNNTabBarPresenter new]];
  131. [uut setSelectedIndex:1];
  132. [[childMock expect] preferredStatusBarStyle];
  133. [uut preferredStatusBarStyle];
  134. [childMock verify];
  135. }
  136. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  137. id eventEmmiterMock = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  138. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  139. UIViewController* vc = [UIViewController new];
  140. UIViewController* vc2 = [UIViewController new];
  141. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[vc, vc2] options:options presenter:[RNNTabBarPresenter new] eventEmitter:eventEmmiterMock];
  142. [uut setSelectedIndex:1];
  143. [[eventEmmiterMock expect] sendBottomTabSelected:[OCMArg any] unselected:[OCMArg any]];
  144. [uut tabBarController:uut didSelectViewController:vc2];
  145. [eventEmmiterMock verify];
  146. }
  147. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  148. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  149. layoutInfo.componentId = @"componentId";
  150. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil];
  151. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[UIViewController new], vc] options:nil presenter:[RNNTabBarPresenter new]];
  152. [uut setSelectedIndexByComponentID:@"componentId"];
  153. XCTAssertTrue(uut.selectedIndex == 1);
  154. }
  155. @end