react-native-navigation的迁移库

RNNCommandsHandlerTest.m 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. @interface RNNCommandsHandlerTest : XCTestCase
  8. @property (nonatomic, strong) id<RNNRootViewCreator> creator;
  9. @property (nonatomic, strong) NSString* pageName;
  10. @property (nonatomic, strong) NSString* containerId;
  11. @property (nonatomic, strong) id emitter;
  12. @property (nonatomic, strong) RNNStore* store;
  13. @property (nonatomic, strong) RNNNavigationOptions* options;
  14. @property (nonatomic, strong) RNNRootViewController* viewController;
  15. @property (nonatomic, strong) RNNCommandsHandler* cmdHandler;
  16. @end
  17. @implementation RNNCommandsHandlerTest
  18. - (void)setUp {
  19. [super setUp];
  20. self.creator = [[RNNTestRootViewCreator alloc] init];
  21. self.pageName = @"somename";
  22. self.containerId = @"cntId";
  23. self.emitter = nil;
  24. self.options = [[RNNNavigationOptions alloc] initWithDict:@{@"title" : @"static title"}];
  25. self.store = [RNNStore new];
  26. self.viewController = [[RNNRootViewController alloc] initWithName:self.pageName withOptions:self.options withContainerId:self.containerId rootViewCreator:self.creator eventEmitter:self.emitter];
  27. [self.store setReadyToReceiveCommands:true];
  28. __unused UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
  29. [self.store setContainer:self.viewController containerId:self.containerId];
  30. self.cmdHandler = [[RNNCommandsHandler alloc] initWithStore:self.store controllerFactory:nil];
  31. }
  32. - (void)testAssertReadyForEachMethodThrowsExceptoins {
  33. RNNStore *store = [RNNStore new];
  34. [store setReadyToReceiveCommands:NO];
  35. RNNCommandsHandler *uut = [[RNNCommandsHandler alloc] initWithStore:store controllerFactory:nil];
  36. NSArray* methods = [self getPublicMethodNamesForObject:uut];
  37. for (NSString* methodName in methods) {
  38. SEL s = NSSelectorFromString(methodName);
  39. IMP imp = [uut methodForSelector:s];
  40. void (*func)(id, SEL) = (void *)imp;
  41. XCTAssertThrowsSpecificNamed(func(uut,s), NSException, @"BridgeNotLoadedError");
  42. }
  43. }
  44. -(NSArray*) getPublicMethodNamesForObject:(NSObject*)obj{
  45. NSMutableArray* skipMethods = [NSMutableArray new];
  46. [skipMethods addObject:@"initWithStore:controllerFactory:"];
  47. [skipMethods addObject:@"assertReady"];
  48. [skipMethods addObject:@".cxx_destruct"];
  49. NSMutableArray* result = [NSMutableArray new];
  50. // count and names:
  51. int i=0;
  52. unsigned int mc = 0;
  53. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  54. for(i=0; i<mc; i++) {
  55. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  56. // filter skippedMethods
  57. if (![skipMethods containsObject:methodName]) {
  58. [result addObject:methodName];
  59. }
  60. }
  61. return result;
  62. }
  63. -(void)testDynamicTopBarBackgroundColor_validColor {
  64. NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
  65. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  66. [self.cmdHandler setOptions:self.containerId options:dictFromJs];
  67. XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
  68. }
  69. -(void)testDynamicStylesMergeWithStaticStyles {
  70. NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
  71. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  72. [self.cmdHandler setOptions:self.containerId options:dictFromJs];
  73. XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
  74. XCTAssertTrue([self.viewController.navigationItem.title isEqual:@"static title"]);
  75. }
  76. @end