react-native-navigation的迁移库

RNNTabBarControllerTest.m 8.6KB

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