react-native-navigation的迁移库

RNNTabBarControllerTest.m 9.8KB

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