react-native-navigation的迁移库

RNNCommandsHandlerTest.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. XCTAssertThrowsSpecificNamed([uut performSelector:s withObject:nil], NSException, @"BridgeNotLoadedError");
  40. }
  41. }
  42. -(NSArray*) getPublicMethodNamesForObject:(NSObject*)obj{
  43. NSMutableArray* skipMethods = [NSMutableArray new];
  44. [skipMethods addObject:@"initWithStore:controllerFactory:"];
  45. [skipMethods addObject:@"assertReady"];
  46. [skipMethods addObject:@".cxx_destruct"];
  47. NSMutableArray* result = [NSMutableArray new];
  48. // count and names:
  49. int i=0;
  50. unsigned int mc = 0;
  51. Method * mlist = class_copyMethodList(object_getClass(obj), &mc);
  52. for(i=0; i<mc; i++) {
  53. NSString *methodName = [NSString stringWithUTF8String:sel_getName(method_getName(mlist[i]))];
  54. // filter skippedMethods
  55. if (![skipMethods containsObject:methodName]) {
  56. [result addObject:methodName];
  57. }
  58. }
  59. return result;
  60. }
  61. -(void)testDynamicTopBarBackgroundColor_validColor {
  62. NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
  63. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  64. [self.cmdHandler setOptions:self.containerId options:dictFromJs];
  65. XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
  66. }
  67. -(void)testDynamicStylesMergeWithStaticStyles {
  68. NSDictionary* dictFromJs = @{@"topBarBackgroundColor" :@(0xFFFF0000)};
  69. UIColor* expectedColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
  70. [self.cmdHandler setOptions:self.containerId options:dictFromJs];
  71. XCTAssertTrue([self.viewController.navigationController.navigationBar.barTintColor isEqual:expectedColor]);
  72. XCTAssertTrue([self.viewController.navigationItem.title isEqual:@"static title"]);
  73. }
  74. @end