react-native-navigation的迁移库

BottomTabsControllerTest.m 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 createWithChildren:@[vc1, vc2]];
  37. [uut viewWillAppear:YES];
  38. XCTAssertTrue(uut.viewControllers.count == 2);
  39. }
  40. - (void)testInitWithLayoutInfo_shouldInitializeDependencies {
  41. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  42. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  43. RNNBottomTabsPresenter *presenter = [[RNNBottomTabsPresenter alloc] init];
  44. NSArray *childViewControllers = @[[UIViewController new]];
  45. RNNEventEmitter *eventEmmiter = [RNNEventEmitter new];
  46. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter bottomTabPresenter:nil dotIndicatorPresenter:nil eventEmitter:eventEmmiter childViewControllers:childViewControllers bottomTabsAttacher:nil];
  47. [uut viewWillAppear:YES];
  48. XCTAssertTrue(uut.layoutInfo == layoutInfo);
  49. XCTAssertTrue(uut.options == options);
  50. XCTAssertTrue(uut.presenter == presenter);
  51. XCTAssertTrue(uut.childViewControllers.count == childViewControllers.count);
  52. XCTAssertTrue(uut.eventEmitter == eventEmmiter);
  53. }
  54. - (void)testInitWithLayoutInfo_shouldSetDelegate {
  55. RNNBottomTabsController *uut = [RNNBottomTabsController createWithChildren:@[]];
  56. XCTAssertTrue(uut.delegate == uut);
  57. }
  58. - (void)testInitWithLayoutInfo_shouldCreateWithDefaultStyles {
  59. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNBottomTabsPresenter alloc] init] eventEmitter:nil childViewControllers:nil];
  60. XCTAssertEqual(uut.modalPresentationStyle, UIModalPresentationPageSheet);
  61. XCTAssertEqual(uut.modalTransitionStyle, UIModalTransitionStyleCoverVertical);
  62. }
  63. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  64. [[self.mockTabBarPresenter reject] applyOptionsOnWillMoveToParentViewController:[self.uut options]];
  65. [self.uut willMoveToParentViewController:nil];
  66. [self.mockTabBarPresenter verify];
  67. }
  68. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  69. [[self.mockTabBarPresenter expect] applyOptions:[OCMArg any]];
  70. [self.uut onChildWillAppear];
  71. [self.mockTabBarPresenter verify];
  72. }
  73. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  74. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  75. [(RNNBottomTabsPresenter *) [self.mockTabBarPresenter expect] mergeOptions:options resolvedOptions:[OCMArg any]];
  76. [self.uut mergeOptions:options];
  77. [self.mockTabBarPresenter verify];
  78. }
  79. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  80. id parentMock = [OCMockObject niceMockForClass:[RNNComponentViewController class]];
  81. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  82. OCMStub([self.uut parentViewController]).andReturn(parentMock);
  83. [((RNNComponentViewController *) [parentMock expect]) mergeChildOptions:options child:self.originalUut];
  84. [self.uut mergeOptions:options];
  85. [parentMock verify];
  86. }
  87. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  88. id parentMock = [OCMockObject partialMockForObject:[RNNStackController new]];
  89. OCMStub([self.uut parentViewController]).andReturn(parentMock);
  90. [[parentMock expect] onChildWillAppear];
  91. [self.uut onChildWillAppear];
  92. [parentMock verify];
  93. }
  94. - (void)testViewDidLayoutSubviews_delegateToPresenter {
  95. [[[self mockTabBarPresenter] expect] viewDidLayoutSubviews];
  96. [[self uut] viewDidLayoutSubviews];
  97. [[self mockTabBarPresenter] verify];
  98. }
  99. - (void)testGetCurrentChild_shouldReturnSelectedViewController {
  100. XCTAssertEqual([self.uut getCurrentChild], [(RNNBottomTabsController *) self.uut selectedViewController]);
  101. }
  102. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  103. [[self.mockTabBarPresenter expect] getStatusBarStyle];
  104. [self.uut preferredStatusBarStyle];
  105. [self.mockTabBarPresenter verify];
  106. }
  107. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleTrue {
  108. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(1)];
  109. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  110. }
  111. - (void)testPreferredStatusHidden_shouldResolveChildStatusBarVisibleFalse {
  112. self.uut.getCurrentChild.options.statusBar.visible = [Bool withValue:@(0)];
  113. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  114. }
  115. - (void)testPreferredStatusHidden_shouldHideStatusBar {
  116. self.uut.options.statusBar.visible = [Bool withValue:@(1)];
  117. XCTAssertFalse(self.uut.prefersStatusBarHidden);
  118. }
  119. - (void)testPreferredStatusHidden_shouldShowStatusBar {
  120. self.uut.options.statusBar.visible = [Bool withValue:@(0)];
  121. XCTAssertTrue(self.uut.prefersStatusBarHidden);
  122. }
  123. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  124. NSUInteger selectedIndex = 2;
  125. OCMStub([self.uut selectedIndex]).andReturn(selectedIndex);
  126. [[self.mockEventEmitter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  127. [self.uut tabBarController:self.uut didSelectViewController:[UIViewController new]];
  128. [self.mockEventEmitter verify];
  129. }
  130. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  131. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  132. layoutInfo.componentId = @"componentId";
  133. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  134. RNNBottomTabsController *uut = [RNNBottomTabsController createWithChildren:@[[UIViewController new], vc]];
  135. [uut viewWillAppear:YES];
  136. [uut setSelectedIndexByComponentID:@"componentId"];
  137. XCTAssertTrue(uut.selectedIndex == 1);
  138. }
  139. - (void)testSetSelectedIndex_ShouldSetSelectedIndexWithCurrentTabIndex {
  140. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initEmptyOptions];
  141. options.bottomTabs.currentTabIndex = [[IntNumber alloc] initWithValue:@(1)];
  142. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  143. RNNBottomTabsController *uut = [RNNBottomTabsController createWithChildren:@[[UIViewController new], vc] options:options];
  144. [uut viewWillAppear:YES];
  145. XCTAssertTrue(uut.selectedIndex == 1);
  146. }
  147. - (void)testOnViewDidLayoutSubviews_ShouldUpdateDotIndicatorForChildren {
  148. id dotIndicator = [OCMockObject partialMockForObject:[[RNNDotIndicatorPresenter alloc] initWithDefaultOptions:nil]];
  149. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  150. 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];
  151. [[dotIndicator expect] bottomTabsDidLayoutSubviews:uut];
  152. [uut viewDidLayoutSubviews];
  153. [dotIndicator verify];
  154. }
  155. @end