react-native-navigation的迁移库

RNNTabBarControllerTest.m 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #import <XCTest/XCTest.h>
  2. #import "RNNBottomTabsController.h"
  3. #import "RNNComponentViewController.h"
  4. #import "RNNStackController.h"
  5. #import <OCMock/OCMock.h>
  6. @interface RNNTabBarControllerTest : XCTestCase
  7. @property(nonatomic, strong) RNNBottomTabsController * 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([RNNBottomTabsController class]);
  16. OCMStub([tabBarClassMock parentViewController]).andReturn([OCMockObject partialMockForObject:[RNNBottomTabsController new]]);
  17. self.mockTabBarPresenter = [OCMockObject partialMockForObject:[[RNNBottomTabsPresenter alloc] init]];
  18. self.mockChildViewController = [OCMockObject partialMockForObject:[RNNComponentViewController new]];
  19. self.mockEventEmitter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  20. self.uut = [OCMockObject partialMockForObject:[[RNNBottomTabsController 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. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil presenter:[[RNNComponentPresenter 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. RNNBottomTabsPresenter *presenter = [[RNNBottomTabsPresenter alloc] init];
  36. NSArray *childViewControllers = @[[UIViewController new]];
  37. RNNBottomTabsController *uut = [[RNNBottomTabsController 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. RNNBottomTabsPresenter *presenter = [[RNNBottomTabsPresenter alloc] init];
  47. RNNEventEmitter *eventEmmiter = [RNNEventEmitter new];
  48. NSArray *childViewControllers = @[[UIViewController new]];
  49. RNNBottomTabsController *uut = [[RNNBottomTabsController 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. RNNBottomTabsController *uut = [[RNNBottomTabsController 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. [(RNNBottomTabsPresenter *) [self.mockTabBarPresenter expect] mergeOptions:options resolvedOptions:[self.uut options]];
  73. [self.uut mergeOptions:options];
  74. [self.mockTabBarPresenter verify];
  75. }
  76. - (void)testMergeOptions_shouldInvokeParentMergeOptions {
  77. id parentMock = [OCMockObject partialMockForObject:[RNNComponentViewController new]];
  78. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initWithDict:@{}];
  79. OCMStub([self.uut parentViewController]).andReturn(parentMock);
  80. [((RNNComponentViewController *) [parentMock expect]) mergeChildOptions:options];
  81. [self.uut mergeOptions:options];
  82. [parentMock verify];
  83. }
  84. - (void)testOnChildAppear_shouldInvokeParentOnChildAppear {
  85. id parentMock = [OCMockObject partialMockForObject:[RNNStackController 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], [(RNNBottomTabsController *) self.uut selectedViewController]);
  98. }
  99. - (void)testPreferredStatusBarStyle_shouldInvokeSelectedViewControllerPreferredStatusBarStyle {
  100. [[self.mockTabBarPresenter expect] getStatusBarStyle:[OCMArg any]];
  101. [self.uut preferredStatusBarStyle];
  102. [self.mockTabBarPresenter verify];
  103. }
  104. - (void)testTabBarControllerDidSelectViewControllerDelegate_shouldInvokeSendBottomTabSelectedEvent {
  105. NSUInteger selectedIndex = 2;
  106. OCMStub([self.uut selectedIndex]).andReturn(selectedIndex);
  107. [[self.mockEventEmitter expect] sendBottomTabSelected:@(selectedIndex) unselected:@(0)];
  108. [self.uut tabBarController:self.uut didSelectViewController:[UIViewController new]];
  109. [self.mockEventEmitter verify];
  110. }
  111. - (void)testSetSelectedIndexByComponentID_ShouldSetSelectedIndexWithCorrectIndex {
  112. RNNLayoutInfo *layoutInfo = [RNNLayoutInfo new];
  113. layoutInfo.componentId = @"componentId";
  114. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  115. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:nil defaultOptions:nil presenter:[RNNBottomTabsPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  116. [uut setSelectedIndexByComponentID:@"componentId"];
  117. XCTAssertTrue(uut.selectedIndex == 1);
  118. }
  119. - (void)testSetSelectedIndex_ShouldSetSelectedIndexWithCurrentTabIndex {
  120. RNNNavigationOptions *options = [[RNNNavigationOptions alloc] initEmptyOptions];
  121. options.bottomTabs.currentTabIndex = [[IntNumber alloc] initWithValue:@(1)];
  122. RNNComponentViewController *vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:nil options:nil defaultOptions:nil];
  123. RNNBottomTabsController *uut = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:nil presenter:[RNNBottomTabsPresenter new] eventEmitter:nil childViewControllers:@[[UIViewController new], vc]];
  124. XCTAssertTrue(uut.selectedIndex == 1);
  125. }
  126. @end