react-native-navigation的迁移库

RNNTabBarControllerTest.m 8.2KB

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