react-native-navigation的迁移库

RNNCommandsHandlerTest.m 12KB

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