react-native-navigation的迁移库

RNNCommandsHandlerTest.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #import <XCTest/XCTest.h>
  2. #import <objc/runtime.h>
  3. #import "RNNCommandsHandler.h"
  4. #import "RNNNavigationOptions.h"
  5. #import "RNNTestRootViewCreator.h"
  6. #import "RNNRootViewController.h"
  7. #import "RNNNavigationStackManager.h"
  8. #import "RNNNavigationController.h"
  9. @interface MockUINavigationController : RNNNavigationController
  10. @property (nonatomic, strong) NSArray* willReturnVCs;
  11. @end
  12. @implementation MockUINavigationController
  13. -(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
  14. return self.willReturnVCs;
  15. }
  16. -(NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
  17. return self.willReturnVCs;
  18. }
  19. @end
  20. @interface RNNCommandsHandlerTest : XCTestCase
  21. @property (nonatomic, strong) RNNStore* store;
  22. @property (nonatomic, strong) RNNCommandsHandler* uut;
  23. @property (nonatomic, strong) RNNRootViewController* vc1;
  24. @property (nonatomic, strong) RNNRootViewController* vc2;
  25. @property (nonatomic, strong) RNNRootViewController* vc3;
  26. @property (nonatomic, strong) MockUINavigationController* nvc;
  27. @end
  28. @implementation RNNCommandsHandlerTest
  29. - (void)setUp {
  30. [super setUp];
  31. // [self.store setReadyToReceiveCommands:true];
  32. self.store = [[RNNStore alloc] init];
  33. self.uut = [[RNNCommandsHandler alloc] initWithStore:self.store controllerFactory:nil eventEmitter:nil];
  34. self.vc1 = [RNNRootViewController new];
  35. self.vc2 = [RNNRootViewController new];
  36. self.vc3 = [RNNRootViewController new];
  37. _nvc = [[MockUINavigationController alloc] init];
  38. [_nvc setViewControllers:@[self.vc1, self.vc2, self.vc3]];
  39. [self.store setComponent:self.vc1 componentId:@"vc1"];
  40. [self.store setComponent:self.vc2 componentId:@"vc2"];
  41. [self.store setComponent:self.vc3 componentId:@"vc3"];
  42. }
  43. - (void)testAssertReadyForEachMethodThrowsExceptoins {
  44. NSArray* methods = [self getPublicMethodNamesForObject:self.uut];
  45. [self.store setReadyToReceiveCommands:false];
  46. for (NSString* methodName in methods) {
  47. SEL s = NSSelectorFromString(methodName);
  48. IMP imp = [self.uut methodForSelector:s];
  49. void (*func)(id, SEL, id, id, id) = (void *)imp;
  50. XCTAssertThrowsSpecificNamed(func(self.uut,s, nil, nil, nil), NSException, @"BridgeNotLoadedError");
  51. }
  52. }
  53. -(NSArray*) getPublicMethodNamesForObject:(NSObject*)obj{
  54. NSMutableArray* skipMethods = [NSMutableArray new];
  55. [skipMethods addObject:@"initWithStore:controllerFactory:eventEmitter:"];
  56. [skipMethods addObject:@"assertReady"];
  57. [skipMethods addObject:@"removePopedViewControllers:"];
  58. [skipMethods addObject:@".cxx_destruct"];
  59. [skipMethods addObject:@"dismissedModal:"];
  60. [skipMethods addObject:@"dismissedMultipleModals:"];
  61. NSMutableArray* result = [NSMutableArray new];
  62. // count and names:
  63. int i=0;
  64. unsigned int mc = 0;
  65. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  66. for(i=0; i<mc; i++) {
  67. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  68. // filter skippedMethods
  69. if (methodName && ![skipMethods containsObject:methodName]) {
  70. [result addObject:methodName];
  71. }
  72. }
  73. return result;
  74. }
  75. -(void)testDynamicStylesMergeWithStaticStyles {
  76. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  77. initialOptions.topBar.title.text = @"the title";
  78. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  79. RNNTestRootViewCreator* creator = [[RNNTestRootViewCreator alloc] init];
  80. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  81. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:creator eventEmitter:nil presenter:presenter options:initialOptions];
  82. RNNNavigationController* nav = [[RNNNavigationController alloc] initWithRootViewController:vc];
  83. nav.presenter = [[RNNNavigationControllerPresenter alloc] init];
  84. nav.options = initialOptions;
  85. [vc viewWillAppear:false];
  86. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  87. [self.store setReadyToReceiveCommands:true];
  88. [self.store setComponent:vc componentId:@"componentId"];
  89. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  90. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  91. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  92. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  93. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  94. }];
  95. }
  96. - (void)testMergeOptions_shouldOverrideOptions {
  97. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  98. initialOptions.topBar.title.text = @"the title";
  99. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  100. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions];
  101. __unused RNNNavigationController* nav = [[RNNNavigationController alloc] initWithRootViewController:vc];
  102. [vc viewWillAppear:false];
  103. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  104. [self.store setReadyToReceiveCommands:true];
  105. [self.store setComponent:vc componentId:@"componentId"];
  106. NSDictionary* dictFromJs = @{@"topBar": @{@"title" : @{@"text" : @"new title"}}};
  107. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  108. XCTAssertTrue([vc.navigationItem.title isEqual:@"new title"]);
  109. }];
  110. }
  111. - (void)testPop_removeTopVCFromStore {
  112. [self.store setReadyToReceiveCommands:true];
  113. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  114. [self.uut pop:@"vc3" mergeOptions:nil completion:^{
  115. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  116. XCTAssertNotNil([self.store findComponentForId:@"vc2"]);
  117. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  118. [expectation fulfill];
  119. } rejection:^(NSString *code, NSString *message, NSError *error) {
  120. }];
  121. [self waitForExpectationsWithTimeout:1 handler:nil];
  122. }
  123. - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
  124. [self.store setReadyToReceiveCommands:true];
  125. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  126. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  127. [self.uut popTo:@"vc1" mergeOptions:nil completion:^{
  128. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  129. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  130. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  131. [expectation fulfill];
  132. } rejection:nil];
  133. [self waitForExpectationsWithTimeout:1 handler:nil];
  134. }
  135. - (void)testPopToRoot_removeAllTopVCsFromStore {
  136. [self.store setReadyToReceiveCommands:true];
  137. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  138. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  139. [self.uut popToRoot:@"vc3" mergeOptions:nil completion:^{
  140. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  141. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  142. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  143. [expectation fulfill];
  144. } rejection:nil];
  145. [self waitForExpectationsWithTimeout:1 handler:nil];
  146. }
  147. @end