react-native-navigation的迁移库

RNNCommandsHandlerTest.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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:[[RNNControllerFactory alloc] initWithRootViewCreator:nil store:self.store eventEmitter:nil andBridge: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 = [[Text alloc] initWithValue:@"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] initWithLayoutInfo:nil childViewControllers:@[vc] options:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[[RNNNavigationControllerPresenter alloc] init]];
  83. [vc viewWillAppear:false];
  84. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  85. [self.store setReadyToReceiveCommands:true];
  86. [self.store setComponent:vc componentId:@"componentId"];
  87. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  88. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  89. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  90. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  91. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  92. }];
  93. }
  94. - (void)testMergeOptions_shouldOverrideOptions {
  95. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  96. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  97. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  98. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions];
  99. __unused RNNNavigationController* nav = [[RNNNavigationController alloc] initWithRootViewController:vc];
  100. [vc viewWillAppear:false];
  101. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  102. [self.store setReadyToReceiveCommands:true];
  103. [self.store setComponent:vc componentId:@"componentId"];
  104. NSDictionary* dictFromJs = @{@"topBar": @{@"title" : @{@"text" : @"new title"}}};
  105. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  106. XCTAssertTrue([vc.navigationItem.title isEqual:@"new title"]);
  107. }];
  108. }
  109. - (void)testPop_removeTopVCFromStore {
  110. [self.store setReadyToReceiveCommands:true];
  111. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  112. [self.uut pop:@"vc3" mergeOptions:nil completion:^{
  113. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  114. XCTAssertNotNil([self.store findComponentForId:@"vc2"]);
  115. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  116. [expectation fulfill];
  117. } rejection:^(NSString *code, NSString *message, NSError *error) {
  118. }];
  119. [self waitForExpectationsWithTimeout:1 handler:nil];
  120. }
  121. - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
  122. [self.store setReadyToReceiveCommands:true];
  123. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  124. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  125. [self.uut popTo:@"vc1" mergeOptions:nil completion:^{
  126. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  127. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  128. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  129. [expectation fulfill];
  130. } rejection:nil];
  131. [self waitForExpectationsWithTimeout:1 handler:nil];
  132. }
  133. - (void)testPopToRoot_removeAllTopVCsFromStore {
  134. [self.store setReadyToReceiveCommands:true];
  135. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  136. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  137. [self.uut popToRoot:@"vc3" mergeOptions:nil completion:^{
  138. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  139. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  140. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  141. [expectation fulfill];
  142. } rejection:nil];
  143. [self waitForExpectationsWithTimeout:1 handler:nil];
  144. }
  145. @end