react-native-navigation的迁移库

RNNDotIndicatorPresenterTest.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #import <XCTest/XCTest.h>
  2. #import <OCMock/OCMock.h>
  3. #import "RNNDotIndicatorPresenter.h"
  4. #import "DotIndicatorOptions.h"
  5. #import "RNNBottomTabsController.h"
  6. #import <ReactNativeNavigation/RNNComponentViewController.h>
  7. #import "RNNTestBase.h"
  8. #import "UITabBarController+RNNUtils.h"
  9. #import <ReactNativeNavigation/BottomTabPresenterCreator.h>
  10. #import "RNNBottomTabsController+Helpers.h"
  11. @interface RNNDotIndicatorPresenterTest : RNNTestBase
  12. @property(nonatomic, strong) id uut;
  13. @property(nonatomic, strong) RNNComponentViewController *child;
  14. @property(nonatomic, strong) id bottomTabs;
  15. @property(nonatomic, strong) BottomTabPresenter* bottomTabPresenter;
  16. @end
  17. @implementation RNNDotIndicatorPresenterTest
  18. - (void)setUp {
  19. [super setUp];
  20. self.child = [self createChild];
  21. self.bottomTabPresenter = [BottomTabPresenterCreator createWithDefaultOptions:nil];
  22. self.uut = [OCMockObject partialMockForObject:[RNNDotIndicatorPresenter new]];
  23. self.bottomTabs = [OCMockObject partialMockForObject:[RNNBottomTabsController createWithChildren:@[self.child]]];
  24. [self setupTopLevelUI:self.bottomTabs];
  25. }
  26. - (void)tearDown {
  27. [self tearDownTopLevelUI:_bottomTabs];
  28. [super tearDown];
  29. }
  30. - (void)testApply_doesNothingIfDoesNotHaveValue {
  31. DotIndicatorOptions *empty = [DotIndicatorOptions new];
  32. [[self uut] apply:self.child :empty];
  33. XCTAssertFalse([self tabHasIndicator]);
  34. }
  35. - (void)testApply_indicatorIsAddedToTabView {
  36. [self applyIndicator];
  37. XCTAssertTrue([self tabHasIndicator]);
  38. }
  39. - (void)testApply_indicatorIsRemovedIfNotVisible {
  40. [self applyIndicator];
  41. XCTAssertTrue([self tabHasIndicator]);
  42. DotIndicatorOptions *options = [DotIndicatorOptions new];
  43. options.visible = [[Bool alloc] initWithBOOL:NO];
  44. [[self uut] apply:self.child :options];
  45. XCTAssertFalse([self tabHasIndicator]);
  46. }
  47. - (void)testApply_invisibleIndicatorIsNotAdded {
  48. DotIndicatorOptions *options = [DotIndicatorOptions new];
  49. options.visible = [[Bool alloc] initWithBOOL:NO];
  50. [[self uut] apply:self.child :options];
  51. XCTAssertFalse([self tabHasIndicator]);
  52. }
  53. - (void)testApply_itDoesNotRecreateIfEqualToCurrentlyVisibleIndicator {
  54. [self applyIndicator];
  55. UIView *indicator1 = [self getIndicator];
  56. [self applyIndicator];
  57. UIView *indicator2 = [self getIndicator];
  58. XCTAssertEqualObjects(indicator1, indicator2);
  59. }
  60. - (void)testApply_itAddsIndicatorToCorrectTabView {
  61. [self applyIndicator];
  62. UIView *indicator1 = [self getIndicator];
  63. XCTAssertEqualObjects([indicator1 superview], [_bottomTabs getTabView:0]);
  64. }
  65. - (void)testApply_itRemovesPreviousDotIndicator {
  66. NSUInteger childCountBeforeApplyingIndicator = [[_bottomTabs getTabView:0] subviews].count;
  67. [self applyIndicator];
  68. NSUInteger childCountAfterApplyingIndicatorOnce = [[_bottomTabs getTabView:0] subviews].count;
  69. XCTAssertEqual(childCountBeforeApplyingIndicator + 1, childCountAfterApplyingIndicatorOnce);
  70. [self applyIndicator:[UIColor greenColor]];
  71. NSUInteger childCountAfterApplyingIndicatorTwice = [[_bottomTabs getTabView:0] subviews].count;
  72. XCTAssertEqual([[self getIndicator] backgroundColor], [UIColor greenColor]);
  73. XCTAssertEqual(childCountAfterApplyingIndicatorOnce, childCountAfterApplyingIndicatorTwice);
  74. }
  75. - (void)testApply_itRemovesPreviousIndicator {
  76. DotIndicatorOptions *options = [DotIndicatorOptions new];
  77. options.visible = [[Bool alloc] initWithBOOL:YES];
  78. options.color = [[Color alloc] initWithValue:[UIColor redColor]];
  79. options.size = [[Number alloc] initWithValue:[[NSNumber alloc] initWithInt:8]];
  80. [[self uut] apply:self.child :options];
  81. XCTAssertTrue([self tabHasIndicator]);
  82. options.visible = [[Bool alloc] initWithBOOL:NO];
  83. [[self uut] apply:self.child :options];
  84. XCTAssertFalse([self tabHasIndicator]);
  85. }
  86. - (void)testApply_indicatorIsAlignedToTopRightOfIcon {
  87. DotIndicatorOptions *options = [DotIndicatorOptions new];
  88. options.visible = [[Bool alloc] initWithBOOL:YES];
  89. options.size = [[Number alloc] initWithValue:[[NSNumber alloc] initWithInt:8]];
  90. [[self uut] apply:self.child :options];
  91. UIView *indicator = [self getIndicator];
  92. UIView * icon = [_bottomTabs getTabIcon:0];
  93. NSArray<NSLayoutConstraint *> *alignmentConstraints = [_bottomTabs getTabView:0].constraints;
  94. XCTAssertEqual([alignmentConstraints count], 2);
  95. XCTAssertEqual([alignmentConstraints[0] constant], -4);
  96. XCTAssertEqual([alignmentConstraints[0] firstItem], indicator);
  97. XCTAssertEqual([alignmentConstraints[0] secondItem], icon);
  98. XCTAssertEqual([alignmentConstraints[0] firstAttribute], NSLayoutAttributeLeft);
  99. XCTAssertEqual([alignmentConstraints[0] secondAttribute], NSLayoutAttributeRight);
  100. XCTAssertEqual([alignmentConstraints[1] constant], -4);
  101. XCTAssertEqual([alignmentConstraints[1] firstItem], indicator);
  102. XCTAssertEqual([alignmentConstraints[1] secondItem], icon);
  103. XCTAssertEqual([alignmentConstraints[1] firstAttribute], NSLayoutAttributeTop);
  104. XCTAssertEqual([alignmentConstraints[1] secondAttribute], NSLayoutAttributeTop);
  105. NSArray *sizeConstraints = indicator.constraints;
  106. XCTAssertEqual([sizeConstraints count], 2);
  107. XCTAssertEqual([sizeConstraints[0] constant], 8);
  108. XCTAssertEqual([sizeConstraints[1] constant], 8);
  109. }
  110. - (void)testApply_onBottomTabsViewDidLayout {
  111. [[self.uut expect] apply:self.child :self.child.resolveOptions.bottomTab.dotIndicator];
  112. [self.uut bottomTabsDidLayoutSubviews:self.bottomTabs];
  113. [self.uut verify];
  114. }
  115. - (void)applyIndicator {
  116. [self applyIndicator:[UIColor redColor]];
  117. }
  118. - (void)applyIndicator:(UIColor *) color {
  119. DotIndicatorOptions *options = [DotIndicatorOptions new];
  120. options.visible = [[Bool alloc] initWithBOOL:YES];
  121. options.color = [[Color alloc] initWithValue:color];
  122. [[self uut] apply:self.child :options];
  123. }
  124. - (RNNComponentViewController *)createChild {
  125. RNNNavigationOptions *options = [RNNNavigationOptions new];
  126. options.bottomTab = [RNNBottomTabOptions new];
  127. id img = [OCMockObject partialMockForObject:[UIImage new]];
  128. options.bottomTab.icon = [[Image alloc] initWithValue:img];
  129. return [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:nil eventEmitter:nil presenter:[[RNNComponentPresenter alloc] initWithDefaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions]] options:options defaultOptions:nil];
  130. }
  131. - (BOOL)tabHasIndicator {
  132. return [self.child tabBarItem].tag > 0;
  133. }
  134. - (UIView *)getIndicator {
  135. return [self tabHasIndicator] ? [[((UITabBarController *) _bottomTabs) tabBar] viewWithTag:_child.tabBarItem.tag] : nil;
  136. }
  137. @end