react-native-navigation的迁移库

RNNTabBarControllerTest.m 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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:@{}] defaultOptions:nil 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:@{}] defaultOptions:nil 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 defaultOptions:nil 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 defaultOptions:nil 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:@{}] defaultOptions:nil presenter:[[RNNViewControllerPresenter alloc] init]];
  60. XCTAssertTrue(uut.delegate == uut);
  61. }
  62. - (void)testWillMoveToParent_invokePresenterApplyOptionsOnWillMoveToParent {
  63. [[self.mockTabBarPresenter expect] applyOptionsOnWillMoveToParentViewController:[(RNNTabBarController *)self.mockUut resolveOptions]];
  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 currentOptions:[(RNNTabBarController *)self.mockUut options] defaultOptions: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)testGetCurrentLeaf_shouldInvokeSelectedViewControllerGetCurrentLeaf {
  99. [[self.mockChildViewController expect] getCurrentLeaf];
  100. [self.mockUut getCurrentLeaf];
  101. [self.mockChildViewController verify];
  102. }
  103. - (void)testGetCurrentChild_shouldReturnSelectedViewController {
  104. XCTAssertEqual([self.mockUut getCurrentChild], [(RNNTabBarController *)self.mockUut selectedViewController]);
  105. }
  106. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  107. [[self.mockChildViewController expect] preferredStatusBarStyle];
  108. [self.mockUut preferredStatusBarStyle];
  109. [self.mockChildViewController verify];
  110. }
  111. - (void)testPreferredStatusBarStyle_shouldInvokeOnSelectedViewController {
  112. [[self.mockChildViewController expect] preferredStatusBarStyle];
  113. [self.mockUut preferredStatusBarStyle];
  114. [self.mockChildViewController verify];
  115. }
  116. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  117. NSUInteger selectedIndex = 2;
  118. OCMStub([self.mockUut selectedIndex]).andReturn(selectedIndex);
  119. [[self.mockEventEmmiter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  120. [self.mockUut tabBarController:self.mockUut didSelectViewController:[UIViewController new]];
  121. [self.mockEventEmmiter verify];
  122. }
  123. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  124. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  125. layoutInfo.componentId = @"componentId";
  126. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  127. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil childViewControllers:@[[UIViewController new], vc] options:nil defaultOptions:nil presenter:[RNNTabBarPresenter new]];
  128. [uut setSelectedIndexByComponentID:@"componentId"];
  129. XCTAssertTrue(uut.selectedIndex == 1);
  130. }
  131. @end