react-native-navigation的迁移库

RNNCommandsHandlerTest.m 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. NSMutableArray* result = [NSMutableArray new];
  59. // count and names:
  60. int i=0;
  61. unsigned int mc = 0;
  62. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  63. for(i=0; i<mc; i++) {
  64. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  65. // filter skippedMethods
  66. if (methodName && ![skipMethods containsObject:methodName]) {
  67. [result addObject:methodName];
  68. }
  69. }
  70. return result;
  71. }
  72. -(void)testDynamicStylesMergeWithStaticStyles {
  73. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  74. initialOptions.topBar.title.text = @"the title";
  75. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithName:@"name"
  76. withOptions:initialOptions
  77. withComponentId:@"componentId"
  78. rootViewCreator:[[RNNTestRootViewCreator alloc] init]
  79. eventEmitter:nil
  80. isExternalComponent:NO];
  81. UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
  82. [vc viewWillAppear:false];
  83. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  84. [self.store setReadyToReceiveCommands:true];
  85. [self.store setComponent:vc componentId:@"componentId"];
  86. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  87. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  88. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  89. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  90. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  91. }];
  92. }
  93. - (void)testPop_removeTopVCFromStore {
  94. [self.store setReadyToReceiveCommands:true];
  95. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  96. [self.uut pop:@"vc3" options:nil completion:^{
  97. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  98. XCTAssertNotNil([self.store findComponentForId:@"vc2"]);
  99. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  100. [expectation fulfill];
  101. } rejection:^(NSString *code, NSString *message, NSError *error) {
  102. }];
  103. [self waitForExpectationsWithTimeout:1 handler:nil];
  104. }
  105. - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
  106. [self.store setReadyToReceiveCommands:true];
  107. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  108. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  109. [self.uut popTo:@"vc1" completion:^{
  110. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  111. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  112. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  113. [expectation fulfill];
  114. } rejection:nil];
  115. [self waitForExpectationsWithTimeout:1 handler:nil];
  116. }
  117. - (void)testPopToRoot_removeAllTopVCsFromStore {
  118. [self.store setReadyToReceiveCommands:true];
  119. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  120. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  121. [self.uut popToRoot:@"vc3" completion:^{
  122. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  123. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  124. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  125. [expectation fulfill];
  126. } rejection:nil];
  127. [self waitForExpectationsWithTimeout:1 handler:nil];
  128. }
  129. @end