react-native-navigation的迁移库

RNNTabBarControllerTest.m 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #import <XCTest/XCTest.h>
  2. #import "RNNTabBarController.h"
  3. #import "RNNNavigationOptions.h"
  4. #import "RNNTabBarPresenter.h"
  5. #import "RNNRootViewController.h"
  6. #import "RNNNavigationController.h"
  7. #import <OCMock/OCMock.h>
  8. @interface RNNTabBarControllerTest : XCTestCase
  9. @property (nonatomic, strong) id mockUut;
  10. @property (nonatomic, strong) id mockChildViewController;
  11. @property (nonatomic, strong) id mockEventEmmiter;
  12. @property (nonatomic, strong) id mockTabBarPresenter;
  13. @end
  14. @implementation RNNTabBarControllerTest
  15. - (void)setUp {
  16. [super setUp];
  17. id tabBarClassMock = OCMClassMock([RNNTabBarController class]);
  18. OCMStub([tabBarClassMock parentViewController]).andReturn([OCMockObject partialMockForObject:[RNNTabBarController new]]);
  19. self.mockTabBarPresenter = [OCMockObject partialMockForObject:[[RNNTabBarPresenter alloc] init]];
  20. self.mockChildViewController = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  21. self.mockEventEmmiter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  22. self.mockUut = [OCMockObject partialMockForObject:[[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:self.mockTabBarPresenter eventEmitter:self.mockEventEmmiter childViewControllers:@[[[UIViewController alloc] init]]]];
  23. OCMStub([self.mockUut selectedViewController]).andReturn(self.mockChildViewController);
  24. }
  25. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  26. XCTAssertNotNil([self.mockUut presenter]);
  27. }
  28. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  29. UIViewController* vc1 = [[UIViewController alloc] init];
  30. UIViewController* vc2 = [[UIViewController alloc] init];
  31. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNViewControllerPresenter alloc] init] eventEmitter:nil childViewControllers:@[vc1, vc2]];
  32. XCTAssertTrue(uut.viewControllers.count == 2);
  33. }
  34. - (void)testInitWithLayoutInfo_shouldInitializeDependencies {
  35. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  36. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  37. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  38. NSArray* childViewControllers = @[[UIViewController new]];
  39. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:nil childViewControllers:childViewControllers];
  40. XCTAssertTrue(uut.layoutInfo == layoutInfo);
  41. XCTAssertTrue(uut.options == options);
  42. XCTAssertTrue(uut.presenter == presenter);
  43. XCTAssertTrue(uut.childViewControllers.count == childViewControllers.count);
  44. }
  45. - (void)testInitWithEventEmmiter_shouldInitializeDependencies {
  46. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  47. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  48. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  49. RNNEventEmitter* eventEmmiter = [RNNEventEmitter new];
  50. NSArray* childViewControllers = @[[UIViewController new]];
  51. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:eventEmmiter childViewControllers:childViewControllers];
  52. XCTAssertTrue(uut.layoutInfo == layoutInfo);
  53. XCTAssertTrue(uut.options == options);
  54. XCTAssertTrue(uut.presenter == presenter);
  55. XCTAssertTrue(uut.childViewControllers.count == childViewControllers.count);
  56. XCTAssertTrue(uut.eventEmitter == eventEmmiter);
  57. }
  58. - (void)testInitWithLayoutInfo_shouldSetDelegate {
  59. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNBasePresenter alloc] init] eventEmitter:nil childViewControllers:nil];
  60. XCTAssertTrue(uut.delegate == uut);
  61. }
  62. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  63. [[self.mockTabBarPresenter reject] applyOptionsOnWillMoveToParentViewController:[(RNNTabBarController *)self.mockUut options]];
  64. [self.mockUut willMoveToParentViewController:nil];
  65. [self.mockTabBarPresenter verify];
  66. }
  67. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  68. [[self.mockTabBarPresenter expect] applyOptions:[OCMArg any]];
  69. [self.mockUut onChildWillAppear];
  70. [self.mockTabBarPresenter verify];
  71. }
  72. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  73. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  74. [(RNNTabBarPresenter *)[self.mockTabBarPresenter expect] mergeOptions:options currentOptions:[(RNNTabBarController *)self.mockUut options] defaultOptions:nil];
  75. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  76. [self.mockTabBarPresenter verify];
  77. }
  78. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  79. id parentMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  80. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  81. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  82. [((RNNRootViewController *)[parentMock expect]) mergeOptions:options];
  83. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  84. [parentMock verify];
  85. }
  86. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  87. id parentMock = [OCMockObject partialMockForObject:[RNNNavigationController new]];
  88. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  89. [[parentMock expect] onChildWillAppear];
  90. [self.mockUut onChildWillAppear];
  91. [parentMock verify];
  92. }
  93. - (void)testGetCurrentChild_shouldReturnSelectedViewController {
  94. XCTAssertEqual([self.mockUut getCurrentChild], [(RNNTabBarController *)self.mockUut selectedViewController]);
  95. }
  96. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  97. [[self.mockChildViewController expect] preferredStatusBarStyle];
  98. [self.mockUut preferredStatusBarStyle];
  99. [self.mockChildViewController verify];
  100. }
  101. - (void)testPreferredStatusBarStyle_shouldInvokeOnSelectedViewController {
  102. [[self.mockChildViewController expect] preferredStatusBarStyle];
  103. [self.mockUut preferredStatusBarStyle];
  104. [self.mockChildViewController verify];
  105. }
  106. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  107. NSUInteger selectedIndex = 2;
  108. OCMStub([self.mockUut selectedIndex]).andReturn(selectedIndex);
  109. [[self.mockEventEmmiter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  110. [self.mockUut tabBarController:self.mockUut didSelectViewController:[UIViewController new]];
  111. [self.mockEventEmmiter verify];
  112. }
  113. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  114. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  115. layoutInfo.componentId = @"componentId";
  116. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  117. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:[RNNTabBarPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  118. [uut setSelectedIndexByComponentID:@"componentId"];
  119. XCTAssertTrue(uut.selectedIndex == 1);
  120. }
  121. - (void)testSetSelectedIndex_ShouldSetSelectedIndexWithCurrentTabIndex {
  122. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  123. options.bottomTabs.currentTabIndex = [[IntNumber alloc] initWithValue:@(1)];
  124. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  125. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:nil presenter:[RNNTabBarPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  126. XCTAssertTrue(uut.selectedIndex == 1);
  127. }
  128. @end