react-native-navigation的迁移库

BottomTabsControllerTest.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #import <XCTest/XCTest.h>
  2. #import <ReactNativeNavigation/RNNBottomTabsController.h>
  3. #import <ReactNativeNavigation/RNNComponentViewController.h>
  4. #import "RNNStackController.h"
  5. #import <OCMock/OCMock.h>
  6. #import <ReactNativeNavigation/BottomTabPresenterCreator.h>
  7. #import "RNNBottomTabsController+Helpers.h"
  8. #import "RNNComponentViewController+Utils.h"
  9. @interface BottomTabsControllerTest : XCTestCase
  10. @property(nonatomic, strong) RNNBottomTabsController * originalUut;
  11. @property(nonatomic, strong) RNNBottomTabsController * uut;
  12. @property(nonatomic, strong) id mockChildViewController;
  13. @property(nonatomic, strong) id mockEventEmitter;
  14. @property(nonatomic, strong) id mockTabBarPresenter;
  15. @end
  16. @implementation BottomTabsControllerTest
  17. - (void)setUp {
  18. [super setUp];
  19. id tabBarClassMock = OCMClassMock([RNNBottomTabsController class]);
  20. OCMStub([tabBarClassMock parentViewController]).andReturn([OCMockObject partialMockForObject:[RNNBottomTabsController new]]);
  21. UIViewController* childViewController = [RNNComponentViewController createWithComponentId:@"componentId" initialOptions:[RNNNavigationOptions emptyOptions]];
  22. NSArray* children = @[childViewController];
  23. self.mockTabBarPresenter = [OCMockObject partialMockForObject:[[RNNBottomTabsPresenter alloc] init]];
  24. self.mockChildViewController = [OCMockObject partialMockForObject:childViewController];
  25. self.mockEventEmitter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  26. self.originalUut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:self.mockTabBarPresenter bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:nil] dotIndicatorPresenter:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil] eventEmitter:self.mockEventEmitter childViewControllers:children bottomTabsAttacher:nil];
  27. self.uut = [OCMockObject partialMockForObject:self.originalUut];
  28. OCMStub([self.uut selectedViewController]).andReturn(self.mockChildViewController);
  29. }
  30. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  31. XCTAssertNotNil([self.uut presenter]);
  32. }
  33. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  34. UIViewController *vc1 = [[UIViewController alloc] init];
  35. UIViewController *vc2 = [[UIViewController alloc] init];
  36. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter alloc] init] eventEmitter:nil childViewControllers:@[vc1, vc2]];
  37. XCTAssertTrue(uut.viewControllers.count == 2);
  38. }
  39. - (void)testInitWithLayoutInfo_shouldInitializeDependencies {
  40. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  41. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  42. RNNBottomTabsPresenter *presenter = [[RNNBottomTabsPresenter alloc] init];
  43. NSArray *childViewControllers = @[[UIViewController new]];
  44. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:nil childViewControllers:childViewControllers];
  45. XCTAssertTrue(uut.layoutInfo == layoutInfo);
  46. XCTAssertTrue(uut.options == options);
  47. XCTAssertTrue(uut.presenter == presenter);
  48. XCTAssertTrue(uut.childViewControllers.count == childViewControllers.count);
  49. }
  50. - (void)testInitWithEventEmmiter_shouldInitializeDependencies {
  51. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  52. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  53. RNNBottomTabsPresenter *presenter = [[RNNBottomTabsPresenter alloc] init];
  54. RNNEventEmitter *eventEmmiter = [RNNEventEmitter new];
  55. NSArray *childViewControllers = @[[UIViewController new]];
  56. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:eventEmmiter childViewControllers:childViewControllers];
  57. XCTAssertTrue(uut.layoutInfo == layoutInfo);
  58. XCTAssertTrue(uut.options == options);
  59. XCTAssertTrue(uut.presenter == presenter);
  60. XCTAssertTrue(uut.childViewControllers.count == childViewControllers.count);
  61. XCTAssertTrue(uut.eventEmitter == eventEmmiter);
  62. }
  63. - (void)testInitWithLayoutInfo_shouldSetDelegate {
  64. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNBasePresenter alloc] init] eventEmitter:nil childViewControllers:nil];
  65. XCTAssertTrue(uut.delegate == uut);
  66. }
  67. - (void)testInitWithLayoutInfo_shouldCreateWithDefaultStyles {
  68. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNBottomTabsPresenter alloc] init] eventEmitter:nil childViewControllers:nil];
  69. XCTAssertEqual(uut.modalPresentationStyle, UIModalPresentationPageSheet);
  70. XCTAssertEqual(uut.modalTransitionStyle, UIModalTransitionStyleCoverVertical);
  71. }
  72. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  73. [[self.mockTabBarPresenter reject] applyOptionsOnWillMoveToParentViewController:[self.uut options]];
  74. [self.uut willMoveToParentViewController:nil];
  75. [self.mockTabBarPresenter verify];
  76. }
  77. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  78. [[self.mockTabBarPresenter expect] applyOptions:[OCMArg any]];
  79. [self.uut onChildWillAppear];
  80. [self.mockTabBarPresenter verify];
  81. }
  82. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  83. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  84. [(RNNBottomTabsPresenter *) [self.mockTabBarPresenter expect] mergeOptions:options resolvedOptions:[OCMArg any]];
  85. [self.uut mergeOptions:options];
  86. [self.mockTabBarPresenter verify];
  87. }
  88. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  89. id parentMock = [OCMockObject niceMockForClass:[RNNComponentViewController class]];
  90. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  91. OCMStub([self.uut parentViewController]).andReturn(parentMock);
  92. [((RNNComponentViewController *) [parentMock expect]) mergeChildOptions:options child:self.originalUut];
  93. [self.uut mergeOptions:options];
  94. [parentMock verify];
  95. }
  96. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  97. id parentMock = [OCMockObject partialMockForObject:[RNNStackController new]];
  98. OCMStub([self.uut parentViewController]).andReturn(parentMock);
  99. [[parentMock expect] onChildWillAppear];
  100. [self.uut onChildWillAppear];
  101. [parentMock verify];
  102. }
  103. - (void)testViewDidLayoutSubviews_delegateToPresenter {
  104. [[[self mockTabBarPresenter] expect] viewDidLayoutSubviews];
  105. [[self uut] viewDidLayoutSubviews];
  106. [[self mockTabBarPresenter] verify];
  107. }
  108. - (void)testGetCurrentChild_shouldReturnSelectedViewController {
  109. XCTAssertEqual([self.uut getCurrentChild], [(RNNBottomTabsController *) self.uut selectedViewController]);
  110. }
  111. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  112. [[self.mockTabBarPresenter expect] getStatusBarStyle];
  113. [self.uut preferredStatusBarStyle];
  114. [self.mockTabBarPresenter verify];
  115. }
  116. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleTrue {
  117. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(1)];
  118. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  119. }
  120. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleFalse {
  121. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(0)];
  122. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  123. }
  124. - (void)testPreferredStatusHidden_shouldHideStatusBar {
  125. self.uut.options.statusBar.visible = [Bool withValue:@(1)];
  126. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  127. }
  128. - (void)testPreferredStatusHidden_shouldShowStatusBar {
  129. self.uut.options.statusBar.visible = [Bool withValue:@(0)];
  130. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  131. }
  132. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  133. NSUInteger selectedIndex = 2;
  134. OCMStub([self.uut selectedIndex]).andReturn(selectedIndex);
  135. [[self.mockEventEmitter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  136. [self.uut tabBarController:self.uut didSelectViewController:[UIViewController new]];
  137. [self.mockEventEmitter verify];
  138. }
  139. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  140. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  141. layoutInfo.componentId = @"componentId";
  142. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  143. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:[RNNBottomTabsPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  144. [uut setSelectedIndexByComponentID:@"componentId"];
  145. XCTAssertTrue(uut.selectedIndex == 1);
  146. }
  147. - (void)testSetSelectedIndex_ShouldSetSelectedIndexWithCurrentTabIndex {
  148. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initEmptyOptions];
  149. options.bottomTabs.currentTabIndex = [[IntNumber alloc] initWithValue:@(1)];
  150. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  151. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:nil presenter:[RNNBottomTabsPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  152. XCTAssertTrue(uut.selectedIndex == 1);
  153. }
  154. - (void)testOnViewDidLayoutSubviews_ShouldUpdateDotIndicatorForChildren {
  155. id dotIndicator = [OCMockObject partialMockForObject:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil]];
  156. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  157. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:nil bottomTabPresenter:nil dotIndicatorPresenter:dotIndicator eventEmitter:nil childViewControllers:@[[UIViewController new], vc] bottomTabsAttacher:nil];
  158. [[dotIndicator expect] bottomTabsDidLayoutSubviews:uut];
  159. [uut viewDidLayoutSubviews];
  160. [dotIndicator verify];
  161. }
  162. @end