react-native-navigation的迁移库

RNNTabBarControllerTest.m 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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]];
  23. [self.mockUut setViewControllers:@[[[UIViewController alloc] init]]];
  24. OCMStub([self.mockUut selectedViewController]).andReturn(self.mockChildViewController);
  25. }
  26. - (void)testInitWithLayoutInfo_shouldBindPresenter {
  27. XCTAssertNotNil([self.mockUut presenter]);
  28. }
  29. - (void)testInitWithLayoutInfo_shouldSetMultipleViewControllers {
  30. UIViewController* vc1 = [[UIViewController alloc] init];
  31. UIViewController* vc2 = [[UIViewController alloc] init];
  32. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNViewControllerPresenter alloc] init] eventEmitter:nil];
  33. [uut setViewControllers:@[vc1, vc2]];
  34. XCTAssertTrue(uut.viewControllers.count == 2);
  35. }
  36. - (void)testInitWithLayoutInfo_shouldInitializeDependencies {
  37. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  38. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  39. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  40. NSArray* childViewControllers = @[[UIViewController new]];
  41. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:nil];
  42. [uut setViewControllers: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. RNNTabBarPresenter* presenter = [[RNNTabBarPresenter alloc] init];
  52. RNNEventEmitter* eventEmmiter = [RNNEventEmitter new];
  53. NSArray* childViewControllers = @[[UIViewController new]];
  54. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:layoutInfo creator:nil options:options defaultOptions:nil presenter:presenter eventEmitter:eventEmmiter];
  55. [uut setViewControllers: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. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNBasePresenter alloc] init] eventEmitter:nil];
  64. XCTAssertTrue(uut.delegate == uut);
  65. }
  66. - (void)testWillMoveToParent_shouldNotInvokePresenterApplyOptionsOnWillMoveToNilParent {
  67. [[self.mockTabBarPresenter reject] applyOptionsOnWillMoveToParentViewController:[(RNNTabBarController *)self.mockUut options]];
  68. [self.mockUut willMoveToParentViewController:nil];
  69. [self.mockTabBarPresenter verify];
  70. }
  71. - (void)testOnChildAppear_shouldInvokePresenterApplyOptionsWithResolvedOptions {
  72. [[self.mockTabBarPresenter expect] applyOptions:[OCMArg any]];
  73. [self.mockUut onChildWillAppear];
  74. [self.mockTabBarPresenter verify];
  75. }
  76. - (void)testMergeOptions_shouldInvokePresenterMergeOptions {
  77. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  78. [(RNNTabBarPresenter *)[self.mockTabBarPresenter expect] mergeOptions:options currentOptions:[(RNNTabBarController *)self.mockUut options] defaultOptions:nil];
  79. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  80. [self.mockTabBarPresenter verify];
  81. }
  82. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  83. id parentMock = [OCMockObject partialMockForObject:[RNNRootViewController new]];
  84. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  85. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  86. [((RNNRootViewController *)[parentMock expect]) mergeOptions:options];
  87. [(RNNTabBarController *)self.mockUut mergeOptions:options];
  88. [parentMock verify];
  89. }
  90. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  91. id parentMock = [OCMockObject partialMockForObject:[RNNNavigationController new]];
  92. OCMStub([self.mockUut parentViewController]).andReturn(parentMock);
  93. [[parentMock expect] onChildWillAppear];
  94. [self.mockUut onChildWillAppear];
  95. [parentMock verify];
  96. }
  97. - (void)testGetCurrentChild_shouldReturnSelectedViewController {
  98. XCTAssertEqual([self.mockUut getCurrentChild], [(RNNTabBarController *)self.mockUut selectedViewController]);
  99. }
  100. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  101. [[self.mockChildViewController expect] preferredStatusBarStyle];
  102. [self.mockUut preferredStatusBarStyle];
  103. [self.mockChildViewController verify];
  104. }
  105. - (void)testPreferredStatusBarStyle_shouldInvokeOnSelectedViewController {
  106. [[self.mockChildViewController expect] preferredStatusBarStyle];
  107. [self.mockUut preferredStatusBarStyle];
  108. [self.mockChildViewController verify];
  109. }
  110. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  111. NSUInteger selectedIndex = 2;
  112. OCMStub([self.mockUut selectedIndex]).andReturn(selectedIndex);
  113. [[self.mockEventEmmiter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  114. [self.mockUut tabBarController:self.mockUut didSelectViewController:[UIViewController new]];
  115. [self.mockEventEmmiter verify];
  116. }
  117. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  118. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  119. layoutInfo.componentId = @"componentId";
  120. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  121. RNNTabBarController* uut = [[RNNTabBarController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:[RNNTabBarPresenter new] eventEmitter:nil];
  122. [uut setViewControllers:@[[UIViewController new], vc]];
  123. [uut setSelectedIndexByComponentID:@"componentId"];
  124. XCTAssertTrue(uut.selectedIndex == 1);
  125. }
  126. @end