react-native-navigation的迁移库

RNNCommandsHandlerTest.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "RNNNavigationController.h"
  8. #import "RNNErrorHandler.h"
  9. #import <OCMock/OCMock.h>
  10. @interface MockUINavigationController : RNNNavigationController
  11. @property (nonatomic, strong) NSArray* willReturnVCs;
  12. @end
  13. @implementation MockUINavigationController
  14. -(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
  15. return self.willReturnVCs;
  16. }
  17. -(NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
  18. return self.willReturnVCs;
  19. }
  20. @end
  21. @interface RNNCommandsHandlerTest : XCTestCase
  22. @property (nonatomic, strong) RNNStore* store;
  23. @property (nonatomic, strong) id overlayStore;
  24. @property (nonatomic, strong) RNNCommandsHandler* uut;
  25. @property (nonatomic, strong) RNNRootViewController* vc1;
  26. @property (nonatomic, strong) RNNRootViewController* vc2;
  27. @property (nonatomic, strong) RNNRootViewController* vc3;
  28. @property (nonatomic, strong) MockUINavigationController* nvc;
  29. @property (nonatomic, strong) id controllerFactory;
  30. @property (nonatomic, strong) id overlayManager;
  31. @property (nonatomic, strong) id eventEmmiter;
  32. @end
  33. @implementation RNNCommandsHandlerTest
  34. - (void)setUp {
  35. [super setUp];
  36. // [self.store setReadyToReceiveCommands:true];
  37. self.store = [[RNNStore alloc] init];
  38. self.overlayStore = [OCMockObject partialMockForObject:[[RNNStore alloc] init]];
  39. self.eventEmmiter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  40. self.overlayManager = [OCMockObject partialMockForObject:[RNNOverlayManager new]];
  41. self.controllerFactory = [OCMockObject partialMockForObject:[[RNNControllerFactory alloc] initWithRootViewCreator:nil eventEmitter:self.eventEmmiter andBridge:nil]];
  42. self.uut = [[RNNCommandsHandler alloc] initWithStore:self.store overlayStore:self.overlayStore controllerFactory:self.controllerFactory eventEmitter:self.eventEmmiter stackManager:[RNNNavigationStackManager new] modalManager:[RNNModalManager new] overlayManager:self.overlayManager];
  43. self.vc1 = [RNNRootViewController new];
  44. self.vc2 = [RNNRootViewController new];
  45. self.vc3 = [RNNRootViewController new];
  46. _nvc = [[MockUINavigationController alloc] init];
  47. [_nvc setViewControllers:@[self.vc1, self.vc2, self.vc3]];
  48. [self.store setComponent:self.vc1 componentId:@"vc1"];
  49. [self.store setComponent:self.vc2 componentId:@"vc2"];
  50. [self.store setComponent:self.vc3 componentId:@"vc3"];
  51. }
  52. - (void)testAssertReadyForEachMethodThrowsExceptoins {
  53. NSArray* methods = [self getPublicMethodNamesForObject:self.uut];
  54. [self.store setReadyToReceiveCommands:false];
  55. for (NSString* methodName in methods) {
  56. SEL s = NSSelectorFromString(methodName);
  57. IMP imp = [self.uut methodForSelector:s];
  58. void (*func)(id, SEL, id, id, id) = (void *)imp;
  59. XCTAssertThrowsSpecificNamed(func(self.uut,s, nil, nil, nil), NSException, @"BridgeNotLoadedError");
  60. }
  61. }
  62. -(NSArray*) getPublicMethodNamesForObject:(NSObject*)obj{
  63. NSMutableArray* skipMethods = [NSMutableArray new];
  64. [skipMethods addObject:@"initWithStore:overlayStore:controllerFactory:eventEmitter:stackManager:modalManager:overlayManager:"];
  65. [skipMethods addObject:@"assertReady"];
  66. [skipMethods addObject:@"removePopedViewControllers:"];
  67. [skipMethods addObject:@".cxx_destruct"];
  68. [skipMethods addObject:@"dismissedModal:"];
  69. [skipMethods addObject:@"dismissedMultipleModals:"];
  70. NSMutableArray* result = [NSMutableArray new];
  71. // count and names:
  72. int i=0;
  73. unsigned int mc = 0;
  74. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  75. for(i=0; i<mc; i++) {
  76. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  77. // filter skippedMethods
  78. if (methodName && ![skipMethods containsObject:methodName]) {
  79. [result addObject:methodName];
  80. }
  81. }
  82. return result;
  83. }
  84. -(void)testDynamicStylesMergeWithStaticStyles {
  85. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  86. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  87. RNNLayoutInfo* layoutInfo = [RNNLayoutInfo new];
  88. RNNTestRootViewCreator* creator = [[RNNTestRootViewCreator alloc] init];
  89. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  90. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:creator eventEmitter:nil presenter:presenter options:initialOptions];
  91. RNNNavigationController* nav = [[RNNNavigationController alloc] initWithLayoutInfo:nil childViewControllers:@[vc] options:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[[RNNNavigationControllerPresenter alloc] init]];
  92. [vc viewWillAppear:false];
  93. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  94. [self.store setReadyToReceiveCommands:true];
  95. [self.store setComponent:vc componentId:@"componentId"];
  96. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  97. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  98. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  99. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  100. XCTAssertTrue([nav.navigationBar.barTintColor isEqual:expectedColor]);
  101. }];
  102. }
  103. - (void)testMergeOptions_shouldOverrideOptions {
  104. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  105. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  106. RNNViewControllerPresenter* presenter = [[RNNViewControllerPresenter alloc] init];
  107. RNNRootViewController* vc = [[RNNRootViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions];
  108. __unused RNNNavigationController* nav = [[RNNNavigationController alloc] initWithRootViewController:vc];
  109. [vc viewWillAppear:false];
  110. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  111. [self.store setReadyToReceiveCommands:true];
  112. [self.store setComponent:vc componentId:@"componentId"];
  113. NSDictionary* dictFromJs = @{@"topBar": @{@"title" : @{@"text" : @"new title"}}};
  114. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  115. XCTAssertTrue([vc.navigationItem.title isEqual:@"new title"]);
  116. }];
  117. }
  118. - (void)testPop_removeTopVCFromStore {
  119. [self.store setReadyToReceiveCommands:true];
  120. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  121. [self.uut pop:@"vc3" mergeOptions:nil completion:^{
  122. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  123. XCTAssertNotNil([self.store findComponentForId:@"vc2"]);
  124. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  125. [expectation fulfill];
  126. } rejection:^(NSString *code, NSString *message, NSError *error) {
  127. }];
  128. [self waitForExpectationsWithTimeout:1 handler:nil];
  129. }
  130. - (void)testPopToSpecificVC_removeAllPopedVCFromStore {
  131. [self.store setReadyToReceiveCommands:true];
  132. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  133. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  134. [self.uut popTo:@"vc1" mergeOptions:nil completion:^{
  135. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  136. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  137. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  138. [expectation fulfill];
  139. } rejection:nil];
  140. [self waitForExpectationsWithTimeout:1 handler:nil];
  141. }
  142. - (void)testPopToRoot_removeAllTopVCsFromStore {
  143. [self.store setReadyToReceiveCommands:true];
  144. _nvc.willReturnVCs = @[self.vc2, self.vc3];
  145. XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method"];
  146. [self.uut popToRoot:@"vc3" mergeOptions:nil completion:^{
  147. XCTAssertNil([self.store findComponentForId:@"vc2"]);
  148. XCTAssertNil([self.store findComponentForId:@"vc3"]);
  149. XCTAssertNotNil([self.store findComponentForId:@"vc1"]);
  150. [expectation fulfill];
  151. } rejection:nil];
  152. [self waitForExpectationsWithTimeout:1 handler:nil];
  153. }
  154. - (void)testShowOverlay_createLayout {
  155. [self.store setReadyToReceiveCommands:true];
  156. OCMStub([self.overlayManager showOverlay:[OCMArg any]]);
  157. NSDictionary* layout = @{};
  158. [[self.controllerFactory expect] createLayout:layout saveToStore:self.overlayStore];
  159. [self.uut showOverlay:layout completion:^{}];
  160. [self.controllerFactory verify];
  161. }
  162. - (void)testShowOverlay_saveToOverlayStore {
  163. [self.store setReadyToReceiveCommands:true];
  164. OCMStub([self.overlayManager showOverlay:[OCMArg any]]);
  165. OCMStub([self.controllerFactory createLayout:[OCMArg any] saveToStore:[OCMArg any]]);
  166. [[self.controllerFactory expect] createLayout:[OCMArg any] saveToStore:self.overlayStore];
  167. [self.uut showOverlay:@{} completion:^{}];
  168. [self.overlayManager verify];
  169. }
  170. - (void)testShowOverlay_withCreatedLayout {
  171. [self.store setReadyToReceiveCommands:true];
  172. UIViewController* layoutVC = [RNNRootViewController new];
  173. OCMStub([self.controllerFactory createLayout:[OCMArg any] saveToStore:[OCMArg any]]).andReturn(layoutVC);
  174. [[self.overlayManager expect] showOverlay:layoutVC];
  175. [self.uut showOverlay:@{} completion:^{}];
  176. [self.overlayManager verify];
  177. }
  178. - (void)testShowOverlay_invokeNavigationCommandEventWithLayout {
  179. [self.store setReadyToReceiveCommands:true];
  180. OCMStub([self.overlayManager showOverlay:[OCMArg any]]);
  181. OCMStub([self.controllerFactory createLayout:[OCMArg any] saveToStore:[OCMArg any]]);
  182. NSDictionary* layout = @{};
  183. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"showOverlay" params:[OCMArg any]];
  184. [self.uut showOverlay:layout completion:^{}];
  185. [self.eventEmmiter verify];
  186. }
  187. - (void)testDismissOverlay_findComponentFromOverlayStore {
  188. [self.store setReadyToReceiveCommands:true];
  189. NSString* componentId = @"componentId";
  190. [[self.overlayStore expect] findComponentForId:componentId];
  191. [self.uut dismissOverlay:componentId completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  192. [self.overlayStore verify];
  193. }
  194. - (void)testDismissOverlay_dismissReturnedViewController {
  195. [self.store setReadyToReceiveCommands:true];
  196. NSString* componentId = @"componentId";
  197. UIViewController* returnedView = [UIViewController new];
  198. OCMStub([self.overlayStore findComponentForId:componentId]).andReturn(returnedView);
  199. [[self.overlayManager expect] dismissOverlay:returnedView];
  200. [self.uut dismissOverlay:componentId completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  201. [self.overlayManager verify];
  202. }
  203. - (void)testDismissOverlay_handleErrorIfNoOverlayExists {
  204. [self.store setReadyToReceiveCommands:true];
  205. NSString* componentId = @"componentId";
  206. id errorHandlerMockClass = [OCMockObject mockForClass:[RNNErrorHandler class]];
  207. [[errorHandlerMockClass expect] reject:[OCMArg any] withErrorCode:1010 errorDescription:[OCMArg any]];
  208. [self.uut dismissOverlay:componentId completion:[OCMArg any] rejection:[OCMArg any]];
  209. [errorHandlerMockClass verify];
  210. }
  211. - (void)testDismissOverlay_invokeNavigationCommandEvent {
  212. [self.store setReadyToReceiveCommands:true];
  213. NSString* componentId = @"componentId";
  214. OCMStub([self.overlayStore findComponentForId:componentId]).andReturn([UIViewController new]);
  215. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"dismissOverlay" params:[OCMArg any]];
  216. [self.uut dismissOverlay:componentId completion:^{
  217. } rejection:^(NSString *code, NSString *message, NSError *error) {}];
  218. [self.eventEmmiter verify];
  219. }
  220. @end