react-native-navigation的迁移库

RNNCommandsHandlerTest.m 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithName:@"name"
  78. withOptions:initialOptions
  79. withComponentId:@"componentId"
  80. rootViewCreator:[[RNNTestRootViewCreator alloc] init]
  81. eventEmitter:nil
  82. isExternalComponent:NO];
  83. UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];
  84. [vc viewWillAppear:false];
  85. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  86. [self.store setReadyToReceiveCommands:true];
  87. [self.store setComponent:vc componentId:@"componentId"];
  88. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  89. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  90. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  91. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  92. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  93. }];
  94. }
  95. - (void)testPop_removeTopVCFromStore {
  96. [self.store setReadyToReceiveCommands:true];
  97. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  98. [self.uut pop:@"vc3" options:nil completion:^{
  99. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  100. XCTAssertNotNil([self.store findComponentForId:@"vc2"]);
  101. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  102. [expectation fulfill];
  103. } rejection:^(NSString *code, NSString *message, NSError *error) {
  104. }];
  105. [self waitForExpectationsWithTimeout:1 handler:nil];
  106. }
  107. - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
  108. [self.store setReadyToReceiveCommands:true];
  109. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  110. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  111. [self.uut popTo:@"vc1" completion:^{
  112. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  113. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  114. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  115. [expectation fulfill];
  116. } rejection:nil];
  117. [self waitForExpectationsWithTimeout:1 handler:nil];
  118. }
  119. - (void)testPopToRoot_removeAllTopVCsFromStore {
  120. [self.store setReadyToReceiveCommands:true];
  121. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  122. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  123. [self.uut popToRoot:@"vc3" completion:^{
  124. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  125. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  126. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  127. [expectation fulfill];
  128. } rejection:nil];
  129. [self waitForExpectationsWithTimeout:1 handler:nil];
  130. }
  131. @end