react-native-navigation的迁移库

RNNCommandsHandlerTest.m 12KB

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