react-native-navigation的迁移库

RNNCommandsHandlerTest.m 12KB

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