react-native-navigation的迁移库

RNNCommandsHandlerTest.m 11KB

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