react-native-navigation的迁移库

RNNTabBarControllerTest.m 8.7KB

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