react-native-navigation的迁移库

RNNTabBarControllerTest.m 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 childViewControllers:@[[[UIViewController alloc] init]] options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:self.mockTabBarPresenter eventEmitter:self.mockEventEmmiter]];
  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 childViewControllers:@[vc1, vc2] options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:[[RNNViewControllerPresenter alloc] init]];
  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 childViewControllers:childViewControllers options:options presenter:presenter];
  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 childViewControllers:childViewControllers options:options presenter:presenter eventEmitter:eventEmmiter];
  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 childViewControllers:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] presenter:[[RNNViewControllerPresenter alloc] init]];
  60. XCTAssertTrue(uut.delegate == uut);
  61. }
  62. - (void)testWillMoveToParent_invokePresenterApplyOptionsOnWillMoveToParent {
  63. [[self.mockTabBarPresenter expect] applyOptionsOnWillMoveToParentViewController:[(RNNTabBarController *)self.mockUut options]];
  64. [self.mockUut willMoveToParentViewController:[UIViewController new]];
  65. [self.mockTabBarPresenter verify];
  66. }
  67. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  68. [[self.mockTabBarPresenter reject] applyOptionsOnWillMoveToParentViewController:[(RNNTabBarController *)self.mockUut options]];
  69. [self.mockUut willMoveToParentViewController:nil];
  70. [self.mockTabBarPresenter verify];
  71. }
  72. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  73. [[self.mockTabBarPresenter expect] applyOptions:[OCMArg any]];
  74. [self.mockUut onChildWillAppear];
  75. [self.mockTabBarPresenter verify];
  76. }
  77. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  78. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  79. [(RNNTabBarPresenter *)[self.mockTabBarPresenter expect] mergeOptions:options resolvedOptions:nil];
  80. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  81. [self.mockTabBarPresenter verify];
  82. }
  83. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  84. id parentMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  85. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  86. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  87. [((RNNRootViewController *)[parentMock expect]) mergeOptions:options];
  88. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  89. [parentMock verify];
  90. }
  91. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  92. id parentMock = [OCMockObject partialMockForObject:[RNNNavigationController new]];
  93. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  94. [[parentMock expect] onChildWillAppear];
  95. [self.mockUut onChildWillAppear];
  96. [parentMock verify];
  97. }
  98. - (void)testGetCurrentChild_shouldInvokeSelectedViewControllerGetCurrentChild {
  99. [[self.mockChildViewController expect] getCurrentChild];
  100. [self.mockUut getCurrentChild];
  101. [self.mockChildViewController verify];
  102. }
  103. - (void)testGetCurrentChild_shouldInvokeOnSelectedViewController {
  104. [[self.mockChildViewController expect] getCurrentChild];
  105. [self.mockUut getCurrentChild];
  106. [self.mockChildViewController verify];
  107. }
  108. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  109. [[self.mockChildViewController expect] preferredStatusBarStyle];
  110. [self.mockUut preferredStatusBarStyle];
  111. [self.mockChildViewController verify];
  112. }
  113. - (void)testPreferredStatusBarStyle_shouldInvokeOnSelectedViewController {
  114. [[self.mockChildViewController expect] preferredStatusBarStyle];
  115. [self.mockUut preferredStatusBarStyle];
  116. [self.mockChildViewController verify];
  117. }
  118. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  119. NSUInteger selectedIndex = 2;
  120. OCMStub([self.mockUut selectedIndex]).andReturn(selectedIndex);
  121. [[self.mockEventEmmiter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  122. [self.mockUut tabBarController:self.mockUut didSelectViewController:[UIViewController new]];
  123. [self.mockEventEmmiter verify];
  124. }
  125. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  126. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  127. layoutInfo.componentId = @"componentId";
  128. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil];
  129. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[UIViewController new], vc] options:nil presenter:[RNNTabBarPresenter new]];
  130. [uut setSelectedIndexByComponentID:@"componentId"];
  131. XCTAssertTrue(uut.selectedIndex == 1);
  132. }
  133. @end