react-native-navigation的迁移库

RNNCommandsHandlerTest.m 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. #import <XCTest/XCTest.h>
  2. #import <objc/runtime.h>
  3. #import <ReactNativeNavigation/RNNCommandsHandler.h>
  4. #import <ReactNativeNavigation/RNNNavigationOptions.h>
  5. #import "RNNTestRootViewCreator.h"
  6. #import <ReactNativeNavigation/RNNComponentViewController.h>
  7. #import <ReactNativeNavigation/RNNStackController.h>
  8. #import <ReactNativeNavigation/RNNErrorHandler.h>
  9. #import <OCMock/OCMock.h>
  10. #import "RNNLayoutManager.h"
  11. #import "RNNBottomTabsController.h"
  12. #import "BottomTabsAttachModeFactory.h"
  13. #import <ReactNativeNavigation/BottomTabPresenterCreator.h>
  14. #import "RNNComponentViewController+Utils.h"
  15. @interface MockUINavigationController : RNNStackController
  16. @property (nonatomic, strong) NSArray* willReturnVCs;
  17. @end
  18. @implementation MockUINavigationController
  19. -(NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
  20. return self.willReturnVCs;
  21. }
  22. -(NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated {
  23. return self.willReturnVCs;
  24. }
  25. @end
  26. @interface RNNCommandsHandlerTest : XCTestCase
  27. @property (nonatomic, strong) RNNCommandsHandler* uut;
  28. @property (nonatomic, strong) id modalManager;
  29. @property (nonatomic, strong) RNNComponentViewController* vc1;
  30. @property (nonatomic, strong) RNNComponentViewController* vc2;
  31. @property (nonatomic, strong) RNNComponentViewController* vc3;
  32. @property (nonatomic, strong) MockUINavigationController* nvc;
  33. @property (nonatomic, strong) id mainWindow;
  34. @property (nonatomic, strong) id sharedApplication;
  35. @property (nonatomic, strong) id controllerFactory;
  36. @property (nonatomic, strong) id overlayManager;
  37. @property (nonatomic, strong) id eventEmmiter;
  38. @property (nonatomic, strong) id creator;
  39. @end
  40. @implementation RNNCommandsHandlerTest
  41. - (void)setUp {
  42. [super setUp];
  43. self.creator = [OCMockObject partialMockForObject:[RNNTestRootViewCreator new]];
  44. self.mainWindow = [OCMockObject partialMockForObject:[UIWindow new]];
  45. self.eventEmmiter = [OCMockObject partialMockForObject:[RNNEventEmitter new]];
  46. self.overlayManager = [OCMockObject partialMockForObject:[RNNOverlayManager new]];
  47. self.modalManager = [OCMockObject partialMockForObject:[RNNModalManager new]];
  48. self.controllerFactory = [OCMockObject partialMockForObject:[[RNNControllerFactory alloc] initWithRootViewCreator:nil eventEmitter:self.eventEmmiter store:nil componentRegistry:nil andBridge:nil bottomTabsAttachModeFactory:[BottomTabsAttachModeFactory new]]];
  49. self.uut = [[RNNCommandsHandler alloc] initWithControllerFactory:self.controllerFactory eventEmitter:self.eventEmmiter modalManager:self.modalManager overlayManager:self.overlayManager mainWindow:_mainWindow];
  50. self.vc1 = [self generateComponentWithComponentId:@"1"];
  51. self.vc2 = [self generateComponentWithComponentId:@"2"];
  52. self.vc3 = [self generateComponentWithComponentId:@"3"];
  53. _nvc = [[MockUINavigationController alloc] init];
  54. [_nvc setViewControllers:@[self.vc1, self.vc2, self.vc3]];
  55. UIApplication* sharedApplication = [OCMockObject niceMockForClass:[UIApplication class]];
  56. id mockedApplicationClass = OCMClassMock([UIApplication class]);
  57. OCMStub(ClassMethod([mockedApplicationClass sharedApplication])).andReturn(sharedApplication);
  58. OCMStub(sharedApplication.keyWindow).andReturn(self.mainWindow);
  59. OCMStub([sharedApplication windows]).andReturn(@[self.mainWindow]);
  60. }
  61. - (RNNComponentViewController *)generateComponentWithComponentId:(NSString *)componentId {
  62. RNNLayoutInfo* layoutInfo = [[RNNLayoutInfo alloc] init];
  63. layoutInfo.componentId = componentId;
  64. return [[RNNComponentViewController alloc] initWithLayoutInfo:layoutInfo rootViewCreator:_creator eventEmitter:nil presenter:[RNNComponentPresenter new] options:[[RNNNavigationOptions alloc] initWithDict:@{}] defaultOptions:nil];
  65. }
  66. - (void)testAssertReadyForEachMethodThrowsExceptions {
  67. NSArray* methods = [self getPublicMethodNamesForObject:self.uut];
  68. [self.uut setReadyToReceiveCommands:false];
  69. for (NSString* methodName in methods) {
  70. SEL s = NSSelectorFromString(methodName);
  71. IMP imp = [self.uut methodForSelector:s];
  72. void (*func)(id, SEL, id, id, id, id, id) = (void *)imp;
  73. XCTAssertThrowsSpecificNamed(func(self.uut,s, nil, nil, nil, nil, nil), NSException, @"BridgeNotLoadedError");
  74. }
  75. }
  76. - (NSArray*)getPublicMethodNamesForObject:(NSObject*)obj {
  77. NSMutableArray* skipMethods = [NSMutableArray new];
  78. [skipMethods addObject:@"initWithControllerFactory:eventEmitter:modalManager:overlayManager:mainWindow:"];
  79. [skipMethods addObject:@"assertReady"];
  80. [skipMethods addObject:@"setReadyToReceiveCommands:"];
  81. [skipMethods addObject:@"readyToReceiveCommands"];
  82. [skipMethods addObject:@".cxx_destruct"];
  83. [skipMethods addObject:@"dismissedModal:"];
  84. [skipMethods addObject:@"attemptedToDismissModal:"];
  85. [skipMethods addObject:@"dismissedMultipleModals:"];
  86. NSMutableArray* result = [NSMutableArray new];
  87. // count and names:
  88. int i=0;
  89. unsigned int mc = 0;
  90. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  91. for(i=0; i<mc; i++) {
  92. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  93. // filter skippedMethods
  94. if (methodName && ![skipMethods containsObject:methodName]) {
  95. [result addObject:methodName];
  96. }
  97. }
  98. return result;
  99. }
  100. -(void)testDynamicStylesMergeWithStaticStyles {
  101. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  102. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  103. RNNTestRootViewCreator* creator = [[RNNTestRootViewCreator alloc] init];
  104. RNNComponentViewController* vc = [RNNComponentViewController createWithComponentId:@"componentId" initialOptions:initialOptions];
  105. RNNStackController* nav = [[RNNStackController alloc] initWithLayoutInfo:nil creator:creator options:[[RNNNavigationOptions alloc] initEmptyOptions] defaultOptions:nil presenter:[[RNNStackPresenter alloc] init] eventEmitter:nil childViewControllers:@[vc]];
  106. [self.mainWindow setRootViewController:nav];
  107. [vc viewWillAppear:false];
  108. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  109. [self.uut setReadyToReceiveCommands:true];
  110. NSDictionary* dictFromJs = @{@"topBar": @{@"background" : @{@"color" : @(0xFFFF0000)}}};
  111. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  112. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  113. }];
  114. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  115. XCTAssertTrue([vc.navigationItem.standardAppearance.backgroundColor isEqual:expectedColor]);
  116. }
  117. - (void)testMergeOptions_shouldOverrideOptions {
  118. RNNNavigationOptions* initialOptions = [[RNNNavigationOptions alloc] initWithDict:@{}];
  119. initialOptions.topBar.title.text = [[Text alloc] initWithValue:@"the title"];
  120. RNNComponentPresenter* presenter = [[RNNComponentPresenter alloc] initWithComponentRegistry:nil defaultOptions:nil];
  121. RNNComponentViewController* vc = [[RNNComponentViewController alloc] initWithLayoutInfo:nil rootViewCreator:[[RNNTestRootViewCreator alloc] init] eventEmitter:nil presenter:presenter options:initialOptions defaultOptions:nil];
  122. __unused RNNStackController* nav = [[RNNStackController alloc] initWithRootViewController:vc];
  123. [vc viewWillAppear:false];
  124. XCTAssertTrue([vc.navigationItem.title isEqual:@"the title"]);
  125. [self.uut setReadyToReceiveCommands:true];
  126. NSDictionary* dictFromJs = @{@"topBar": @{@"title" : @{@"text" : @"new title"}}};
  127. [self.uut mergeOptions:@"componentId" options:dictFromJs completion:^{
  128. XCTAssertTrue([vc.navigationItem.title isEqual:@"new title"]);
  129. }];
  130. }
  131. - (void)testShowOverlay_createLayout {
  132. [self.uut setReadyToReceiveCommands:true];
  133. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  134. NSDictionary* layout = @{};
  135. [[self.controllerFactory expect] createLayout:layout];
  136. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  137. [self.controllerFactory verify];
  138. }
  139. - (void)testShowOverlay_saveToStore {
  140. [self.uut setReadyToReceiveCommands:true];
  141. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  142. OCMStub([self.controllerFactory createLayout:[OCMArg any]]);
  143. [[self.controllerFactory expect] createLayout:[OCMArg any]];
  144. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  145. [self.overlayManager verify];
  146. }
  147. - (void)testShowOverlay_withCreatedLayout {
  148. [self.uut setReadyToReceiveCommands:true];
  149. UIViewController* layoutVC = [self generateComponentWithComponentId:nil];
  150. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(layoutVC);
  151. [[self.overlayManager expect] showOverlayWindow:[OCMArg any]];
  152. [self.uut showOverlay:@{} commandId:@"" completion:^{}];
  153. [self.overlayManager verify];
  154. }
  155. - (void)testShowOverlay_invokeNavigationCommandEventWithLayout {
  156. [self.uut setReadyToReceiveCommands:true];
  157. OCMStub([self.overlayManager showOverlayWindow:[OCMArg any]]);
  158. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  159. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  160. NSDictionary* layout = @{};
  161. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"showOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  162. [self.uut showOverlay:layout commandId:@"" completion:^{}];
  163. [self.eventEmmiter verify];
  164. }
  165. - (void)testDismissOverlay_findComponentFromLayoutManager {
  166. [self.uut setReadyToReceiveCommands:true];
  167. NSString* componentId = @"componentId";
  168. id classMock = OCMClassMock([RNNLayoutManager class]);
  169. [[classMock expect] findComponentForId:componentId];
  170. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  171. [classMock verify];
  172. }
  173. - (void)testDismissOverlay_dismissReturnedViewController {
  174. [self.uut setReadyToReceiveCommands:true];
  175. NSString* componentId = @"componentId";
  176. UIViewController* returnedView = [UIViewController new];
  177. id classMock = OCMClassMock([RNNLayoutManager class]);
  178. OCMStub(ClassMethod([classMock findComponentForId:componentId])).andReturn(returnedView);
  179. [[self.overlayManager expect] dismissOverlay:returnedView];
  180. [self.uut dismissOverlay:componentId commandId:@"" completion:^{} rejection:^(NSString *code, NSString *message, NSError *error) {}];
  181. [self.overlayManager verify];
  182. }
  183. - (void)testDismissOverlay_handleErrorIfNoOverlayExists {
  184. [self.uut setReadyToReceiveCommands:true];
  185. NSString* componentId = @"componentId";
  186. id errorHandlerMockClass = [OCMockObject mockForClass:[RNNErrorHandler class]];
  187. [[errorHandlerMockClass expect] reject:[OCMArg any] withErrorCode:1010 errorDescription:[OCMArg any]];
  188. [self.uut dismissOverlay:componentId commandId:@"" completion:[OCMArg any] rejection:[OCMArg any]];
  189. [errorHandlerMockClass verify];
  190. }
  191. - (void)testDismissOverlay_invokeNavigationCommandEvent {
  192. [self.uut setReadyToReceiveCommands:true];
  193. NSString* componentId = @"componentId";
  194. id classMock = OCMClassMock([RNNLayoutManager class]);
  195. OCMStub(ClassMethod([classMock findComponentForId:componentId])).andReturn([UIViewController new]);
  196. [[self.eventEmmiter expect] sendOnNavigationCommandCompletion:@"dismissOverlay" commandId:[OCMArg any] params:[OCMArg any]];
  197. [self.uut dismissOverlay:componentId commandId:@"" completion:^{
  198. } rejection:^(NSString *code, NSString *message, NSError *error) {}];
  199. [self.eventEmmiter verify];
  200. }
  201. - (void)testSetRoot_setRootViewControllerOnMainWindow {
  202. [self.uut setReadyToReceiveCommands:true];
  203. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(self.vc1);
  204. [[self.mainWindow expect] setRootViewController:self.vc1];
  205. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  206. [self.mainWindow verify];
  207. }
  208. - (void)testSetStackRoot_resetStackWithSingleComponent {
  209. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(@[self.vc2]);
  210. [self.uut setReadyToReceiveCommands:true];
  211. id classMock = OCMClassMock([RNNLayoutManager class]);
  212. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  213. self.vc2.options.animations.setStackRoot.enable = [[Bool alloc] initWithBOOL:NO];
  214. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  215. } rejection:^(NSString *code, NSString *message, NSError *error) {
  216. }];
  217. XCTAssertEqual(_nvc.viewControllers.firstObject, self.vc2);
  218. XCTAssertEqual(_nvc.viewControllers.count, 1);
  219. }
  220. - (void)testSetStackRoot_setMultipleChildren {
  221. NSArray* newViewControllers = @[_vc1, _vc3];
  222. id classMock = OCMClassMock([RNNLayoutManager class]);
  223. OCMStub(ClassMethod([classMock findComponentForId:@"vc1"])).andReturn(_nvc);
  224. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  225. [self.uut setReadyToReceiveCommands:true];
  226. _vc3.options.animations.setStackRoot.enable = [[Bool alloc] initWithBOOL:NO];
  227. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  228. } rejection:^(NSString *code, NSString *message, NSError *error) {
  229. }];
  230. XCTAssertTrue([_nvc.viewControllers isEqual:newViewControllers]);
  231. }
  232. - (void)testSetStackRoot_callRenderTreeAndWaitOnce {
  233. id vc1Mock = [OCMockObject partialMockForObject:_vc1];
  234. id vc2Mock = [OCMockObject partialMockForObject:_vc2];
  235. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  236. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  237. [self.uut setReadyToReceiveCommands:true];
  238. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  239. } rejection:^(NSString *code, NSString *message, NSError *error) {
  240. }];
  241. [[vc1Mock expect] render];
  242. [[vc2Mock expect] render];
  243. }
  244. - (void)testSetStackRoot_waitForRender {
  245. _vc2.options.animations.setStackRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  246. id vc1Mock = OCMPartialMock(_vc1);
  247. id vc2Mock = OCMPartialMock(_vc2);
  248. NSArray* newViewControllers = @[vc1Mock, vc2Mock];
  249. OCMStub([self.controllerFactory createChildrenLayout:[OCMArg any]]).andReturn(newViewControllers);
  250. [self.uut setReadyToReceiveCommands:true];
  251. [self.uut setStackRoot:@"vc1" commandId:@"" children:nil completion:^{
  252. } rejection:^(NSString *code, NSString *message, NSError *error) {
  253. }];
  254. [[vc1Mock expect] render];
  255. [[vc2Mock expect] render];
  256. }
  257. - (void)testSetRoot_waitForRenderTrue {
  258. [self.uut setReadyToReceiveCommands:true];
  259. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  260. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  261. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  262. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  263. [[mockedVC expect] render];
  264. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  265. [mockedVC verify];
  266. }
  267. - (void)testSetRoot_waitForRenderFalse {
  268. [self.uut setReadyToReceiveCommands:true];
  269. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  270. self.vc1.options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:NO];
  271. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  272. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  273. [[mockedVC expect] render];
  274. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  275. [mockedVC verify];
  276. }
  277. - (void)testSetRoot_withBottomTabsAttachModeTogether {
  278. [self.uut setReadyToReceiveCommands:true];
  279. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  280. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"together"];
  281. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  282. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] bottomTabPresenter:nil dotIndicatorPresenter:nil eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  283. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  284. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  285. XCTAssertTrue(_vc1.isViewLoaded);
  286. XCTAssertTrue(_vc2.isViewLoaded);
  287. XCTAssertEqual(_vc1.view.tag, 1);
  288. XCTAssertEqual(_vc2.view.tag, 2);
  289. }
  290. - (void)testSetRoot_withBottomTabsAttachModeOnSwitchToTab {
  291. [self.uut setReadyToReceiveCommands:true];
  292. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  293. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"onSwitchToTab"];
  294. options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  295. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  296. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] bottomTabPresenter:nil dotIndicatorPresenter:nil eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  297. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  298. [self.uut setRoot:@{} commandId:@"" completion:^{}];
  299. XCTAssertTrue(_vc1.isViewLoaded);
  300. XCTAssertFalse(_vc2.isViewLoaded);
  301. [tabBarController setSelectedIndex:1];
  302. XCTAssertTrue(_vc2.isViewLoaded);
  303. }
  304. - (void)testSetRoot_withBottomTabsAttachModeAfterInitialTab {
  305. [self.uut setReadyToReceiveCommands:true];
  306. RNNNavigationOptions* options = [[RNNNavigationOptions alloc] initEmptyOptions];
  307. options.bottomTabs.tabsAttachMode = [[BottomTabsAttachMode alloc] initWithValue:@"afterInitialTab"];
  308. options.animations.setRoot.waitForRender = [[Bool alloc] initWithBOOL:YES];
  309. BottomTabsBaseAttacher* attacher = [[[BottomTabsAttachModeFactory alloc] initWithDefaultOptions:nil] fromOptions:options];
  310. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:options defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] bottomTabPresenter:nil dotIndicatorPresenter:nil eventEmitter:_eventEmmiter childViewControllers:@[_vc1, _vc2] bottomTabsAttacher:attacher];
  311. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  312. [self.uut setRoot:@{} commandId:@"" completion:^{
  313. XCTAssertFalse(self->_vc2.isViewLoaded);
  314. }];
  315. XCTAssertTrue(_vc1.isViewLoaded);
  316. XCTAssertTrue(_vc2.isViewLoaded);
  317. }
  318. - (void)testMergeOptions_shouldMergeWithChildOnly {
  319. [self.uut setReadyToReceiveCommands:true];
  320. NSDictionary* mergeOptions = @{@"bottomTab": @{@"badge": @"Badge"}};
  321. RNNNavigationOptions* firstChildOptions = [RNNNavigationOptions emptyOptions];
  322. firstChildOptions.bottomTab.text = [Text withValue:@"First"];
  323. RNNNavigationOptions* secondChildOptions = [RNNNavigationOptions emptyOptions];
  324. secondChildOptions.bottomTab.text = [Text withValue:@"Second"];
  325. RNNComponentViewController* firstChild = [RNNComponentViewController createWithComponentId:@"first" initialOptions:firstChildOptions];
  326. RNNComponentViewController* secondChild = [RNNComponentViewController createWithComponentId:@"second" initialOptions:secondChildOptions];
  327. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[RNNNavigationOptions emptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:[RNNNavigationOptions emptyOptions]] dotIndicatorPresenter:nil eventEmitter:_eventEmmiter childViewControllers:@[firstChild, secondChild] bottomTabsAttacher:nil];
  328. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  329. [self.mainWindow setRootViewController:tabBarController];
  330. [secondChild viewWillAppear:YES];
  331. [self.uut mergeOptions:secondChild.layoutInfo.componentId options:mergeOptions completion:^{
  332. }];
  333. XCTAssertTrue([secondChild.tabBarItem.badgeValue isEqualToString:@"Badge"]);
  334. XCTAssertNil(firstChild.tabBarItem.badgeValue);
  335. XCTAssertTrue([firstChild.tabBarItem.title isEqualToString:@"First"]);
  336. XCTAssertTrue([secondChild.tabBarItem.title isEqualToString:@"Second"]);
  337. }
  338. - (void)testMergeOptions_shouldResolveTreeOptions {
  339. [self.uut setReadyToReceiveCommands:true];
  340. NSDictionary* mergeOptions = @{@"bottomTab": @{@"badge": @"Badge"}};
  341. RNNNavigationOptions* firstChildOptions = [RNNNavigationOptions emptyOptions];
  342. RNNNavigationOptions* secondChildOptions = [RNNNavigationOptions emptyOptions];
  343. secondChildOptions.bottomTab.text = [Text withValue:@"Second"];
  344. RNNNavigationOptions* stackOptions = [RNNNavigationOptions emptyOptions];
  345. stackOptions.bottomTab.text = [Text withValue:@"First"];
  346. RNNComponentViewController* firstChild = [RNNComponentViewController createWithComponentId:@"first" initialOptions:firstChildOptions];
  347. RNNStackController* stack = [[RNNStackController alloc] initWithLayoutInfo:nil creator:nil options:stackOptions defaultOptions:nil presenter:nil eventEmitter:nil childViewControllers:@[firstChild]];
  348. RNNComponentViewController* secondChild = [RNNComponentViewController createWithComponentId:@"second" initialOptions:secondChildOptions];
  349. RNNBottomTabsController* tabBarController = [[RNNBottomTabsController alloc] initWithLayoutInfo:nil creator:nil options:[RNNNavigationOptions emptyOptions] defaultOptions:[[RNNNavigationOptions alloc] initEmptyOptions] presenter:[RNNBasePresenter new] bottomTabPresenter:[BottomTabPresenterCreator createWithDefaultOptions:[RNNNavigationOptions emptyOptions]] dotIndicatorPresenter:nil eventEmitter:_eventEmmiter childViewControllers:@[stack, secondChild] bottomTabsAttacher:nil];
  350. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(tabBarController);
  351. [self.mainWindow setRootViewController:tabBarController];
  352. [secondChild viewWillAppear:YES];
  353. [self.uut mergeOptions:firstChild.layoutInfo.componentId options:mergeOptions completion:^{
  354. }];
  355. XCTAssertTrue([stack.tabBarItem.badgeValue isEqualToString:@"Badge"]);
  356. XCTAssertTrue([stack.tabBarItem.title isEqualToString:@"First"]);
  357. XCTAssertTrue([secondChild.tabBarItem.title isEqualToString:@"Second"]);
  358. }
  359. - (void)testShowModal_shouldShowAnimated {
  360. [self.uut setReadyToReceiveCommands:true];
  361. self.vc1.options = [[RNNNavigationOptions alloc] initEmptyOptions];
  362. self.vc1.options.animations.showModal.enable = [[Bool alloc] initWithBOOL:YES];
  363. id mockedVC = [OCMockObject partialMockForObject:self.vc1];
  364. OCMStub([self.controllerFactory createLayout:[OCMArg any]]).andReturn(mockedVC);
  365. [[self.modalManager expect] showModal:mockedVC animated:YES completion:[OCMArg any]];
  366. [self.uut showModal:@{} commandId:@"showModal" completion:^(NSString *componentId) {
  367. }];
  368. [self.modalManager verify];
  369. }
  370. - (void)testPop_shouldRejectPromiseForInvalidComponentId {
  371. [self.uut setReadyToReceiveCommands:true];
  372. XCTestExpectation *expectation = [self expectationWithDescription:@"Should invoke reject block"];
  373. [self.uut pop:@"invalidComponentId" commandId:@"pop" mergeOptions:nil completion:^{
  374. [expectation fulfill];
  375. } rejection:^(NSString *code, NSString *message, NSError *error) {
  376. XCTAssert([code isEqualToString:@"1012"]);
  377. XCTAssert([message isEqualToString:@"Popping component failed - componentId 'invalidComponentId' not found"]);
  378. [expectation fulfill];
  379. }];
  380. [self waitForExpectationsWithTimeout:5 handler:nil];
  381. }
  382. @end