react-native-navigation的迁移库

RNNTabBarControllerTest.m 9.8KB

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