react-native-navigation的迁移库

RNNCommandsHandlerTest.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 store:self.store componentRegistry:nil 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, id, id) = (void *)imp;
  68. XCTAssertThrowsSpecificNamed(func(self.uut,s, nil, nil, 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 creator:creator options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:[[RNNNavigationControllerPresenter alloc] init] eventEmitter:nil];
  101. [nav setViewControllers:@[vc]];
  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)testShowOverlay_createLayout {
  129. [self.store setReadyToReceiveCommands:true];
  130. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  131. NSDictionary* layout = @{};
  132. [[self.controllerFactory expect] createLayout:layout];
  133. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  134. [self.controllerFactory verify];
  135. }
  136. - (void)testShowOverlay_saveToStore {
  137. [self.store setReadyToReceiveCommands:true];
  138. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  139. OCMStub([self.controllerFactory createLayout:[OCMArg any]]);
  140. [[self.controllerFactory expect] createLayout:[OCMArg any]];
  141. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  142. [self.overlayManager verify];
  143. }
  144. - (void)testShowOverlay_withCreatedLayout {
  145. [self.store setReadyToReceiveCommands:true];
  146. UIViewController* layoutVC = [RNNRootViewController new];
  147. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(layoutVC);
  148. [[self.overlayManager expect] showOverlayWindow:[OCMArg any]];
  149. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  150. [self.overlayManager verify];
  151. }
  152. - (void)testShowOverlay_invokeNavigationCommandEventWithLayout {
  153. [self.store setReadyToReceiveCommands:true];
  154. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  155. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  156. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  157. NSDictionary* layout = @{};
  158. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"showOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  159. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  160. [self.eventEmmiter verify];
  161. }
  162. - (void)testDismissOverlay_findComponentFromStore {
  163. [self.store setReadyToReceiveCommands:true];
  164. NSString* componentId = @"componentId";
  165. [[self.store expect] findComponentForId:componentId];
  166. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  167. [self.store verify];
  168. }
  169. - (void)testDismissOverlay_dismissReturnedViewController {
  170. [self.store setReadyToReceiveCommands:true];
  171. NSString* componentId = @"componentId";
  172. UIViewController* returnedView = [UIViewController new];
  173. OCMStub([self.store findComponentForId:componentId]).andReturn(returnedView);
  174. [[self.overlayManager expect] dismissOverlay:returnedView];
  175. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  176. [self.overlayManager verify];
  177. }
  178. - (void)testDismissOverlay_handleErrorIfNoOverlayExists {
  179. [self.store setReadyToReceiveCommands:true];
  180. NSString* componentId = @"componentId";
  181. id errorHandlerMockClass = [OCMockObject mockForClass:[RNNErrorHandler class]];
  182. [[errorHandlerMockClass expect] reject:[OCMArg any] withErrorCode:1010 errorDescription:[OCMArg any]];
  183. [self.uut dismissOverlay:componentId commandId:@"" completion:[OCMArg any] rejection:[OCMArg any]];
  184. [errorHandlerMockClass verify];
  185. }
  186. - (void)testDismissOverlay_invokeNavigationCommandEvent {
  187. [self.store setReadyToReceiveCommands:true];
  188. NSString* componentId = @"componentId";
  189. OCMStub([self.store findComponentForId:componentId]).andReturn([UIViewController new]);
  190. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"dismissOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  191. [self.uut dismissOverlay:componentId commandId:@"" completion:^{
  192. } rejection:^(NSString *code, NSString *message, NSError *error) {}];
  193. [self.eventEmmiter verify];
  194. }
  195. - (void)testSetRoot_setRootViewControllerOnMainWindow {
  196. [self.store setReadyToReceiveCommands:true];
  197. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(self.vc1);
  198. [[self.mainWindow expect] setRootViewController:self.vc1];
  199. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  200. [self.mainWindow verify];
  201. }
  202. - (void)testSetStackRoot_resetStackWithSingleComponent {
  203. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(@[self.vc2]);
  204. [self.store setReadyToReceiveCommands:true];
  205. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  206. } rejection:^(NSString *code, NSString *message, NSError *error) {
  207. }];
  208. XCTAssertEqual(_nvc.viewControllers.firstObject, self.vc2);
  209. XCTAssertEqual(_nvc.viewControllers.count, 1);
  210. }
  211. - (void)testSetStackRoot_setMultipleChildren {
  212. NSArray* newViewControllers = @[_vc1, _vc3];
  213. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  214. [self.store setReadyToReceiveCommands:true];
  215. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  216. } rejection:^(NSString *code, NSString *message, NSError *error) {
  217. }];
  218. XCTAssertTrue([_nvc.viewControllers isEqual:newViewControllers]);
  219. }
  220. - (void)testSetRoot_waitForRenderTrue {
  221. [self.store setReadyToReceiveCommands:true];
  222. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  223. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  224. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  225. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  226. [[mockedVC expect] renderTreeAndWait:YES perform:[OCMArg any]];
  227. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  228. [mockedVC verify];
  229. }
  230. - (void)testSetRoot_waitForRenderFalse {
  231. [self.store setReadyToReceiveCommands:true];
  232. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  233. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:NO];
  234. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  235. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  236. [[mockedVC expect] renderTreeAndWait:NO perform:[OCMArg any]];
  237. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  238. [mockedVC verify];
  239. }
  240. @end