react-native-navigation的迁移库

RNNCommandsHandlerTest.m 5.8KB

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